在WordPress中激活插件时调用我的函数的问题

时间:2019-06-26 作者:Faisal Janjua

我正在创建新的Wordpress插件,并将代码拆分为不同的文件以进行代码管理。这是我的主插件文件的代码my-best-plugin.php代码如下:

<?php
    if ( ! defined(\'ABSPATH\') ){
        die;
    }

    if(!defined(\'MY_PLUGIN_PATH\')) { 
        define( \'MY_PLUGIN_PATH\', plugin_dir_path(__FILE__) ); 
    }

    if( ! class_exists(\'MyBestPlugin\') ){
        class MyBestPlugin {

            function mbp_activate(){
                require_once MY_PLUGIN_PATH .\'inc/mbp-plugin-activate.php\';
                MBPActivate::activate();
            }

        }

        $pluginInstance = new MyBestPlugin();

        // activation
        register_activation_hook( __FILE__ , array( $pluginInstance, \'mbp_activate\' ) );

    }
现在,我的第二个激活文件位于inc/mbp-plugin-activate.php 代码如下:

<?php

    class MBPActivate {

        public static function activate(){
            // I want to do more here by calling functions
            // cpt(); 
            // dont know how to call function cpt()
            // also not sure is it running properly or not :P
            add_action(\'init\', array( \'MBPActivate\',\'cpt\' ));
            flush_rewrite_rules();
        }

        public static function cpt(){
            register_post_type(\'book\', [\'public\'=>true, \'label\'=>\'Books\']);
        }

    }
首先,我不确定我的插件激活文件是否正在运行,但没有给出错误,而且当我调用自己的函数时,它会给出致命错误或已发送的标题错误。请告诉我如何在激活插件文件的激活过程中调用我的函数。提前感谢:)

2 个回复
SO网友:Techno Deviser

我会这样做:-

   // Make sure we don\'t expose any info if called directly
if ( ! defined(\'ABSPATH\') ){
    die;
}

if(!defined(\'MY_PLUGIN_PATH\')) { 
    define( \'MY_PLUGIN_PATH\', plugin_dir_path(__FILE__) ); 
}

if( ! class_exists(\'MyBestPlugin\') ){
        class MyBestPlugin {

            public function __construct(){
                register_activation_hook( __FILE__ , array( $this, \'mbp_activate\' ) );
                register_deactivation_hook( __FILE__ , array( $this, \'mbp_deactivate\' ) );
                if(get_option(\'cpt_enable\')){
                   add_action( \'init\', array($this, \'cpt\' ) ); 
                } //get the \'cpt enabled option if available.Hit the hook\'
            }

            public function mbp_activate(){
                require_once MY_PLUGIN_PATH .\'inc/yes.php\';
                MBPActivate::activate();
            }

            public function mbp_deactivate(){
                require_once MY_PLUGIN_PATH .\'inc/yes.php\';
                MBPActivate::deactivate();
            }

            public function cpt(){
                $args = array(
                                \'public\'    => true,
                                \'label\'     => __( \'Books\', \'textdomain\' ),
                                \'menu_icon\' => \'dashicons-book\',
                            );
                register_post_type( \'book\', $args );
            }

        }

        $pluginInstance = new MyBestPlugin();

}
/////下一页是。php////

class MBPActivate {
        public function activate(){
            add_option(\'cpt_enable\',\'cpt_enable\'); // set the cpt enabled option after activation hook.
            flush_rewrite_rules();
        }
        public function deactivate(){
            delete_option(\'cpt_enable\');
            flush_rewrite_rules();
        }
    }
    $plugin = new MBPActivate();

SO网友:Chetan Vaghela

请尝试以下代码

my-plugin.php file

<?php
/**
Plugin Name: My Plugin
Plugin URI: http://#####
Description: This plugin just for test
Author: Chetan Vaghela
Version: 1.0.0
Author URI: http://che##la.###/
*/
if ( ! defined(\'ABSPATH\') ){
        die;
}
if(!defined(\'MY_PLUGIN_PATH\')) { 
    define( \'MY_PLUGIN_PATH\', plugin_dir_path(__FILE__) ); 
}

function mbp_activate(){
  flush_rewrite_rules();
  add_option( \'my_plugin_activated\', time() );
}
require_once MY_PLUGIN_PATH .\'inc/mbp-plugin-activate.php\';
function run_mbpactive() {

    $plugin = new MBPActivate();

}
run_mbpactive();

register_activation_hook( __FILE__ , \'mbp_activate\'  );


register_uninstall_hook( __FILE__, \'my_plugin_uninstall\' );
function my_plugin_uninstall() {
     unregister_post_type( \'book\' );
     delete_option("my_plugin_activated");
}

inc/mbp-plugin-active.php file

<?php
class MBPActivate {

     public function __construct() {

        add_action( \'init\', array($this, \'cptcallback\' ) );

    }

    public function cptcallback(){

       // / die("calledddd");
        $books_labels = array(
            \'name\'               => _x( \'Books\', \'Books name\', \'text-domain\' ),
            \'singular_name\'      => _x( \'Book\', \'Books name\', \'text-domain\' ),
            \'menu_name\'          => _x( \'Books\', \'admin menu\', \'text-domain\' ),
            \'name_admin_bar\'     => _x( \'Books\', \'add new on admin bar\', \'text-domain\' ),
            \'add_new\'            => _x( \'Add New\', \'Books\', \'text-domain\' ),
            \'add_new_item\'       => __( \'Add New Books\', \'text-domain\' ),
            \'new_item\'           => __( \'New Books\', \'text-domain\' ),
            \'edit_item\'          => __( \'Edit Books\', \'text-domain\' ),
            \'view_item\'          => __( \'View Books\', \'text-domain\' ),
            \'all_items\'          => __( \'All Books\', \'text-domain\' ),
            \'search_items\'       => __( \'Search Books\', \'text-domain\' ),
            \'parent_item_colon\'  => __( \'Parent Books:\', \'text-domain\' ),
            \'not_found\'          => __( \'No Books found.\', \'text-domain\' ),
            \'not_found_in_trash\' => __( \'No Books found in Trash.\', \'text-domain\' )
        );

        $books_args = array(
            \'labels\'             => $books_labels,
            \'description\'        => __( \'Description.\', \'text-domain\' ),
            \'public\'             => true,
            \'publicly_queryable\' => true,
            \'show_ui\'            => true,
            \'show_in_menu\'       => true,
            \'query_var\'          => true,
            \'rewrite\'            => array( \'slug\' => \'book\' ),
            \'capability_type\'    => \'post\',
            \'has_archive\'        => true,
            \'hierarchical\'       => false,
            \'menu_position\'      => null,
            \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ),
            \'menu_icon\'          => \'dashicons-testimonial\'
        );
        register_post_type( \'book\', $books_args );

    }
}
让我知道它是否适合你!

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。