1

I've got a bunch of customizer settings set up

Rather than calling individual get_theme_mod statements for each setting individually, is there a way to simplify it to loop thru them without having to explicitly write each one individually?

The complete file is also setup on Gist for review

<?php


/** Adds the Customize page to the WordPress admin area */
function litho_customizer_menu() {
    add_theme_page( 'Customize', 'Customize', 'edit_theme_options', 'customize.php' );
}
add_action( 'admin_menu', 'litho_customizer_menu' );

/** Adds the individual sections, settings, and controls to the theme customizer */
function litho_customizer( $wp_customize ) {
    class Example_Customize_Textarea_Control extends WP_Customize_Control {
        public $type = 'textarea';

        public function render_content() { ?>
            <label>
                <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
                <textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
            </label>
        <?php }
    }

    $wp_customize->add_section( 'site_settings', array(
            'title' => 'Framework Settings',
            'description' => 'Adjust settings',
            'priority' => 35,
    ));


/* <?php echo $sidebar_position; ?> */



    $wp_customize->add_setting( 'site-subtitle', array( 'default' => 'Default subtitle text','sanitize_callback' => 'litho_sanitize_text', ) );
    $wp_customize->add_control('site-subtitle', array(
            'label' => 'Copyright text',
            'section' => 'site_settings',
            'type' => 'text'
    ));

    $wp_customize->add_setting( 'litho_copyright_textbox',array( 'default' => 'Default copyright text','sanitize_callback' => 'litho_sanitize_text', ) );
    $wp_customize->add_control( 'litho_copyright_textbox',array(
            'label' => 'Copyright text',
            'section' => 'site_settings',
            'type' => 'text'
    ));

        $wp_customize->add_setting( 'litho_hide_copyright',array('sanitize_callback' => 'litho_sanitize_checkbox',) );
        $wp_customize->add_control( 'litho_hide_copyright',array(
                'type' => 'checkbox',
                'label' => 'Hide copyright text',
                'section' => 'site_settings'
        ));

        $wp_customize->add_setting( 'litho_logo_placement', array( 'default' => 'left','sanitize_callback' => 'litho_sanitize_logo_placement', ) );
        $wp_customize->add_control( 'litho_logo_placement',array(
                'type' => 'radio',
                'label' => 'Logo placement',
                'section' => 'site_settings',
                'choices' => array( 'left' => 'Left','right' => 'Right','center' => 'Center')
        ));

    $wp_customize->add_setting( 'litho_powered_by', array( 'default' => 'wordpress','sanitize_callback' => 'litho_sanitize_powered_by', ) );
    $wp_customize->add_control( 'litho_powered_by',array(
            'type' => 'select',
            'label' => 'This site is powered by:',
            'section' => 'site_settings',
            'choices' => array( 'wordpress' => 'WordPress','nuclear-energy' => 'Nuclear Energy')
    ));

    $wp_customize->add_setting( 'page-setting', array( 'sanitize_callback' => 'litho_sanitize_integer', ) );
    $wp_customize->add_control( 'page-setting',array(
            'type' => 'dropdown-pages',
            'label' => 'Choose a page:',
            'section' => 'site_settings'
    ));

    $wp_customize->add_setting( 'color-setting', array( 'default' => '#000000','sanitize_callback' => 'sanitize_hex_color', ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'color-setting',array(
            'label' => 'Color Setting',
            'section' => 'site_settings',
            'settings' => 'color-setting'
    )));

    $wp_customize->add_setting( 'file-upload' );
    $wp_customize->add_control( new WP_Customize_Upload_Control( $wp_customize, 'file-upload',array(
            'label' => 'File Upload',
            'section' => 'site_settings',
            'settings' => 'file-upload'
    )));

    $wp_customize->add_setting( 'img-upload' );
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'img-upload',array(
            'label' => 'Image Upload',
            'section' => 'site_settings',
            'settings' => 'img-upload'
    )));

    $wp_customize->add_setting( 'textarea' );
    $wp_customize->add_control( new Example_Customize_Textarea_Control( $wp_customize, 'textarea',array(
            'label' => 'Textarea',
            'section' => 'site_settings',
            'settings' => 'textarea'
    )));

    $wp_customize->add_setting( 'font-color', array( 'default' => '#444444', 'sanitize_callback' => 'sanitize_hex_color', ));
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'font-color',array(
            'label' => 'Font Color',
            'section' => 'colors',
            'settings' => 'font-color'
    )));

    $wp_customize->add_setting( 'featured-background',array( 'default' => '#ffffff','sanitize_callback' => 'sanitize_hex_color','transport' => 'postMessage', ));
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize,'featured-background',array(
            'label' => 'Featured Background',
            'section' => 'colors',
            'settings' => 'featured-background'
    )));

    /* AJAX function for live preview */
    if ( $wp_customize->is_preview() && ! is_admin() ) {
        add_action( 'wp_footer', 'litho_customize_preview', 21);
    }

    function litho_customize_preview() {
        wp_enqueue_script( 'litho-customize-js', get_template_directory_uri().'/core/js/customize.js',array());
    }
    add_action( 'wp_enqueue_scripts', 'litho_customize_preview' );

    $wp_customize->get_setting('blogname')->transport='postMessage';
}
add_action( 'customize_register', 'litho_customizer' );

