我有在主插件文件中运行良好的代码:
if ( ! class_exists( \'Id_CRM_Contacts_User_Cpt\' ) ) {
class Id_CRM_Contacts_User_Cpt {
function __construct() {
register_activation_hook( __FILE__, array( $this, \'custom_post_type\' ) );
register_activation_hook( __FILE__, array( $this, \'create_first_contact_status\' ) );
add_action( \'init\', array( $this, \'custom_post_type\' ) );
}
public function custom_post_type() {
$labels = array(
\'name\' => esc_html_x( \'Statuses\', \'taxonomy general name\', \'idcrm_contacts\' ),
\'singular_name\' => esc_html_x( \'Status\', \'taxonomy singular name\', \'idcrm_contacts\' ),
\'search_items\' => esc_html__( \'Search Statuses\', \'idcrm_contacts\' ),
\'all_items\' => esc_html__( \'All Statuses\', \'idcrm_contacts\' ),
\'parent_item\' => esc_html__( \'Parent Status\', \'idcrm_contacts\' ),
\'parent_item_colon\' => esc_html__( \'Parent Status:\', \'idcrm_contacts\' ),
\'edit_item\' => esc_html__( \'Edit Status\', \'idcrm_contacts\' ),
\'update_item\' => esc_html__( \'Update Status\', \'idcrm_contacts\' ),
\'add_new_item\' => esc_html__( \'Add New Status\', \'idcrm_contacts\' ),
\'new_item_name\' => esc_html__( \'New Status Name\', \'idcrm_contacts\' ),
\'menu_name\' => esc_html__( \'Statuses\', \'idcrm_contacts\' ),
);
$args = array(
\'hierarchical\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array(
\'slug\' => \'contacts/status\',
\'feeds\' => false,
\'feed\' => false,
),
\'labels\' => $labels,
\'sort\' => true,
\'capabilities \' => array(
\'manage_terms\' => \'edit_user_status\',
\'edit_terms\' => \'edit_user_status\',
\'delete_terms\' => \'edit_user_status\',
\'assign_terms\' => \'edit_user_status\',
),
);
register_taxonomy( \'user_status\', \'user_contact\', $args );
register_post_type(
\'user_contact\',
array(
\'public\' => true,
\'has_archive\' => true,
\'rewrite\' => array(
\'slug\' => \'contacts\',
\'feeds\' => false,
\'feed\' => false,
),
\'label\' => esc_html__( \'Contacts\', \'idcrm_contacts\' ),
\'supports\' => array( \'title\', \'editor\', \'comments\', \'revisions\', \'author\', \'excerpt\', \'custom-fields\', \'thumbnail\' ),
\'taxonomies\' => array( \'user_status\', \'user_source \' ),
\'show_ui\' => true,
\'show_in_menu\' => \'idcrm-contacts\',
\'capability_type\' => array( \'user_contact\', \'user_contacts\' ),
\'map_meta_cap\' => true,
)
);
}
/** Create first contact status */
public function create_first_contact_status() {
$term_check = term_exists( esc_html__( \'Leads\', \'idcrm_contacts\' ) );
if ( empty( $term_check ) ) {
wp_insert_term( esc_html__( \'Leads\', \'idcrm_contacts\' ), \'user_status\' );
}
}
}
}
if ( class_exists( \'Id_CRM_Contacts_User_Cpt\' ) ) {
$id_crm_contacts_user_cpt = new Id_CRM_Contacts_User_Cpt();
$id_crm_contacts_user_cpt->register();
}
但如果我把它移到单独的文件中,它就不起作用了。我尝试从主文件中这样启动它:
if ( ! class_exists( \'Id_CRM_Contacts_User_Cpt\' ) ) {
require IDCRMCONTACTS_PATH . \'includes/class-id-crm-contacts-user.php\';
}
我不想使用“default\\u term”,因为它会在每次用户更改时创建它。并且不想使用相同的“init”。它应该只在插件激活时创建。是否可以从其他文件中向自定义分类法插入术语?