请尝试以下代码
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 );
}
}
让我知道它是否适合你!