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.