致命错误:调用未定义的函数is_user_logged_in()

时间:2014-05-09 10:22:10

标签: error-handling wordpress-plugin wordpress

我正在开发一个Wordpress插件。使用(wp_insert_post)时出现此错误:

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

以下是pastebin上的文件:http://pastebin.com/YETGT4dK此文件包含在主插件文件中。

谢谢!


pastebin的相关部分是

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

1 个答案:

答案 0 :(得分:11)

问题是is_user_logged_in是一个可插入的函数,因此在调用此插件逻辑后加载。解决方案是确保您不要过早地打电话。这可以通过将此逻辑包装在一个函数中并从' 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');