• I’ve added a setting in the Customizer which is (supposed) to allow users to add css class names to the body tag.

    So then in another function I want to add a filter of the body_class hook to add the classnames entered.

    The following doesn’t seem to work:

    function custom_body_class_filter($classse)
    {
        $classes[] = get_option('custom_body_classes);
    
        return $classes;
    }
    add_filter('body_classes', 'custom_body_classes');

    Do I need to call the options globally within any other function? Such as:

    global $wp_options; // or $wp_cusomize or something else?

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi Luke!

    How are you adding the setting?

    Not sure if your meant to use classe as the variable you are passing to the filter or not but that may be a possible issue.

    One way I can think of would be using something like:

    add_action( 'customize_register', 'jc_extra_classes' );
    function jc_extra_classes( $wp_customize ){
    	$wp_customize->add_setting( 'jc_classes',
    		array(
    			'default' => 'blue',
    			'type' => 'theme_mod'
    		) );
    	$wp_customize->add_control( 'jc_classes_ctrl',
    			array(
    			'settings' => 'jc_classes',
    			'type' => 'text',
    			'label' => __( 'Add additional classes, separated by spaces','text-domain' ),
    			'section' => 'colors'
    			) );
    }
    
    add_filter( 'body_class', 'jc_bodyclasses' );
    function jc_bodyclasses( $classes ){
    	$extra = join( ' ' , (array)get_theme_mod( 'jc_classes', '' ) );
    
    	$classes = array_merge( $classes, (array)$extra );
    
    	return $classes;
    
    }

    Hope that helps!

    Thread Starter Luke Watts

    (@lukequietus)

    Hi Jose,

    Thanks for the reply.

    I’ve since broken the entire functionality out into a plugin with it’s own options page.

    However, I’m going to give this a go because I don’t want users to have to leave the front of the website to make design changes.

    That’s the whole point of the customizer.

    Also, because this functionality isn’t only mean to work with one theme it should have been a plugin anyways…but that’s another topic. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can I use "options" or "theme_mod" values from WP_Customizer in other functions?’ is closed to new replies.