我正在使用wp_insert_post获取文件的html并自动创建帖子。我有一个功能,搜索目录,打开并提取正确的HTML。这些文件有日期(这就是我使用下面的subsrt()的原因)。 html是一个字符串,就像我用于内容的任何其他东西一样。
<?PHP
require_once(dirname(__FILE__)."/createpost.php");
$dir = dirname(__FILE__)."/html";
$files = scandir($dir);
$date = (string) date("Ymd");
foreach($files as $value){
if((substr($value,5,8)) == $date){
$html = file_get_contents($dir."/".$value);
createpost($value, $html);
}
}
以上作品。下面是createpost()。
<?php
function createpost($post_title, $post_content){
require_once("/var/www/wp-load.php");
$mypost = array(
'ID' => $post_id,//[ <post id> ] //Are you updating an existing post?
'menu_order' => $menu_order,//[ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs.
'comment_status' => $comment_status,//[ 'closed' | 'open' ] // 'closed' means no comments.
'ping_status' => $ping_status,//[ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
'pinged' => $pinged,//[ ? ] //?
'post_author' => $post_author,//[ <user ID> ] //The user ID number of the author.
'post_category' => $post_category,//[ array(<category id>, <...>) ] //post_category no longer exists, try wp_set_post_terms() for setting a post's categories
'post_content' => "[raw]\n\n".$post_content."\n\n[/raw]",//[ <the text of the post> ] //The full text of the post.
'post_date' => $post_date,//[ Y-m-d H:i:s ] //The time post was made.
'post_date_gmt' => $post_date_gmt,//[ Y-m-d H:i:s ] //The time post was made, in GMT.
'post_excerpt' => $post_excerpt,//[ <an excerpt> ] //For all your post excerpt needs.
'post_name' => $post_name,//[ <the name> ] // The name (slug) for your post
'post_parent' => $post_parent,//[ <post ID> ] //Sets the parent of the new post.
'post_password' => $post_password,//[ ? ] //password for post?
'post_status' => 'private',//[ 'draft' | 'publish' | 'pending'| 'future' | 'private' | 'custom_registered_status' ] //Set the status of the new post.
'post_title' => $post_title,//[ <the title> ] //The title of your post.
'post_type' => $post_type,//[ 'post' | 'page' | 'link' | 'nav_menu_item' | 'custom_post_type' ] //You may want to insert a regular post, page, link, a menu item or some custom post type
'tags_input' => $tags_input,//[ '<tag>, <tag>, <...>' ] //For tags.
'to_ping' => $to_ping,//[ ? ] //?
'tax_input' => $tax_input//[ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies.
);
// Insert the post into the database
wp_insert_post( $mypost );
}
?>
当这一切全部运行时,我得到以下无限错误,并且永远不会创建帖子:
PHP Warning: in_array() expects parameter 2 to be array, null given in /var/www/wp-includes/kses.php on line 1146
PHP Warning: in_array() expects parameter 2 to be array, null given in /var/www/wp-includes/kses.php on line 1146
PHP Warning: in_array() expects parameter 2 to be array, null given in /var/www/wp-includes/kses.php on line 1146....
如果我注释掉或更改html文件的内容,则运行正常。看起来html中有一些东西,但无论文件内容如何,它都被视为一个字符串。这个警告是指什么?有关绕过它的任何想法吗?
答案 0 :(得分:0)
关闭kses过滤器。过滤器实际上是在更改要插入的代码。 https://developer.wordpress.org/reference/functions/kses_remove_filters/
在插入之前将它们关闭然后重新打开。