2015-06-09 49 views
3

我想創建一個插件,但此刻我在創建激活頁面時遇到問題,請參閱下面的代碼,我該在哪裏出錯?它不會創造任何東西,但會激活正常。無法在插件激活時創建頁面

include('add-acf.php'); 


if(!class_exists('TW_Contact')) { 
    class TW_Contact { 


     public function __construct() {   
      add_filter('single_template', array($this, 'tw_single_template'));    
      add_shortcode('tw_contact', array($this, 'tw_contact_shortcode')); 

      $this->bootstrap(); 

      //echo 'construct'; 
      //exit; 
     } 


     /** 
     * Setup the environment for the plugin 
     */ 
     public function bootstrap() { 

      //echo 'bootstrap'; 
      //exit; 

      register_activation_hook(__FILE__, array($this, 'activate')); 
      register_activation_hook(__FILE__, 'my_plugin_install_function'); 
      add_action('init', array($this, 'register_custom_fields')); 
      add_action('wp_enqueue_scripts', array($this, 'tw_enqueue_scripts')); 
     } 


     /** 
     * Do some stuff upon activation 
     */ 
     public function activate() { 
      $this->register_custom_fields(); 
      $this->tw_enqueue_scripts(); 

      echo 'before'; 

      // Flush rewrite rules so that users can access custom post types on the front-end right away 
      flush_rewrite_rules(); 

      echo 'after'; 
     } 

     function my_plugin_install_function() { 
      //post status and options 
      $post = array(
        'comment_status' => 'closed', 
        'ping_status' => 'closed' , 
        'post_author' => 1, 
        'post_date' => date('Y-m-d H:i:s'), 
        'post_name' => 'Checklists', 
        'post_status' => 'publish' , 
        'post_title' => 'Checklists', 
        'post_type' => 'page', 
      ); 
      wp_insert_post($post); 
     } 

    } 
} 

回答

0

這一切都來到了我被調用函數的方式,應該是:

 register_activation_hook(MYPLUGIN_FILE, array($this, 'plugin_activated')); 
     register_deactivation_hook(MYPLUGIN_FILE, array($this, 'plugin_deactivated')); 
0

確保頁面「清單」尚未創建。 試試看密碼。

include('add-acf.php'); 


if(!class_exists('TW_Contact')) { 
    class TW_Contact { 


     public function __construct() {   
      add_filter('single_template', array($this, 'tw_single_template'));    
      add_shortcode('tw_contact', array($this, 'tw_contact_shortcode')); 

      $this->bootstrap(); 

      //echo 'construct'; 
      //exit; 
     } 


     /** 
     * Setup the environment for the plugin 
     */ 
     public function bootstrap() { 

      //echo 'bootstrap'; 
      //exit; 

      register_activation_hook(__FILE__, array($this, 'activate')); 
      register_activation_hook(__FILE__, array($this,'my_plugin_install_function'); 
      add_action('init', array($this, 'register_custom_fields')); 
      add_action('wp_enqueue_scripts', array($this, 'tw_enqueue_scripts')); 
     } 


     /** 
     * Do some stuff upon activation 
     */ 
     public function activate() { 
      $this->register_custom_fields(); 
      $this->tw_enqueue_scripts(); 

      echo 'before'; 

      // Flush rewrite rules so that users can access custom post types on the front-end right away 
      flush_rewrite_rules(); 

      echo 'after'; 
     } 

     function my_plugin_install_function() { 
      //post status and options 
      $post = array(
        'comment_status' => 'closed', 
        'ping_status' => 'closed' , 
        'post_author' => 1, 
        'post_date' => date('Y-m-d H:i:s'), 
        'post_name' => 'Checklists', 
        'post_status' => 'publish' , 
        'post_title' => 'Checklists', 
        'post_type' => 'page', 
      ); 
      wp_insert_post($post); 
     } 

    } 
} 
+0

仍然沒有運氣,只是不上激活創建頁面。我還沒有檢查單頁面。 – Rob

+0

你在插件激活時是否有任何錯誤 – sarath

0
if(!class_exists('TW_Contact')) { 
    class TW_Contact { 


     public function __construct() {   
      // add_filter('single_template', array($this, 'tw_single_template'));    
      // add_shortcode('tw_contact', array($this, 'tw_contact_shortcode')); 

      $this->bootstrap(); 

      //echo 'construct'; 
      //exit; 
     } 


     /** 
     * Setup the environment for the plugin 
     */ 
     public function bootstrap() { 

      //echo 'bootstrap'; 
      //exit; 

      // register_activation_hook(__FILE__, array($this, 'activate')); 
      register_activation_hook(__FILE__, array($this, 'my_plugin_install_function')); 
      // add_action('init', array($this, 'register_custom_fields')); 
      // add_action('wp_enqueue_scripts', array($this, 'tw_enqueue_scripts')); 
     } 


     /** 
     * Do some stuff upon activation 
     */ 
     public function activate() { 
     // $this->register_custom_fields(); 
      $this->tw_enqueue_scripts(); 

      // echo 'before'; 

      // Flush rewrite rules so that users can access custom post types on the front-end right away 
      // flush_rewrite_rules(); 

      // echo 'after'; 
     } 

     function my_plugin_install_function() { 
      //post status and options 
      $post = array(
        'comment_status' => 'closed', 
        'ping_status' => 'closed' , 
        'post_author' => 1, 
        'post_date' => date('Y-m-d H:i:s'), 
        'post_name' => 'Checklists', 
        'post_status' => 'publish' , 
        'post_title' => 'Checklists', 
        'post_type' => 'post', 
      ); 
      wp_insert_post($post); 
     } 

    } 

    global $obj; 
    $obj = new TW_Contact(); 

} 

此代碼將工作。你應該初始化對象。我曾經評論過一些不需要的代碼。希望這會起作用。

+0

仍然沒有運氣,我不知道這是否是因爲它在一個插件中,但插入帖子功能在正常模板中工作時非常簡單。只是不明白爲什麼它不起作用! – Rob

+0

我查過了。爲我工作.. – sarath

+0

確實使用過上面的代碼? – sarath

相關問題