0

我正在使用 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 中有一些东西,但无论文件内容如何,​​它都被视为字符串。这个警告指的是什么?关于绕过它的任何想法?

4

1 回答 1

0

关闭 kses 过滤器。过滤器实际上正在更改您要插入的代码。https://developer.wordpress.org/reference/functions/kses_remove_filters/

在插入之前将它们关闭,然后重新打开。

于 2014-07-09T06:01:12.117 回答