dououde4065 2015-06-09 09:53
浏览 60
已采纳

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

I'm trying to create a plugin but at the moment I'm having trouble creating a page on activation, please see code below, where am I going wrong? It doesn't create anything but activates fine.

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条回答 默认 最新

  • douningqiu4991 2015-06-09 14:13
    关注

    It all came down to the way I was calling the function, should be:

            register_activation_hook( MYPLUGIN_FILE, array( $this, 'plugin_activated' ) );
            register_deactivation_hook( MYPLUGIN_FILE, array($this, 'plugin_deactivated' ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序