0

I'm realizing a sort of "page" inside admin area to insert post manually without the default method. This is only the begin... Anyway, after firing Insert button of the form, the article is inserted, but i get a big long error: Fatal error: Uncaught Error: Call to undefined function ....

Here is the code:

<?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 } ?>

I hope somebody can help me... thanks a lot!

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
1

So If I understand correctly you are creating a custom import plugin to wordpress outside the normal wordpress scope.

Therefore you don't have access to the functions inside .pluggable.php like is_user_logged_in you can redefine them in your plugin or you can just use

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

You will also not be able to use wp_insert_post without

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

loading the WordPress environment in your naked PHP is most often bad practice.

I would recommend loading your plugin within the WordPress environment. You can do this by rendering your form with a shortcode or a custom template than all these functions will be available to you.

  • Added full error at the top. – Devilix Apr 27 '18 at 17:58
  • Yes, with require_once( ABSPATH . "wp-includes/pluggable.php" ); it works! Was my target be under WP environment... but i dont understand HOW i did it... i think was enough what i have already done. – Devilix Apr 27 '18 at 19:08
  • you could be help me to put this under wp env? – Devilix Apr 27 '18 at 19:18
  • Hey sorry were you able to figure it out @Devilix – Orlando P. Apr 30 '18 at 20:47
  • i have solved the error, but not the wp environment issue... – Devilix May 1 '18 at 23:35
-2

I also had the same issue. But i fixed it with:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.