在插件激活时创建页面时遇到问题

时间:2015-06-09 09:53:09

标签: php wordpress

我正在尝试创建一个插件,但目前我在创建激活页面时遇到问题,请参阅下面的代码,我哪里出错了?它没有创造任何东西,但激活很好。

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 );
        }   

    }
}

3 个答案:

答案 0 :(得分: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 );
        }   

    }
}

答案 1 :(得分: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();

}

此代码将起作用。您应该初始化对象。我曾评论过一些不需要的代码。希望这会有用。

答案 2 :(得分:0)

这一切都归结为我调用函数的方式,应该是:

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