Back to Blog
DevelopmentWordPressWPMLJavaScriptACF

Automatically check WPML custom field checkboxes

Today I faced a pretty daunting task, I needed to check around 2,600 rows of checkboxes across 4 different sites, each that had 3 different options.

2 min read
300 words
Automatically check WPML custom field checkboxes

Today I faced a pretty daunting task, I needed to check around 2,600 rows of checkboxes across 4 different sites, each that had 3 different options.

You might face this yourself within the WordPress WPML ( WordPress Multi Site) custom field / ACF / Advanced custom fields area. What I wanted to do was programmatically send off the correct job translations that I wanted, and not send off the ones that didn't need to be translated.

This handy snippet that I came up with solved just that!

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
jQuery("#icl_cf_translation tbody tr").each(function() {
        var tdVal = jQuery(this).find("td:first").html();

        if(tdVal[0] != '_' &&
        	!endsWith(tdVal,'image') &&
	        !endsWith(tdVal,'background') &&
	        !endsWith(tdVal,'link') &&
	        !endsWith(tdVal,'width') &&
	        !endsWith(tdVal,'filter') &&
	        !endsWith(tdVal,'section') &&
	        !endsWith(tdVal,'shortcode') &&
	        !endsWith(tdVal,'style') &&
	        !endsWith(tdVal,'sizing') &&
	        !endsWith(tdVal,'spacing') &&
	        !endsWith(tdVal,'side') &&
	        !endsWith(tdVal,'size') &&
	        !endsWith(tdVal,'left') &&
	        !endsWith(tdVal,'right') &&
	        !endsWith(tdVal,'hover') &&
	        !endsWith(tdVal,'icon') &&
	        !endsWith(tdVal,'over') &&
	        !endsWith(tdVal,'mp4') &&
	        !endsWith(tdVal,'ogv') &&
	        !endsWith(tdVal,'webm')){
        	//console.log(tdVal);
        	jQuery(this).find(":radio[value=2]").attr('checked',true);
        }
        else{
        	jQuery(this).find(":radio[value=0]").attr('checked',true);
        }
});

You will need to tweak / modify this yourself to fit your specific needs, but this will work perfectly within the Chrome network inspect / developer tools of your choice.

Enjoy!