0

I have code that works good in main plugin file:

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();
 }

But if I move it to separate file it doesn't work. I try to fire it from main file like this:

if ( ! class_exists( 'Id_CRM_Contacts_User_Cpt' ) ) {
 require IDCRMCONTACTS_PATH . 'includes/class-id-crm-contacts-user.php';
}

I don't want to use 'default_term' because it creates every time then user change it. And don't want to use 'init' the same. It should creates at plugin activation only. Is it possible to insert term to custom taxonomy from other file?

3
  • what is IDCRMCONTACTS_PATH? __FILE__ must be the main plugin file, if you move your class then you need to provide it with that value or you'll get the wrong value for __FILE__
    – Tom J Nowell
    Nov 18, 2021 at 14:52
  • IDCRMCONTACTS_PATH - url to file with class. If I use require plugin_dir_path( __FILE__ ) . 'includes/class-id-crm-contacts-user.php'; it's still doesn't work.
    – Megos
    Nov 18, 2021 at 15:01
  • you can't use __FILE__ in register_activation_hook if it's not the main plugin file.
    – Tom J Nowell
    Nov 18, 2021 at 15:13

1 Answer 1

0

register_activation_hook requires the main plugin file as its first parameter, and __FILE__ is an easy way to get this.

But by moving this code into a sub-file you've changed the value of __FILE__ and it no longer matches your plugin main file ( includes/class-id-crm-contacts-user.php is not the main file of your plugin with the plugin header that got activated ).

Instead, you must pass the value as a parameter. This will be difficult as you have chosen to both define, use, and create objects in the same file. Instead if you move the creation of the object back out of the file, you'll be able to pass it the needed value via the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.