Home › Forums › Front-end Issues › ACF Frontend Form: add new post multilingual (WPML) › Reply To: ACF Frontend Form: add new post multilingual (WPML)
Solution:
[code]
function duplicate_post_to_secondary_language( $post_id, $mark_as_duplicate = false ) {
// Retrieve the post's element type, trid, and existing translations
$element_type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );
$trid = apply_filters( 'wpml_element_trid', null, $post_id, $element_type );
$translations = (array) apply_filters( 'wpml_get_element_translations', [], $trid, $element_type );
if ( ! $translations ) {
// Handle error or invalid values accordingly
return;
}
// Retrieve the active languages
$wpml_languages = (array) apply_filters('wpml_active_languages', [], 'orderby=id&order=desc');
foreach ( $wpml_languages as $language ) {
// Duplicate the post if no translation exists for the given language
if ( ! isset( $translations[ $language['language_code'] ] ) ) {
apply_filters(
'wpml_copy_post_to_language',
$post_id,
$language['language_code'],
$mark_as_duplicate
);
}
}
}
add_action('acf/save_post', 'duplicate_post_to_secondary_language', 20);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.