skip to Main Content

WPML is used throughout the site for multilingualism.
A new multilingual post (2 languages, de/en) is to be created in a ACF frontend form.

A new post in one language can easily be created with ACF (ACF pro) frontend form.
However, the new post should always be created in both languages (from the frontend) at the same time.
How can I achieve this?

2

Answers


  1. To add a new multilingual post using WPML on the frontend, first, create your post in the default language. Then, switch to the desired language using the WPML language switcher. Finally, translate the content and publish the post. This is the easiest way I found.

    Login or Signup to reply.
  2. You can also give a try by adding the given code to the functions.php file of the theme.

    Here we are using acf/save_post hook for inserting posts.

    <?php
    function custom_acfform_save_post( $post_id ) {
        // Check if this is the ACF form submission
        // here field_32146789bca is the field id.
        if ( isset( $_POST['acf']['field_32146789bca'] ) ) {
            
            $post_data = get_post( $post_id );
            
            // Here we are creating a new post array for the Primary post.
            $new_post = array(
                'post_title'  => $post_data->post_title,
                'post_content'=> $post_data->post_content,
                'post_status' => 'publish',
                'post_type'   => 'post'
            );
    
            // Here we are inserting the new post in German Language.
            $new_post_id = wp_insert_post( $new_post );
    
            if ( ! is_wp_error( $new_post_id ) ) {
                // Here we are setting up the post language to German using WPML.
                do_action('wpml_set_element_language_details', array(
                    'element_id'    => $new_post_id,
                    'element_type'  => 'post_post',
                    'trid'          => apply_filters('wpml_element_trid', null, $post_id, 'post_post'),
                    'language_code' => 'de',
                    'source_language_code' => 'en'
                ));
    
                // Link the two posts together
                global $sitepress;
                $sitepress->set_element_translations(
                    array(
                    'post_post' => array(
                        'en' => $post_id,
                        'de' => $new_post_id
                        )
                    )
                );
            }
        }
    }
    add_action('acf/save_post', 'custom_acfform_save_post', 20);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search