使用Ajax添加WordPress帖子

使用Ajax添加WordPress帖子

许多WordPress主题和插件都带有一些演示数据(帖子和页面),用户可以在激活插件时安装它们。这些WordPress帖子和页面旨在为用户提供设置主题或插件的先机,并为他们提供一些随时可用的虚拟数据供您使用。

在Chamber Dashboard插件中,尽管我们有大量的文档和几个演示站点供用户查看,但是对于某些用户而言,要直观地看到其主题的业务目录页面仍然不容易。因此,我们决定添加一些演示内容,这些内容将在用户首次在其站点上激活Business Directory插件时安装。

用于添加演示数据的按钮
演示数据按钮在欢迎页面上以模态显示。使用jQuery .dialog()函数显示模式。


$("#demo_content_buttons").dialog({
modal: true,
buttons: [
{
text: "Ok",
click: function() {
$( this ).dialog( "close" );
}
}
]
});

这是用于添加演示数据按钮的html。“ loader” div将显示何时添加数据以及完成数据时,消息将显示在success_message div中。最初,加载器div使用jQuery隐藏。


function cdash_install_demo_content_button(){
?>
<div id="demo_content_buttons">
<p>Welcome to the Business Directory plugin! Install demo content now, then check out the Business Directory page to see a sample Directory, or dismiss this notice to get started with uploading your own listings.</p>
<p class="demo_content button cdash_admin button-primary">Install Demo Content</p>
<p class="demo_content_decline button cdash_admin button-primary">No Thanks!</p>
<p id="loader">Loading...</p>
<p class="cdash_demo_success_message"></p>
</div>
<?php
}
$("#loader").css("display", "none");

设置瞬态
此功能的基本要求之一是,演示数据安装按钮仅在用户首次安装并激活插件时才显示。已经使用过它的用户在将其插件更新到新版本时不应看到它。因此,为了照顾这两种情况,我添加了一个瞬态。首次激活插件时,瞬态将设置为true。仅在未设置瞬变的情况下,演示数据按钮才会显示。


//If plugin is activated for the first time, set the transient
function cdash_show_demo_buttons(){
//$demo_content_install = get_transient('cdash_demo_content_install');
if(false === get_transient('cdash_demo_content_install')){
//show the Install Demo Content button
cdash_install_demo_content_button();
}
set_transient('cdash_demo_content_install', 'true');
}

如果插件已更新,则瞬变也设置为true。这样可以确保如果用户只是在更新插件,他们将不会看到弹出窗口询问他们是否要安装演示数据。


function cdash_set_demo_transient(){
set_transient('cdash_demo_content_install', 'true');
}
add_action( 'upgrader_process_complete', 'cdash_set_demo_transient');

Ajax调用演示数据
实际的演示数据将通过单击“安装”按钮时触发的Ajax调用添加。

$(".demo_content.button").click(function(event){
event.preventDefault();
$.ajax({
url: ajaxurl,
type:'post',
data: {
'action': 'cdash_add_demo_data',
},
beforeSend: function() {
$('#loader').show();
$(".demo_content_decline").hide();
},
complete: function(){
$('#loader').hide();
$(".dashboard_page_cdash-about .ui-dialog-buttonpane").show();
},
success: function(response){
$(".cdash_demo_success_message").html(response);
},
});
});

Ajax函数调用cdash_add_demo_data()函数。该功能添加了业务类别,演示业务和几个演示页面。如果页面或帖子已经存在,将不再创建。


function cdash_add_demo_data(){
$response = '';
//Create demo business categories
cdash_demo_bus_categories();
//Creating demo businesses
$demo_post_id = cdash_insert_demo_business();
//Creating demo pages
$demo_page_id = cdash_add_demo_pages();
//Check if there the post or page exists
if ( ($demo_post_id != 0) || $demo_page_id !=0 )
{
//If post or page does not exists
$response = 'Demo data successfully added.';
}
else {
//Post or page exists
$response = 'The data already exists.';
}
// Return the String
die($response);
}
// creating Ajax call for WordPress
add_action( 'wp_ajax_cdash_add_demo_data', 'cdash_add_demo_data' );

让我们更详细地看一下添加演示内容功能。使用wp_insert_term()添加类别。添加业务类别的功能将检查该类别是否已经存在,以避免任何重复的类别。

下一步是添加业务。演示内容位于数组中。演示按钮将在自定义帖子类型“ business”中安装3个新帖子。第一步是创建post对象。


