1

I'm currently working on a WordPress theme. The design + functionality is already done and it's activated. But this is my first day of creating themes for WordPress (hello, new friends) and I'm unsure how to create a customizer option.

I've checked various tutorials and I understand the code they put in there, but I don't understand this:

Where do I put the code? Do I need to create a new file or something? Because I want it to stick with the theme, obviously.

Thanks for helping me out.


I added this in functions.php but nothing really happened to the customizer?

$wp_customize->add_control(
    'copyright_textbox',
    array(
        'label' => 'Copyright text',
        'section' => 'example_section_one',
        'type' => 'text',
    )
);
  • just a tip, had some luck using kirki library, makes customizer easier... – majick May 31 '16 at 14:12
3

The easiest solution is to put it in your functions.php file in your theme's directory. All the code in functions.php is automatically executed when the theme is active.

If the code in the file gets too much you can choose to divide it in smaller files and include those files in functions.php

EDIT

You should change your provided code snippet to the following

function wpse_228333_customize( $wp_customize ) {
    $wp_customize->add_control(
        'copyright_textbox',
        array(
            'label' => 'Copyright text',
            'section' => 'example_section_one', 
            'type' => 'text',
        )
    );
}
add_action( 'customize_register', 'wpse_228333_customize' );

EDIT 2

The problem with the code is that a control won't show up if the attached section doesn't exist. Also there is not a setting created that the control actually controls.

The code snippet below is a fully functional customizer section that adds a text field

function wpse_228333_customize( $wp_customize ) {

    /** First add a setting to be controlled / changed */
    $wp_customize->add_setting( 'wpse_228333_copyright_text' , array(
        'default'     => '',
    ) );

    /** Now add a section to put all your customizer controls in */
    $wp_customize->add_section( 'wpse_228333_section' , array(
        'title'      => __( 'My Custom Section!', 'text-domain' )
    ) );

    /** Now add a control to the previously made section to control the previously added setting */

    $wp_customize->add_control( 'wpse_228333_copyright_text_control', array(
        'label'      => __('Copyright text', 'themename'),
        'section'    => 'wpse_228333_section', // Corresponds to previously made section
        'settings'   => 'wpse_228333_copyright_text', // Corresponds to previously made setting
        'type'       => 'text'
    ));

}
add_action( 'customize_register', 'wpse_228333_customize' );
  • Okay, so I have edited my answer with my code I want to add, but for some reason it's not showing up ? – J. Doe May 31 '16 at 12:42
  • 1
    That is because the $wp_customize variable is not set anywhere. You need to add an action first that hooks into the customizer registration process which then sends the wp_customize instance to the defined function. I've editted my answer to reflect your piece of code. – Jeffrey von Grumbkow May 31 '16 at 12:46
  • I'm not sure if I'm doing anything wrong, but the option isn't showing up in my customizer. I have hard refreshed, signed out & signed in again, and all that didn't really work. Freaky.. – J. Doe May 31 '16 at 12:56
  • You stated that you checked various tutorials so I assumed the tutorials were complete but they apparently are not. I've again updated my answer and tested the code. It also comments on what certain lines of code to and why your code previously wasn't working – Jeffrey von Grumbkow May 31 '16 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.