admin管理员组

文章数量:1202779

I'm creating a plugin and I have to add some custom controls through the plugin. Here is the code for the custom control defined in featured-types.php -

 function itlst_featured_types_customize_register( $wp_customize ) {

//Featured Property Types
$wp_customize->add_section(
    'itlst_feat_types', array(
        'title'     =>  __('Feautured Types', 'it-listings'),
        'panel' =>  'itlst_property'
    )
);

$wp_customize->add_setting(
    'itlst_feat_prop', array(
        'default'           =>  '',
        'sanitize_callback' =>  'itlst_sanitize_type'
    )
);

$wp_customize->add_control(
    new itlst_WP_Customize_Property_Control(
        $wp_customize, 'itlst_feat_prop', array(
            'label'     =>  __('Property Type', 'text-domain'),
            'section'   =>  'itlst_feat_types',
            'settings'  =>  'itlst_feat_prop'
        )
    )
);

}
 add_action('customize_register', 'itlst_featured_types_customize_register');

The class itlst_WP_Customize_Property_Control is defined in custom-controls.php -

if ( class_exists('WP_Customize_Control') ) {

 class itlst_WP_Customize_Property_Control extends WP_Customize_Control {

    /**
     * Render the control's content.
     */
    public function render_content() {
        $dropdown = wp_dropdown_categories(
            array(
                'name'              => '_customize-dropdown-categories-' . $this->id,
                'echo'              => 0,
                'show_option_none'  => __( '— Select —', 'it-listings' ),
                'taxonomy'          => 'property-type',
                'option_none_value' => '0',
                'selected'          => $this->value(),
            )
        );

        $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );

        printf(
            '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
            esc_html($this->label),
            $dropdown
        );
    }
  }
}

Both these files are included in the main plugin file. Here is the code -

require_once(ITL_PATH . 'listings-cpt/customizer/custom-controls.php');
require_once(ITL_PATH . 'listings-cpt/customizer/featured-types.php');

But it gives the "class not found" error. Take a look at the screenshot below -

If I comment out the code for custom control, website loads smoothly.

I have used the same code in WordPress Themes multiple times and it worked smoothly everytime. It's causing issues in the plugin.

Any idea what's going on? I searched through internet but was not able to figure out the exact issue?

My best guess is that the 'customize_register' action hook is being called earlier than the class.

I'm creating a plugin and I have to add some custom controls through the plugin. Here is the code for the custom control defined in featured-types.php -

 function itlst_featured_types_customize_register( $wp_customize ) {

//Featured Property Types
$wp_customize->add_section(
    'itlst_feat_types', array(
        'title'     =>  __('Feautured Types', 'it-listings'),
        'panel' =>  'itlst_property'
    )
);

$wp_customize->add_setting(
    'itlst_feat_prop', array(
        'default'           =>  '',
        'sanitize_callback' =>  'itlst_sanitize_type'
    )
);

$wp_customize->add_control(
    new itlst_WP_Customize_Property_Control(
        $wp_customize, 'itlst_feat_prop', array(
            'label'     =>  __('Property Type', 'text-domain'),
            'section'   =>  'itlst_feat_types',
            'settings'  =>  'itlst_feat_prop'
        )
    )
);

}
 add_action('customize_register', 'itlst_featured_types_customize_register');

The class itlst_WP_Customize_Property_Control is defined in custom-controls.php -

if ( class_exists('WP_Customize_Control') ) {

 class itlst_WP_Customize_Property_Control extends WP_Customize_Control {

    /**
     * Render the control's content.
     */
    public function render_content() {
        $dropdown = wp_dropdown_categories(
            array(
                'name'              => '_customize-dropdown-categories-' . $this->id,
                'echo'              => 0,
                'show_option_none'  => __( '&mdash; Select &mdash;', 'it-listings' ),
                'taxonomy'          => 'property-type',
                'option_none_value' => '0',
                'selected'          => $this->value(),
            )
        );

        $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );

        printf(
            '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
            esc_html($this->label),
            $dropdown
        );
    }
  }
}

Both these files are included in the main plugin file. Here is the code -

require_once(ITL_PATH . 'listings-cpt/customizer/custom-controls.php');
require_once(ITL_PATH . 'listings-cpt/customizer/featured-types.php');

But it gives the "class not found" error. Take a look at the screenshot below -

If I comment out the code for custom control, website loads smoothly.

I have used the same code in WordPress Themes multiple times and it worked smoothly everytime. It's causing issues in the plugin.

Any idea what's going on? I searched through internet but was not able to figure out the exact issue?

My best guess is that the 'customize_register' action hook is being called earlier than the class.

Share Improve this question asked Mar 25, 2022 at 11:39 Divjot SinghDivjot Singh 13612 bronze badges 4
  • The error says you are calling require_once in the file listings-cpt/customizer/featured-types.php. Did you check that file? – fuxia Commented Mar 25, 2022 at 12:19
  • Yes, it is the file where the code for custom control is. – Divjot Singh Commented Mar 25, 2022 at 12:22
  • So you include the file in the file itself? – fuxia Commented Mar 25, 2022 at 13:30
  • No, all files are included in the main plugin file. – Divjot Singh Commented Mar 25, 2022 at 14:52
Add a comment  | 

1 Answer 1

Reset to default 0

The issue was resolved. I had to hook the custom classes to the customize_register action hook. Something like this -

function itlst_extend_customize_control() {    
if ( class_exists('WP_Customize_Control') ) {

class itlst_WP_Customize_Property_Control extends WP_Customize_Control {

/**
 * Render the control's content.
 */
public function render_content() {
    $dropdown = wp_dropdown_categories(
        array(
            'name'              => '_customize-dropdown-categories-' . $this->id,
            'echo'              => 0,
            'show_option_none'  => __( '&mdash; Select &mdash;', 'it-listings' ),
            'taxonomy'          => 'property-type',
            'option_none_value' => '0',
            'selected'          => $this->value(),
        )
    );

    $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );

    printf(
        '<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
        esc_html($this->label),
        $dropdown
    );
}
}
}
add_action('customize_register', 'itlst_extend_customize_control');

This is peculiar as in themes, nothing of this sort has to be done. Seems like classes are included after the customize_register hook in plugins.

本文标签: theme developmentrequireonce not working in plugin wordpress