Question

I am developing a Wordpress Plugin. While using (wp_insert_post) I got this error:

Fatal error: Call to undefined function is_user_logged_in() in /home/designs/public_html/WP/wp-includes/post.php on line 2185

Here is the file on pastebin: http://pastebin.com/YETGT4dK This file is included in main plugin file.

Thank you !


The relevant section of the pastebin is

if(isset($_POST['save_coupons']) and $_POST['save_coupons']=='yes'){


    //lots of stuff

    /*
     * Inserting New Coupon as Post
    */
    $post = array();
    $post['post_status']   = 'publish';
    $post['post_type']     = 'coupons';
    $post['post_title']    = $title;
    $post['post_content']  = $description;

    $post_id = wp_insert_post( $post );

    //lots more stuff


}//endif
Was it helpful?

Solution

The problem is that is_user_logged_in is a pluggable function, and is therefore loaded after this plugin logic is called. The solution is to make sure that you don't call this too early. This can be done by wrapping this logic in a function and calling it from 'init'

function rizwan_insert_coupons()
{
    if(isset($_POST['save_coupons']) and $_POST['save_coupons']=='yes'){


        //lots of stuff

        /*
         * Inserting New Coupon as Post
        */
        $post = array();
        $post['post_status']   = 'publish';
        $post['post_type']     = 'coupons';
        $post['post_title']    = $title;
        $post['post_content']  = $description;

        $post_id = wp_insert_post( $post );

        //lots more stuff


    }
}
add_action('init', 'rizwan_inert_coupons');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top