面向对象方法中的注册激活钩子

时间:2013-02-11 作者:Yves Gonzaga

我尝试用面向对象的方法开发插件。我尝试加载构造函数类中所需的所有函数,例如动作挂钩。问题是这个挂钩“register\\u activation\\u hook”。当插件被激活时,它不会加载。我的主插件文件位于插件的根目录中,我的类文件位于“classes”文件夹中。

我在这个链接上阅读了这个函数的用法http://codex.wordpress.org/Function_Reference/register_activation_hook 它说$file参数是:

wp-content/plugins目录中主插件文件的$file(string)(必需)路径。完整路径将起作用。

所以我试着在我的Constructor类中这样使用它。

register_activation_hook(__DIR__ . \'/../yslider.php\',array($this, \'yslider_install\'));//Installing data 
但仍然不起作用。如果我把它放在主插件文件中,它就会工作。但我认为这不可能是一种面向对象的方法吗?

谁能告诉我在oop appraoch中使用这个函数的正确方法是什么?

4 个回复
SO网友:djb

这并不重要。如果必须在类中包含它,我将使用常量和静态方法。

// in the main plugin file
define( \'MYPLUGIN_FILE\', __FILE__ );

// include another file with this class in
class MyPlugin {

    public static function init() {
        register_activation_hook( MYPLUGIN_FILE, array( \'MyPlugin\', \'install\' ));
    }

    public static function install() {
         echo "little lamb, who made thee? is it wordpress or is it me";
    }
}

// call the static method
MyPlugin::init();

SO网友:Saiful Islam
//define
define(\'DIR_PATH\',plugin_dir_path(__FILE__));
//class
class nssUser_creator {
    //method
    static function nss_init() {
        register_activation_hook(DIR_PATH,array(\'nssUser_creator\',\'nss_plug_install\'));
    }
    static function nss_plug_install(){
        echo \'YOUR plugins is now activated ! YeaP\';
    }
}//end class

//object
nssUser_creator::nss_init();
SO网友:Iván Carrasco

此外,在更为面向对象的方法中,您可以通过以下方式完成:

在插件类中:

class MyPlugin {

    private $file_base;

    /* 
     * Useful in case you put a lot of stuff in this class that
     * could be used later from other classes without knowing if the
     * class has already been instantiated. If it\'s just for the 
     * initialization without further using, go with a simple 
     * constructor
     */
    public static function get_instance(){
         static $plugin;
         if ( !isset( $plugin ) ){
             $plugin = new MyPlugin();
         }
         return $plugin;
    }  

    /*
     * If you go with a singleton, make it private, public otherwise
     */
    private function __construct() {
         $this->file_base = plugin_dir_path( dirname( __FILE__ ) ) . \'your_plugin_main_file.php\';
         $this->init();
    }

    /*
     * Should be invoked just from the constructor, so private
     */
    private function init() {
         register_activation_hook( $this->file_base, array( $this, \'install\' ) );

    }

    /*
     * This is the callback, so make it public
     */
    public function install() {
         echo "little lamb, who made thee?";
    }
}
然后在插件主文件(例如:your\\u plugin\\u main\\u file.php)中,只需:

MyPlugin::get_instance();

SO网友:csaborio

根据@Ivan Carrasco建议的代码,我创建了以下文件:

https://gist.github.com/csaborio001/51c8c808ef1bc09e284ea012e6e72dbf

这样调用它:

$match_score                = new Match_Score();
$plugin_activation_instance = PluginActivation::get_instance( \'compeer-matching-plugin.php\', \'vinnies_connect_matching_cron\', \'daily\' );
$plugin_activation_instance->init();
$plugin_activation_instance->set_cron_callback( $match_score, \'calculate_all_scores\' );

结束

相关推荐

plugins_url vs plugin_dir_url

我看到WordPress插件在为一些文件夹创建常量时使用plugins\\u url或plugin\\u dir\\u url。一个比另一个好吗?示例:define( \'MEMBERS_URI\', trailingslashit( plugin_dir_url( __FILE__ ) ) ); define( \'WPACCESS_INC\', plugins_url( \'inc\', __FILE__ ) , true );