2013-08-01 70 views
0

因此,我正在编写一个插件,用于解析json提要并从提要中以编程方式生成页面。我想以编程方式创建一个将成为页面作者的用户。问题是我打电话username_exists()这个函数内部调用get_user_by()最终是未定义的。我的猜测是,我需要采取一些行动,或者需要先完成一些其他事件,但我不知所措。下面的代码,并且错误Apache是​​抛回:get_user_by()undefined in wordpress

/** 
* A simple class for the cron user, ie the 'author' that will 
* generate pages from the feed 
*/ 
class PP_CronUser { 

    private static $cronUserName = 'Cron User'; 
    private static $cronEmail = 'asdf'; 
    private static $cronPW = 'asdf'; 
    private static $ID = null; 

    public static function getUserID() { 
    if(!is_null(self::$ID)) return self::$ID; 
    if(!($id = username_exists(self::$cronUserName))) { //Here's the offending line 
    self::$ID = $id; 
    return $id; 
    } 
    self::$ID = wp_create_user(self::$cronUserName, self::$cronPW, self::$cronEmail); 
    return self::$ID; 
    } 
} 

错误:

Fatal error: Call to undefined function get_user_by() in /home/who_cares/wordpress/wp-includes/user.php on line 1198

所以username_exists定义,但这在内部调用get_user_by这是没有定义的。有任何想法吗?

+0

这不是*重复*,但问题是一样的,并可能解决,[wordpress插件 - >调用未定义函数wp_get_current_user()](http://stackoverflow.com/q/6127559/1432801 ) –

回答

2

因此,你只需要调用wp-blog-header.php你的插件文件的顶部确保你所提到的正确路径

require('path/to/wp-blog-header.php'); 

Note this Fatal error: Call to undefined function get_user_by() error occurs when you call any undefined function or there is no definition of the function

+0

什么应该是正确的路径? –

0

我是正确的思维,我需要挂钩的东西。我直接从插件调用这个方法。附加到admin_menu挂钩允许加载所有必要的库。

+0

请使用您问题上的编辑链接添加其他信息。后回答按钮应该只用于问题的完整答案。 – Patrick

+0

@Patrick这个特定的帖子不应该是一个编辑 - 这是一个自我回答。 –

+0

最正确的操作可能是''init'',请参见[答案](http://stackoverflow.com/a/6127607/1432801) –