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