2012-09-27 75 views
3

我目前正在寫一個wordpress插件,我遇到了一些問題。 我的功能不會在插件激活時運行...有人可以告訴我問題在哪裏?WordPress的:register_activation_hook不工作

 


class my_plugin { 

    public $ajaxurl; 
    public $excanvas; 
    public $plugin_path = ''; 

    function __construct() 
    { 

     register_activation_hook(__FILE__,array(&$this, 'install')); 
    } 


    public function wpmon_install() 
    { 
     //Copy my page template to current theme. 

     $plugin_path = getcwd() . DIRECTORY_SEPARATOR . 'wp-content'. DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'wpmon' . DIRECTORY_SEPARATOR; 
     $target_path = get_stylesheet_directory(). DIRECTORY_SEPARATOR; 
     $target_path = str_replace('/',DIRECTORY_SEPARATOR,$target_path); 
     $template_files = glob($plugin_path . 'page_template'.DIRECTORY_SEPARATOR.'*.php'); 

     foreach($template_files as $files) 
     { 
      $basename = basename($files); 
      try{ 
       $target = $target_path . $basename; 
       copy($files , $target); 

      } 
      catch(Exception $e){ 

       $this->log_error($e->getMessage()); 
      } 
     } 
    } 

可惜安裝功能不工作... 但當外類中「安裝」這個代碼FUNC工作

回答

0

你的WordPress試圖調用中調用的類install功能,但它不存在。因此,嘗試改變:

register_activation_hook(__FILE__,array(&$this, 'install')); 

register_activation_hook(__FILE__,array(&$this, 'wpmon_install')); 
+0

我也嘗試過,但仍然沒有好... – Monski

0

從公佈的代碼,我可以看到兩個問題。安裝應該更改爲wpmon_install。你也應該添加一些代碼來初始化你的類。

$wp_mon = new my_plugin();class my_plugin { }

+0

是的,我初始化它的方式..對不起,我沒有把它包括在代碼中... – Monski

0

請檢查你使用軟鏈接,該插件必須完全在/path/to/wordpress/wp-content/plugins/yourplugin,意思是說,yourplugin不是軟鏈接。