Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save onwp/06a65eed0f717eca9ff632430885637f to your computer and use it in GitHub Desktop.
Save onwp/06a65eed0f717eca9ff632430885637f to your computer and use it in GitHub Desktop.
Check "Translate" option for all custom fields in WPML plugin
// Scroll into Custom Fields settings
document.querySelector(".wpml-custom-fields-settings").scrollIntoView();
// Click on 'Display All' button
jQuery(".wpml-custom-fields-settings .display-all").click();
const set_acf_fields_translatable = setInterval(() => {
// When 'Display All' button is clicked, the 'display-all' class is removed, therefore check until it doesnt exist
if (jQuery(".wpml-custom-fields-settings .display-all").length == "0") {
// Scroll into the 'Save' button
document
.querySelector(".wpml-custom-fields-settings input[type='submit']")
.scrollIntoView();
// Set all custom fields as 'Translatable'
jQuery(".wpml-custom-fields-settings .wpml-flex-table-row").each(
function () {
// Values for radio buttons
// 0: Don't translate
// 1: Copy
// 2: Translate
// 3: Copy once
// Check Translate option for all custom fields
jQuery(this).find(":radio[value=2]").attr("checked", true);
}
);
// Click on 'Save' button
jQuery(".wpml-custom-fields-settings input[type='submit']").click();
// Check if the data is saved, then redirect to back the settings page
jQuery(".wpml-custom-fields-settings .icl_ajx_response").on(
"DOMSubtreeModified",
function () {
if (jQuery(this).text().includes("Data saved")) {
if (!window.location.hash) {
window.location.href = window.location.href;
} else {
window.location.reload();
}
}
}
),
clearInterval(set_acf_fields_translatable);
}
}, 1000);
@onwp
Copy link
Author

onwp commented Jul 5, 2024

You can copy&paste into console to run it once, since you will probably need this only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment