admin管理员组文章数量:1415069
I wrote a WP theme using namespaces and autoload. Everything went smooth until I started to use WP classes which I understand of course because I understand how namespacing works.
My question is, is there a way possible to use a Wordpress class inside one of my theme classes, for example I want to use $wp_customize
, an instance of WP_Customize_Manager
? How would I achieve that, keeping my OOP theme structure.
UPDATE:
Thanks to Pat J. I was able to solve it. It works perfectly and this is how it looks like:
class ColorController extends BaseController
{
public function register()
{
add_action('customize_register', array($this, 'theme_customize_register'));
}
public function theme_customize_register($wp_customize)
{
// Text color
$wp_customize->add_setting('text_color', array(
'default' => '',
'transport' => 'refresh',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'text_color', array(
'section' => 'colors',
'label' => esc_html__('Text color', 'theme'),
)));
// Link color
$wp_customize->add_setting('link_color', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'link_color', array(
'section' => 'colors',
'label' => esc_html__('Link color', 'theme'),
)));
// Accent color
$wp_customize->add_setting('accent_color', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'accent_color', array(
'section' => 'colors',
'label' => esc_html__('Accent color', 'theme'),
)));
// Border color
$wp_customize->add_setting('border_color', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'border_color', array(
'section' => 'colors',
'label' => esc_html__('Border color', 'theme'),
)));
// Sidebar background
$wp_customize->add_setting('sidebar_background', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'sidebar_background', array(
'section' => 'colors',
'label' => esc_html__('Sidebar Background', 'theme'),
)));
}
}
I wrote a WP theme using namespaces and autoload. Everything went smooth until I started to use WP classes which I understand of course because I understand how namespacing works.
My question is, is there a way possible to use a Wordpress class inside one of my theme classes, for example I want to use $wp_customize
, an instance of WP_Customize_Manager
? How would I achieve that, keeping my OOP theme structure.
UPDATE:
Thanks to Pat J. I was able to solve it. It works perfectly and this is how it looks like:
class ColorController extends BaseController
{
public function register()
{
add_action('customize_register', array($this, 'theme_customize_register'));
}
public function theme_customize_register($wp_customize)
{
// Text color
$wp_customize->add_setting('text_color', array(
'default' => '',
'transport' => 'refresh',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'text_color', array(
'section' => 'colors',
'label' => esc_html__('Text color', 'theme'),
)));
// Link color
$wp_customize->add_setting('link_color', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'link_color', array(
'section' => 'colors',
'label' => esc_html__('Link color', 'theme'),
)));
// Accent color
$wp_customize->add_setting('accent_color', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'accent_color', array(
'section' => 'colors',
'label' => esc_html__('Accent color', 'theme'),
)));
// Border color
$wp_customize->add_setting('border_color', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'border_color', array(
'section' => 'colors',
'label' => esc_html__('Border color', 'theme'),
)));
// Sidebar background
$wp_customize->add_setting('sidebar_background', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new \WP_Customize_Color_Control($wp_customize, 'sidebar_background', array(
'section' => 'colors',
'label' => esc_html__('Sidebar Background', 'theme'),
)));
}
}
Share
Improve this question
edited Aug 19, 2019 at 17:48
Amir Rami
asked Aug 18, 2019 at 14:27
Amir RamiAmir Rami
1234 bronze badges
1 Answer
Reset to default 0You should be able to do something like this:
$wp_customize = new \WP_Customize_Manager();
...since, as I understand it, WordPress core puts all its classes, functions, etc. in the global namespace.
Edit
It appears that $wp_customize
may be a bad example, as it's a global variable (so, in your theme, all you should need to do is declare global $wp_customize;
before you use it).
If you're looking to learn about how to use the Customize API, I'd recommend you read through the official Customize API documentation. Note that, anywhere you need to call a WordPress class or function, you can do so by prepending a \
to its name.
本文标签: phpHow to call a WP Class inside my theme
版权声明:本文标题:php - How to call a WP Class inside my theme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745232006a2648865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论