1

I am using wordpress theme customizer to change color scheme of my theme but was wondering if there is any shorter way to achieve this from what i have below. Thank you.

wp.customize('orn_color_scheme',function( value ) {
    value.bind(function(to) {
    var $color;
    if($('body').hasClass('default')) { $color = 'default' }
    if($('body').hasClass('beige')) { $color = 'beige' }
    if($('body').hasClass('blue')) { $color = 'blue' }
    if($('body').hasClass('celadon')) { $color = 'celadon' }
    if($('body').hasClass('cherry')) { $color = 'cherry' }
    if($('body').hasClass('cyan')) { $color = 'cyan' }
    if($('body').hasClass('dark')) { $color = 'dark' }
    if($('body').hasClass('dirty-green')) { $color = 'green' }
    if($('body').hasClass('orchid')) { $color = 'orchid' }
    if($('body').hasClass('red')) { $color = 'red' }

    $('#orbitnews-color-scheme-css').attr('href', '<?php echo get_template_directory_uri(); ?>/layouts/colors/' + to + '.css');
    $('body').removeClass($color).addClass(to);
        });
    });
9
  • 1
    Why prefix your variable names with a $ ? This is not perl or php. Commented Nov 20, 2013 at 14:56
  • 1
    @ArlaudAgbePierre It's valid javascript. $ is often placed at the beginning of variables to signify the variable contains a jQuery collection Commented Nov 20, 2013 at 14:57
  • 1
    @oGeez But in this case, the variable is a simple string. The variable name is valid, but misleading for other developers. Commented Nov 20, 2013 at 14:58
  • 2
    @ArlaudAgbePierre Instead of saying +1, you can just upvote the comment. Commented Nov 20, 2013 at 14:59
  • 1
    This happens when some developers use jQuery before leaning basics of JavaScript. Commented Nov 20, 2013 at 15:00

3 Answers 3

1
var colors = ['default', 
              'beige',
              'blue',
              'celadon',
              'cherry'
              ... etc
             ];

wp.customize('orn_color_scheme',function( value ) {
    value.bind(function(to) {
        var $color;
        $.each(colors, function(_, color)) {
            if ( $('body').hasClasss(color) ) {
                $color = color;
                break;
            }
        });
        $('#orbitnews-color-scheme-css').prop('href', '<?php echo get_template_directory_uri(); ?>/layouts/colors/' + to + '.css');
        $('body').removeClass($color).addClass(to);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try like

$('body').toggleClass( color , to);

So it will be

wp.customize('orn_color_scheme',function( value ) {
     value.bind(function(to) {
         var color;
         color = $('body').attr('class');
         $('#orbitnews-color-scheme-css').attr('href', '<?php echo get_template_directory_uri(); ?>/layouts/colors/' + to + '.css');
         $('body').toggleClass( color , to);
     });
});

Comments

0

Maybe you can make use of data attribute to make the code shorter and adjustable:

HTML:

<body class="someclass1 someclass2 red" data-color="red"></body>

Javascript:

function(to) {
    var $color = $('body').data().color;

    $('#orbitnews-color-scheme-css').attr('href', '<?php echo get_template_directory_uri(); ?>/layouts/colors/' + to + '.css');

    $('body').removeClass($color).addClass(to).data("color",to);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.