// Create post object
$demo_content = "Create a description for your business here, or install the Member Updater plugin so your members can update their own listings!";
$demo_bus_data = array(
array (
'title' => 'Karleton’s Bakery',
'content' => $demo_content,
'post_category' => 'Restaurants',
'featured_image' => 'images/demo_content/bakery_photogy-karlis-dambrans.jpg',
),
array (
'title' => 'Wong’s Coffee & Tea',
'content' => $demo_content,
'post_category' => 'Restaurants',
'featured_image' =>'images/demo_content/coffee_shopphotoby-jason-wong.jpg',
),
array (
'title' => 'Brendon’s Camera',
'content' => $demo_content,
'post_category' => 'Retail Shops',
'featured_image' =>'images/demo_content/camera_shop_photoby-brendan-church.jpg',
),
);

下一步是遍历post对象并插入post。


foreach ($demo_bus_data as $bus_demo) {
if ( post_exists( $bus_demo['title'] ) ) {
$demo_post_id = 0;
}else{
//post exists
$add_pages = array(
'post_title' => $bus_demo['title'],
'post_content' => $bus_demo['content'],
'post_status' => 'publish',
'post_type' => 'business'
);
// Insert the post into the database
$demo_post_id = wp_insert_post( $add_pages );
}

然后,需要设置帖子的类别。

wp_set_object_terms( $demo_post_id, $bus_demo['post_category'], 'business_category', $append );

由于演示数据用于企业目录,因此我想为演示企业添加特色图片。我将图像添加到插件内的文件夹中,并从路径生成了图像URL。url和post_id用于将图像上传到媒体库并将其添加到帖子中。cdash_insert_attachment_from_url()使用来自
https://gist.github.com/m1r0/f22d5237ee93bcccb0d9的代码 添加特色图片。

//attach the image files as featured image for each post
$getImageFile = plugins_url( $bus_demo['featured_image'], dirname(__FILE__) );
$url = $getImageFile;
$attach_id = cdash_insert_attachment_from_url($url, $demo_post_id);
set_post_thumbnail( $demo_post_id, $attach_id );

用于插入具有内容和特色图像的演示业务的完整代码。

function cdash_insert_demo_business(){
//Code to create a post with demo data
global $wpdb;
$user_id = get_current_user_id();
// Create post object
$demo_content = "Create a description for your business here, or install the Member Updater plugin so your members can update their own listings!";
$demo_bus_data = array(
array (
'title' => 'Karleton’s Bakery',
'content' => $demo_content,
'post_category' => 'Restaurants',
'featured_image' => 'images/demo_content/bakery_photogy-karlis-dambrans.jpg',
),
array (
'title' => 'Wong’s Coffee & Tea',
'content' => $demo_content,
'post_category' => 'Restaurants',
'featured_image' =>'images/demo_content/coffee_shopphotoby-jason-wong.jpg',
),
array (
'title' => 'Brendon’s Camera',
'content' => $demo_content,
'post_category' => 'Retail Shops',
'featured_image' =>'images/demo_content/camera_shop_photoby-brendan-church.jpg',
),
);
foreach ($demo_bus_data as $bus_demo) {
if ( post_exists( $bus_demo['title'] ) ) {
$demo_post_id = 0;
}else{
//post exists
$add_pages = array(
'post_title' => $bus_demo['title'],
'post_content' => $bus_demo['content'],
'post_status' => 'publish',
'post_type' => 'business'
);
// Insert the post into the database
$demo_post_id = wp_insert_post( $add_pages );
wp_set_object_terms( $demo_post_id, $bus_demo['post_category'], 'business_category', $append );
//attach the image files as featured image for each post
$getImageFile = plugins_url( $bus_demo['featured_image'], dirname(__FILE__) );
$url = $getImageFile;
$attach_id = cdash_insert_attachment_from_url($url, $demo_post_id);
set_post_thumbnail( $demo_post_id, $attach_id );
}
}
return $demo_post_id;
}

添加帖子后,还将添加两个新页面。这些页面的添加类似于帖子,只是“ post_type”将是“ page”而不是“ business”(这是自定义帖子类型)。由于页面没有类别或特色图像,因此仅创建post对象并循环遍历即可添加页面。该功能还检查页面是否已存在,以避免重复页面。

Leave a Reply

Your email address will not be published. Required fields are marked *