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' => __( '— 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.
1 Answer
Reset to default 0The 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' => __( '— 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
);
}
}
}
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
版权声明:本文标题:theme development - require_once not working in plugin wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738657160a2105206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
require_once
in the filelistings-cpt/customizer/featured-types.php
. Did you check that file? – fuxia ♦ Commented Mar 25, 2022 at 12:19