Skip Navigation

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.

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 2 years, 5 months ago.

Assisted by: Nigel.

Author Posts
July 11, 2022 at 1:06 pm #11642791

Bernd Baumann

Hy,

My jobs are created with WP Job Manger.

And that in the frontend, the visitor does not choose the default language.

Therefore, the job will only output in the current language setting.
I also don't want the jobs to be translated in different languages.

It should be possible that the created job is automatically duplicated in the default language.

So that the job is displayed in all languages.

Here I have found a good solution
https://wpml.org/forums/topic/automatically-duplicate-a-new-post/#post-9619543

Thanks Bernd

July 12, 2022 at 11:24 am #11649651

Nigel
Supporter

Timezone: Europe/Madrid (GMT+01:00)

Hi Bernd

Can I clarify with you how you intend things to work.

You have users submitting Job posts using the front-end forms provided by WP Job Manager.

Those submissions could be in any of the site languages.

By way of example, let's say the default language is EN, and the site is also available in DE, FR, and ES.

If someone submits a Job in EN, you don't want the post to be translated, you want visitors to be able to visit DE, FR and ES versions of the posts and see the original EN text.

If someone submits a Job in FR, you want visitors to be able to visit EN, DE and ES versions of the post and see the untranslated FR text.

Is that correct?

The first case is straightforward; if the Job post type is set to translatable with fallback then visitors can access the URLs of the "translated" posts and see the original EN content.

This works because the Job was submitted in the default language.

But that setting only works where the original post is in the default language, not when originating with a post in a secondary language.

So an alternative is to set the Job post type as translatable, but without the fallback, so that duplicates of the original submission need to be created.

This does not require the submitted Job to be in the default language, see details of the wpml_make_post_duplicates hook that can be used to generate the duplicates: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-606504

That is the solution in the thread you linked to, and the code shared by Noman here: https://wpml.org/forums/topic/automatically-duplicate-a-new-post/#post-9619543

In particular, you would need to use the second example (for a specific post type), edited for the custom post type used by WP Job Manager.

Did you try that?

July 15, 2022 at 11:05 am #11673753

Bernd Baumann

Hy Nigel,

thanks for anwser.
Yes, that is exactly the problem.
If in the frontend the job is not created in the default language (EN).

The function *translatable with fallback* takes effect.
Because the job was not created in EN.

yes i have tried with this code.
However, it comes to a time out.
And if it works, not all data is copied.

add_action( 'wp_insert_post', 'my_duplicate_on_publishh' );
function my_duplicate_on_publishh( $post_id ) {
       
    $post = get_post( $post_id ); 
        
    // don't save for autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // dont save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }
     
      
     if ( isset( $post->post_type ) && $post->post_type == 'job_listing' ) {
         
        // we need this to avoid recursion see add_action at the end
        remove_action( 'wp_insert_post', 'my_duplicate_on_publishh' );
         
            // make duplicates if the post being saved
            // #1. itself is not a duplicate of another or
            // #2. does not already have translations
         
            $is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
         
            if ( !$is_translated ) {
                do_action( 'wpml_admin_make_post_duplicates', $post_id );
            }
     
    }
    // must hook again - see remove_action further up
    add_action( 'wp_insert_post', 'my_duplicate_on_publishh' );
}

July 18, 2022 at 11:12 am #11685193

Nigel
Supporter

Timezone: Europe/Madrid (GMT+01:00)

Hi there

I checked the code on my own test site, and it looks like there is a flaw with the code, in particular where the final line to add back the action hook that was previously removed appears.

Here is an updated version of the code with that fixed:

add_action('wp_insert_post', 'my_duplicate_on_publishh');
function my_duplicate_on_publishh( $post_id )
{
    $post = get_post($post_id);

    // don't save for autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // dont save for revisions
    if (isset($post->post_type) && $post->post_type == 'revision') {
        return;
    }

    if (isset($post->post_type) && $post->post_type == 'job_listing') {

        // we need this to avoid recursion see add_action at the end
        remove_action('wp_insert_post', 'my_duplicate_on_publishh');

        // make duplicates if the post being saved
        // #1. itself is not a duplicate of another or
        // #2. does not already have translations

        $is_translated = apply_filters('wpml_element_has_translations', '', $post_id, $post->post_type);

        if (!$is_translated) {

            do_action( 'wpml_admin_make_post_duplicates', $post_id );
        }

        // must hook again - see remove_action further up
        add_action('wp_insert_post', 'my_duplicate_on_publishh');
    }

}

Don't forget to check the WPML settings to make sure the Job Listing post type is translatable, as well as checking settings for any custom fields that you may want the duplicated posts to include copies of. (The Job Manager may be adding custom fields to the posts and you will likely to want to ensure they are also added to the translated duplicates.)