我正在编写一个WordPress插件,我想执行以下操作:
添加一个包含按钮的WordPress插件管理页面当您单击按钮时,它会从文件中加载一个JSON对象数组,然后为每个对象创建一个WordPress帖子,我已经完成了,我可以看到在单击按钮时加载的JSON对象数组,因为我使用了console.log
. (我用this tutorial - 使用AJAX。)
但是,我如何才能让WordPress访问JSON数组,以便使用wp_insert_posts
? 这可能吗?
我正在编写一个WordPress插件,我想执行以下操作:
添加一个包含按钮的WordPress插件管理页面当您单击按钮时,它会从文件中加载一个JSON对象数组,然后为每个对象创建一个WordPress帖子,我已经完成了,我可以看到在单击按钮时加载的JSON对象数组,因为我使用了console.log
. (我用this tutorial - 使用AJAX。)
但是,我如何才能让WordPress访问JSON数组,以便使用wp_insert_posts
? 这可能吗?
这里我假设您的json是一个对象数组,其中的属性命名如下wp_insert_post
arguments: \'post\\u标题、“post\\u内容”等。
function process_ajax() {
if ( ! isset($_POST[\'nonce\']) || ! wp_verify_nonce($_POST[\'nonce\'], \'mynonce\') )
die(\'error on checking nonce\');
if ( ! isset($_POST[\'filepath\']) die(\'no file given\');
if ( ! file_exists($_POST[\'filepath\']) ) die(\'invalid file given\');
$posts = json_decode( file_get_contents($_POST[\'filepath\']) );
$done = 0;
if ($posts) {
foreach ( $posts as $post) {
$post = (array)$post;
if ( isset($post[\'ID\']) ) unset($post[\'ID\']);
if ( wp_insert_post($post) ) $done++;
}
$str = "Successfully insert " . $done . "posts, ";
$str .= ( count($posts) - $done ) . \' failed.\'
die($str);
} else {
die(\'File contains not valid Json.\');
}
}
这是我的搜索js代码,用于处理搜索:jQuery(document).ready(function($){ $(\'#searchsubmit\').click(function(e){ e.preventDefault(); var searchval = $(\'#s\').val(); // get search term $.post( WPaAj