This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by Bruno Kos 1 month, 2 weeks ago.

Assigned support staff: Bruno Kos.

Author Posts
August 28, 2019 at 7:48 am

Marten Vaino

I am trying to create a possibility which automatically duplicates post into every language if the post is published. But after pressing "publish" it changes category into default category in every language. Quite same problem was here but it's closed: https://wpml.org/forums/topic/duplicate-to-language-posts-automatically-gets-category-uncategorized/

PHP function which should do the thing was taken here: https://wpml.org/forums/topic/auto-duplicate-postspages-on-publish-2/

I also tried wpml_admin_make_post_duplicates but that didn't work either (after couple of minutes it changed to default category).

I have changed theme and disable all plugins (except wpml) but this is not a case.

Link to a page where the issue can be seen: hidden link

August 28, 2019 at 9:46 am
August 28, 2019 at 11:27 am #4473895

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

I was about to send this case to our 2nd tier, however I see that the form is not working at all:
hidden link

I also noticed that all the code we tried has been commented out, so can I assume that no WPML code for creating duplicates has been used at all at the moment?. Can you tell me if you can get the form working? I see that it is endlessly spinning and no posts are being created.

Regards,
Bruno Kos

August 28, 2019 at 11:53 am #4474015

Marten Vaino

I uncommented our last function. Sorry for inconvenience.

August 28, 2019 at 1:17 pm #4474537

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

No worries - while we are checking this, can you install Duplicator plugin and create a package? We may need it for further debugging.

https://wpml.org/faq/provide-supporters-copy-site/

Let me know if this is OK with you. We will download packages eventually directly from the site, so no need to upload them into external services.

Regards,
Bruno Kos

August 28, 2019 at 1:29 pm #4474655

Marten Vaino

It's o.k, I've built the package

August 28, 2019 at 1:42 pm #4474729

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

I received feedback from the 2nd tier:

wpml_make_post_duplicates needs post ID and wp_footer hook will not give you a post ID.

Can you check with CF7 support to find out the hook when post is created on front-end with newly created post ID? Using that hook we can suggest our function which can duplicate the posts.

Therefore, can you reach out their support about this, sending them all the information, along with documentation and the code?

add_action('wp_footer', 'my_duplicate_on_insert');
function my_duplicate_on_insert($post_id ) {
global $post;
$is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
    if ( $is_translated ) {
        do_action( 'wpml_make_post_duplicates', $post_id );
    }
}

Regards,
Bruno Kos

August 28, 2019 at 2:05 pm #4474997

Marten Vaino

O.k, will do.

August 28, 2019 at 7:09 pm #4476667

Marten Vaino

Their answer was: "Contact Form 7 is a contact form plugin. It doesn’t create posts." I'll try asking it from "Post my CF7" creator.

August 29, 2019 at 6:02 am #4477957

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Ok, let me know about the results.

Regards,
Bruno Kos

August 29, 2019 at 12:09 pm #4480659

Marten Vaino

Hey, I got an answer.

"Hello there,

its not clear exactly at which point to you need to get the post ID, however, when you form is posted and successfully mapped (saved) to a post, the hook #16 in the metabox helper (see screenshots #8).

This is hook #16:

add_action('cf7_2_post_form_submitted_to_post', 'new_post_mapped',10,3);
/**
* Function to take further action once form has been submitted and saved as a post.  Note this action is only fired for submission which has been submitted as opposed to saved as drafts.
* @param string $post_id new post ID to which submission was saved.
* @param array $cf7_form_data complete set of data submitted in the form as an array of field-name=>value pairs.
* @param string $cf7form_key unique key to identify your form.
*/
function new_post_mapped($post_id, $cf7_form_data, $cf7form_key){
  //do something.
}

at this point you could programmatically create your additional WPML posts (as drafts or publish them automcally),however

"After pressing publish this post has to duplicate into every language I have set by WPML."

it sounds like you are manually publishing the draft form posts and then wanting it to be duplicated as WPML posts, in which case you need the WordPress hook save_post which is fired after you have published your posts."

save_post - https://developer.wordpress.org/reference/hooks/save_post/

August 29, 2019 at 12:42 pm #4480945

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Perhaps something like this could work:

add_action('cf7_2_post_form_submitted_to_post', 'new_post_mapped',10,3);
/**

Function to take further action once form has been submitted and saved as a post. Note this action is only fired for submission which has been submitted as opposed to saved as drafts.
@param string $post_id new post ID to which submission was saved.
@param array $cf7_form_data complete set of data submitted in the form as an array of field-name=>value pairs.
@param string $cf7form_key unique key to identify your form.

*/
function new_post_mapped($post_id, $cf7_form_data, $cf7form_key){
if ( $post_id ) {
do_action( 'wpml_make_post_duplicates', $post_id );
}
}

But we can't guarantee about this because we have not tested it. Therefore, since this seems to be going outside of the support scope, I can only suggest any of the following:
- consult our contractors to code and test this for you https://wpml.org/contractors/
- maybe consider checking out https://toolset.com/home/views-create-elegant-displays-for-your-content/ or search through that support to see if there are working examples of this

Regards,
Bruno Kos

August 29, 2019 at 12:43 pm #4480949

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

I gave the wrong link - it is not about toolset views, but Forms plugin:
https://toolset.com/forums/topic/duplicate-cred-form-at-frontend/

Regards,
Bruno Kos