function litho_header_alignment() {
    $litho_position = get_theme_mod( 'litho_logo_placement' );
    if( $litho_position != '' ) {
        switch ( $litho_position ) {
            case 'left':
                break;
            case 'right':
                echo '<style type="text/css">';
                echo '#header #logo{ float: right; }';
                echo '</style>';
                break;
            case 'center':
                echo '<style type="text/css">';
                echo '#header{ text-align: center; }';
                echo '#header #logo{ float: none; margin-left: auto; margin-right: auto; }';
                echo '</style>';
                break;
        }
    }
}
add_action ( 'wp_head','litho_header_alignment' );

/* The force_balance_tags() function ensures that no tags are left unclosed, while the wp_kses_post() ensures that only safe tags make it into the database (the same tags that are allowed in a standard WordPress post */
function litho_sanitize_text( $input ) {
    return wp_kses_post( force_balance_tags( $input ) );
}
function litho_sanitize_checkbox( $input ) {
    if ( $input == 1 ) {
        return 1;
    } else {
        return '';
    }
}
function litho_sanitize_logo_placement( $input ) {
    $valid = array(
        'left' => 'Left',
        'right' => 'Right',
        'center' => 'Center',
    );

    if ( array_key_exists( $input, $valid ) ) {
        return $input;
    } else {
        return '';
    }
}

function litho_sanitize_powered_by( $input ) {
    $valid = array(
        'wordpress' => 'WordPress',
        'nuclear-energy' => 'Nuclear Energy',
    );

    if ( array_key_exists( $input, $valid ) ) {
        return $input;
    } else {
        return '';
    }
}
function litho_sanitize_integer( $input ) {
    if( is_numeric( $input ) ) {
        return intval( $input );
    }
}

1 Answer 1

1

There is get_them_mods() which will...

Retrieve all theme modification values for the current theme.

If no theme mods have been set, will return boolean false.

https://codex.wordpress.org/Function_Reference/get_theme_mods

It should return an array that you can loop over.

3
  • Can you provide an example or does the example on the codex pretty much work straight outa the box? I'm just pretty new at WP. Thx Commented Apr 30, 2014 at 19:28
  • get_theme_mods() is pretty straightforward. I can post an example if you have trouble.
    – s_ha_dum
    Commented Apr 30, 2014 at 19:33
  • As of now, get_theme_mods() does not work with customizer. Customizer uses the filter of get_theme_mod() to change the values, temporarily. There's no such filter in get_theme_mods()
    – Jashwant
    Commented Jan 17, 2015 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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