PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Adding WPML language to post

  1. Lea Cohen
    Member

    I'm using Gravity Forms to enable users to upload posts. Our site is also using WPML as it has to support a few languages.

    We're creating only one form for the post upload, and I was wondering if there is a field that we can use to enable the user to choose the language that the post will belong to, in such a way the WPML will understand and assign to that language.

    Thank you

    Posted 8 years ago on Wednesday November 30, 2011 | Permalink
  2. Lea Cohen
    Member

    I haven't gotten an answer to this yet, and I really need the answer in order to continue my development

    Posted 8 years ago on Wednesday December 7, 2011 | Permalink
  3. I'm not familiar enough with WPML to know how it says what language the post is. There isn't a built in feature in Gravity Forms for you to select the language because it's not core WordPress functionality so it's not something built into the Post Fields.

    If WPML simply stores the language type as a custom field in post meta for that post then it will be as simple as using the Post Custom Field, set the correct custom field name and then configure it as a drop down and set the choices so they use values that are WPML friendly.

    You'll need to find out from WPML where and how this data is stored. If it's as post meta, whats the custom field name/key and what is the value that is stored. Is it's the language code, name, what exactly. You'll need those details to be able to implement this.

    If you can find out from WPML how the language is associated with the post, we can give you some guidance on how to accomplish this using Gravity Forms.

    Posted 8 years ago on Wednesday December 7, 2011 | Permalink
  4. Lea Cohen
    Member

    Thank you, Carl.

    I asked them, and they pointed me to a page that lists the WPML tables.
    Then I continued to search on how to insert into those tables, and found this post, which essentially says that this post parameter has to be filled

    $_POST['icl_post_language']

    And then call wp_insert_post($new_post);
    Can you guide me how to go on from here?

    I already use the gform_post_submission hook in order to add a custom field to the post, and I call wp_update_post at the end of my function, so I thought I'd add that line right before the call to wp_update_post. But I must be doing it wrong, as the language isn't added. Maybe it's too late to add it there? Maybe an UPDATE to that table is due at that stage?

    Posted 8 years ago on Sunday December 11, 2011 | Permalink
  5. I believe you can add it using the gform_post_submission hook. However, if you are using Gravity Forms 1.6 or later, that hook has been replaced by gform_after_submission and you should now use that instead.

    Please post the whole function you are using with gform_post_submission and maybe we can see what's going on. It should work fine there.

    Update:
    It's also possible to use the gform_update_meta function hooked to gform_after_submission.

    Posted 8 years ago on Monday December 12, 2011 | Permalink
  6. Lea Cohen
    Member

    It didn't work for me, and we ended up not doing it.

    Posted 7 years ago on Thursday September 6, 2012 | Permalink
  7. OK. If you want to give it a shot again, please let us know.

    Posted 7 years ago on Thursday September 6, 2012 | Permalink
  8. revonorway
    Member

    Hello,

    I would like to get this working... Has anyone ever tried doing this... I have users with different languages submitting posts from the front end, and would like to add the wpml language info to it...

    Thanks for the help!

    Posted 7 years ago on Wednesday September 19, 2012 | Permalink
  9. We can help you with the Gravity Forms end of it, but we need to know what WPML needs to see, and where, so we now how to help. What information do you need stored with the post to enable WPML?

    Posted 7 years ago on Wednesday September 19, 2012 | Permalink
  10. revonorway
    Member

    I need to get the current language, which is stored in the wpml ICL_LANGUAGE_CODE constant, and then to store it in this post parameter:
    $_POST['icl_post_language']

    Let's say someone is posting on the french part of the site, the current language would be "fr", and then I would need to store "fr" in the $_POST['icl_post_language'] parameter.

    Doing all this in a hidden field would be great if possible.

    Is that helpful or would you need more info?

    Thanks a lot!

    Posted 7 years ago on Friday September 21, 2012 | Permalink
  11. revonorway
    Member

    Ok, just managed to do this with some copy/pasting and searching different places. Here is for anyone else that would need it:

    //This filter prepopulates gravity forms with current language. Add a text field (can be hidden) and tick the "allow field to be populated dynamically" box. The parameter name is current_language.
    
    add_filter('gform_field_value_current_language', create_function("", '$value = ICL_LANGUAGE_CODE; return $value;' ));
    
    //Then add that language to the icl_post_language of the post just created. Change the 2 after gform_after_submission_ to whatever form needs this. Change entry[6] to whatever field id you have chosen for the language field.
    
    add_action("gform_after_submission_2", "set_post_content", 10, 2);
    function set_post_content($entry, $form){
    
        //getting post
        $post = get_post($entry["post_id"]);
    
        //changing post content
        $_POST['icl_post_language']= $entry[6];
    
        //updating post
        wp_update_post($post);
    }

    VoilĂ ! The code is pretty straightforward.

    Posted 7 years ago on Tuesday September 25, 2012 | Permalink
  12. Thank you for posting your solution.

    Posted 7 years ago on Wednesday September 26, 2012 | Permalink

This topic has been resolved and has been closed to new replies.