WP_INSERT_TERM不适用于具有自定义分类的单独文件

时间:2021-11-18 作者:Megos

我有在主插件文件中运行良好的代码:

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”。它应该只在插件激活时创建。是否可以从其他文件中向自定义分类法插入术语?

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

register_activation_hook 需要主插件文件作为其第一个参数,并且__FILE__ 这是一个简单的方法。

但通过将此代码移动到子文件中,您已经更改了__FILE__ 并且它不再匹配你的插件主文件(includes/class-id-crm-contacts-user.php 不是具有激活的插件头的插件的主文件)。

相反,您必须将该值作为参数传递。这将很困难,因为您选择在同一个文件中定义、使用和创建对象。相反,如果您将对象的创建从文件中移出,您将能够通过构造函数将所需的值传递给它。

相关推荐