WP_INSERT_POST()从插件获取致命错误

时间:2018-04-27 作者:Devilix

我在管理区域内实现了一种“页面”,可以在没有默认方法的情况下手动插入帖子。这只是开始。。。无论如何,在启动表单的插入按钮后,插入了文章,但我得到了一个很长的错误:致命错误:未捕获错误:调用未定义函数。。。。

代码如下:

<?php
/*
Plugin Name: TEST Importer
Plugin URI: 
Description: No description.
Text Domain: 
Author: The author
Version: 1.0
Author URI: http://www.example.net/
*/

if ( ! defined( \'ABSPATH\' ) ) {
    exit;
}

add_action( \'admin_menu\', \'my_plugin_menu\' );
function my_plugin_menu() {
add_menu_page(
    \'Importer\',
    \'IMPORTER\',
    \'manage_options\',
    \'importer\',
    \'display_content\', //function
    \'dashicons-video-alt3\',
    50
    );
}
add_action( \'admin_enqueue_scripts\', \'register_importer_styles\' );
function register_importer_styles() {
    wp_enqueue_style( \'importer\', plugins_url(\'importer.css\', __FILE__) );
    wp_enqueue_script( \'importer\', plugins_url(\'importer.js\', __FILE__) );
}

if (isset($_POST[\'insert\'])) {
    $title = $_POST[\'data\'];
/*require_once( ABSPATH . \'wp-load.php\' );*/

$my_post = array(
\'post_title\'    => $title,
\'post_content\'  => \'content here\',
\'post_status\'   => \'draft\',
\'post_author\'   => 1,
);

wp_insert_post( $my_post );

/*if ($post_id) {
// insert post meta
add_post_meta($post_id, \'duration\', $seconds);
add_post_meta($post_id, \'quality\', $is_hd);
}*/

}

add_action( \'admin_head\', \'my_custom_admin_head\' );
function my_custom_admin_head() { ?>
<script type="text/javascript">
    //Some javascript code...
</script>;
<?php }


function display_content() { ?>

<div id="main_importer_area">

<form action="<?php echo admin_url(\'admin.php?page=importer\'); ?>" method="post">
    <input id="jx_text" type="text" name="data" >
    <input id="ins_btn" type="submit" value="Insert" name="insert">
</form>

</div>
<?php } ?>
我希望有人能帮助我。。。非常感谢!

Fatal error: Uncaught Error: Call to undefined function is_user_logged_in() in /home/***/public_html/wp-includes/post.php:2191
Stack trace:
#0 /home/***/public_html/wp-includes/post.php(6008): _count_posts_cache_key(\'post\', \'readable\')
#1 /home/***/public_html/wp-includes/class-wp-hook.php(286): _transition_post_status(\'draft\', \'new\', Object(WP_Post))
#2 /home/***/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(\'\', Array)
#3 /home/***/public_html/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#4 /home/***/public_html/wp-includes/post.php(4036): do_action(\'transition_post...\', \'draft\', \'new\', Object(WP_Post))
#5 /home/***/public_html/wp-includes/post.php(3496): wp_transition_post_status(\'draft\', \'new\', Object(WP_Post))
#6 /home/***/public_html/wp-content/plugins/importer/importer.php(45): wp_insert_post(Array)
#7 /home/***/public_html/wp-settings.php(305): include_once(\'/home/yo.../...\')
#8 /home/***/public_html/wp-config.php(89): require_o in /home/***/public_html/wp-includes/post.php on line 2191

2 个回复
SO网友:Orlando P.

因此,如果我理解正确,您将在正常wordpress范围之外创建一个自定义导入插件到wordpress。

因此,您无法访问其中的功能.pluggable.php 喜欢is_user_logged_in 你可以在插件中重新定义它们,也可以直接使用

require_once( ABSPATH . "wp-includes/pluggable.php" );
如果没有,您也将无法使用wp\\u insert\\u post

require_once( ABSPATH . "wp-includes/wp-load.php" );
在裸PHP中加载WordPress环境通常是不好的做法。

我建议您在WordPress环境中加载插件。您可以通过使用短代码或自定义模板呈现表单来实现这一点,而所有这些函数都将对您可用。

SO网友:bhumi patel

我也有同样的问题。但我用以下方法修复了它:

require_once( ABSPATH . "wp-includes/pluggable.php" );

结束

相关推荐

正在将外部Java脚本函数入队。php

由于某种原因,我似乎无法将外部JS文件排队。我可以将外部css文件排队,但不能将JS文件排队。我有一个使用引导css框架的自定义主题。我的职能。php如下:function add_css_scripts(){ wp_enqueue_style(\'boot-strap\',\'https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\',false); wp_enqueue_style