Rizwan
Rizwan

Reputation: 308

Fatal error: Call to undefined function is_user_logged_in()

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

Upvotes: 2

Views: 10928

Answers (1)

Max
Max

Reputation: 765

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');

Upvotes: 13

Related Questions