I’m looking for a way to add a new kind of control to the customize live preview panel. I have seen how to add new sections to the panel using
add_action( 'customize_register'...
The control I want to implement is a different kind of color picker. In a previous post, we see how to extend core classes to add widgets, but what I lack here is a hook that will enable me to bring my object into scope – WP_Customize_Palette_Control. At
You can see the beginnings of the code here. This code is in the functions.php
file of my theme.
Thanks for any help.
Rob
Just updated the code. Now I have require_once
to bring in the classes. So now I have no PHP errors but my new control HTML does not appear.
<?php
require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
class WP_Customize_Palette_Control extends WP_Customize_Image_Control {
public $type = 'palette';
public $removed = '';
public $context;
public function enqueue() {
//wp_enqueue_script( 'wp-plupload' );
}
public function to_json() {
parent::to_json();
$this->json['removed'] = $this->removed;
if ( $this->context )
$this->json['context'] = $this->context;
}
public function render_content() {
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<div>
<a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
<a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
</div>
</label>
<?php
}
}
//new WP_Customize_Palette_Control();
//add_action('customize_controls_init', 'WP_Customize_Palette_Control');
// add an option to the customize panel
function sci_customize_controls_init($wp_customize) {
$wp_customize->add_section( 'themename_color_scheme', array(
'title' => __( 'Color Scheme', 'themename' ),
'priority' => 35,
) );
$wp_customize->add_setting( 'themename_theme_options[color_scheme]', array(
'default' => 'some-default-value',
'type' => 'option',
'capability' => 'edit_theme_options',
) );
$wp_customize->add_control( 'themename_color_scheme', array(
'label' => __( 'Color Scheme', 'themename' ),
'section' => 'themename_color_scheme',
'settings' => 'themename_theme_options[color_scheme]',
'type' => 'palette',
'choices' => array(
'value1' => 'Choice 1',
'value2' => 'Choice 2',
'value3' => 'Choice 3',
),
) );
}
add_action( 'customize_register', 'sci_customize_controls_init' );
Example and class for usage
You can see on my current theme, how it’s possible to use this. Also you can usage the class. See this class on Github an check the
functions.php
for include this.Start & init
You can register your custom settings for the theme customizer via the
customize_register
hook:In-Theme usage:
Use it, like in the example below â:
Adjustments
The you can also change the control:
The default control-type is
text
. It creates a text box control. Another control-type isdropdown-pages
, which creates a dropdown list of the WordPress Pages.But thatâs not all. Thereâre actually several more, but because theyâre so custom, theyâre declared differently.
This one makes use of OOP:
Additional notes:
WP_Customize_Upload_Control
â This gives you an upload box for files. However, you probably wonât use this directly,youâll want to extend it for other things⦠like:
WP_Customize_Image_Control
âThis gives you the image picker and the uploader box. It extends the
upload controller. You can see it in action on the custom background
piece, where a user can upload a new file to be the background image.
WP_Customize_Header_Image_Control
â Because of the resizing action ofthe header piece, it needs a bit of special handling and display, so
the
WP_Customize_Header_Image_Control
extends theWP_Customize_Image_Control
to add that functionality. You can see itin action on the custom header piece, where a user can upload a new
file to be the header image.
You can find more about “Theme Customizer” in ottos blog.
Update 11/06/2012
Live Example for read possibilities and more examples, see the open repo for the source and the doku.
Update 01/15/2013
We have create a repo on github with custom class to use it, easy and ready. Maybe you can only use it or advance with your ideas and solutions.
Ok, here’s how to do this. Seperate your control class(es) to one or more new files.
You have a function or method hooked on customize_register, right? In that function or method require once your new files just before adding your custom controls. Then PHP won’t complain about redefining classes.
Note: This will not work out of the box, but shows the trick.
You’re never using your class. Try passing a new instance of your class to the add_control method:
Also, I don’t think WP knows that the option name for your setting is part of an array. Maybe it’s better to have
themename_theme_options_color_scheme
instead ofthemename_theme_options[color_scheme]
.The class your extending belongs to the image upload control. If you’re creating a color picker, you should probably extend the WP_Customize_Control class.
Just for completeness: An example on how to add a number field to the Theme Customizer.
I think you have to add backslash before the WP_Customize. So, it will be
, Because backslash assumed that the WP_Customize_Image_Control not from same Namespace
Let me know if it helped