1

I have a contact form 7 on the front-end which creates a new custom post everytime it's filled. Everything is working fine but I don't understand how to set the language of this post (currently it's saved in English as it's my default language in WPML).

Here is what I've tried:

function save_form_to_post( $posted_data ) {

    $args = array(
        'post_type' => 'projects',
        'post_status'=> 'draft',
        'post_title'=> wp_strip_all_tags( $posted_data['title'] ),
        'post_content'=> wp_strip_all_tags( $posted_data['pitch'] ),
    );
    $post_id = wp_insert_post($args);

    if( $posted_data['language'] == 'Spanish' || $posted_data['language'] == 'German' || $posted_data['language'] == 'French' ) {
        if( $posted_data['language'] == 'Spanish' ) {
            $set_language_args = array(
                'element_id' => $post_id,
                'element_type'  => 'projects',
                'language_code' => 'es'
            );
            $cat_ID = 729;
        } elseif( $posted_data['language'] == 'German' ) {
            $set_language_args = array(
                'element_id' => $post_id,
                'element_type'  => 'projects',
                'language_code' => 'de'
            );
            $cat_id = 726;
        } elseif( $posted_data['language'] == 'French' ) {
            $set_language_args = array(
                'element_id' => $post_id,
                'element_type'  => 'projects',
                'language_code' => 'fr'
            );
            $cat_id = 725;
        }
        do_action( 'wpml_set_element_language_details', $set_language_args );
    } else {
        $cat_id = 723;
    }
    wp_set_object_terms( $post_id, $cat_id, 'project_category' );

    return $posted_data;
}
add_filter( 'wpcf7_posted_data', 'sep_save_screenplay_form' );

So the language is set depending on the value of a select field on the front-end form. Then I've tried to used the wpml_set_element_language_details hook to define a language for the post (each post has no translation). And finally I set a taxonomy for the post.

Right now, the post only saves in English and not in other languages. Could someone explain to me why it's not working and how to make it work?

Thanks a lot.

  • You have a problem with the element_type its should be post_projects you can use the filter $element_type = apply_filters('wpml_element_type', 'projects'); to get the correct element type. – Shibi Jan 19 '18 at 16:20
  • Would you mind posting this as an answer here so others can more easily see it and upvote it? That's how the site works :-) – swissspidy Jan 19 '18 at 17:36
  • I was waiting for @Shibi to post it as an answer as he is the one who have found the solution. If he doesn't I'll post the answer myself. – Pipo Jan 19 '18 at 18:14
1

The wpml_set_element_language_details action element_type need to be with the correct prefix that WPML use:

From the WPML Docs

element_type(string) The type of an element. Can be a post type: post_post, post_page, post_attachment, post_nav_menu_item, post_{custom post key} or taxonomy: tax_category, tax_post_tag, tax_nav_menu, tax_{custom taxonomy key}. Defaults to post_post if not set.

In your case it should be post_projects but you can also use wpml_element_type to get the correct prefix like this:

$element_type = apply_filters('wpml_element_type', 'projects');

Resources:

https://wpml.org/wpml-hook/wpml_set_element_language_details/

https://wpml.org/wpml-hook/wpml_element_type/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.