Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Customizer API Custom WordPress Customizer Settings Making the Logo Uploader Work with postMessage

Ulises Calvo
Ulises Calvo
2,233 Points

Do I have to repeat the JS postMessage script for every setting even if I have multiple equal settings in the customizer

I have a section with 8 settings that use the upload image control. I intend to use the postMessage transport. My question is, do I have to repeat the same JS script for each of those settings? or is there a way to like create a function ( to ) that applies to all of the settings and can be called for each one without all that amount of code?

I tried to do something like this:

wp.customize( 'team_member_one', function( value ) { value.bind( ( to ) ); });

wp.customize( 'team_member_two', function( value ) {
    value.bind( ( to ) );
});

wp.customize( 'team_member_three', function( value ) {
    value.bind( ( to ) );
});

function( to ) {
    $("[data-img]").each function() {
        if ($("[data-img]").data() < 9){
            if( to == '' ) {
                $("[data-img]").hide();
            } else {
                $("[data-img]").show();
                $("[data-img]").attr( 'src', to );
            }   
        }
    }
}

assigning a data-img="number" attribute to each html <img> Any ideas? or should I just go ahead and reiterate what is presented in the video multiple times. Thank you.

Ulises Calvo
Ulises Calvo
2,233 Points

Note: obviously the code above did not work, I am a JS beginer.