0

I'm using WPUF Pro form to submit a custom-post-type vehicle, through frontend. I added a custom function to auto-duplicate post on publish.

I notice that when I got the Form in 2nd language (not default) and I submit my vehicle, the function is not creating duplicate post for the default language, even if the opposite is workign well.

function wpml_duplicate_on_publish($post_id)
{
    global $post, $sitepress, $iclTranslationManagement;

    // don't save for autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }
    // save only for campaign
    if (isset($post->post_type) && $post->post_type != 'vehicle') {
        return $post_id;
    }

    // get languages
    $langs = $sitepress->get_active_languages();
    unset($langs[$sitepress->get_default_language()]);

    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'wpml_translate_post');

    // make duplicates if the post being saved does not have any already or is not a duplicate of another
    $has_duplicates = $iclTranslationManagement->get_duplicates($post_id);
    $is_duplicate = get_post_meta($post_id, '_icl_lang_duplicate_of', true);

    if (!$is_duplicate && !$has_duplicates) {
        //now lets create duplicates of all new posts in all languages used for translations
        foreach ($langs as $language_code => $v) {
            $iclTranslationManagement->make_duplicate($post_id, $language_code);
        }
    }
}

add_action('wpuf_add_post_after_insert', 'wpml_duplicate_on_publish');

Any suggestions ? Thank you in advance.

  • 1
    You are unsetting the default language form $langs array, so when you loop over $langs to make the duplicates, the default language is never included. This may be the cause of your problem (nothing to do with WordPress by the way). – cybmeta Nov 17 '15 at 9:32
  • @cybmeta Please add your comment as an answer so you can get the bounty reputation. We were right. Thank you – John Priestakos Nov 18 '15 at 9:28
0
+50

The problem is that you are unsetting the default language form $langs array, so when you loop over $langs to make the duplicates, the default language is never included. This may be the cause of your problem (nothing to do with WordPress by the way).

function wpml_duplicate_on_publish($post_id) {
    global $post, $sitepress, $iclTranslationManagement;

    // don't save for autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }
    // save only for campaign
    if (isset($post->post_type) && $post->post_type != 'vehicle') {
        return $post_id;
    }

    // get languages
    $langs = $sitepress->get_active_languages();

    // Unseeting the default lenguage was the problem
    // unset($langs[$sitepress->get_default_language()]);

    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'wpml_translate_post');

    // make duplicates if the post being saved does not have any already or is not a duplicate of another
    $has_duplicates = $iclTranslationManagement->get_duplicates($post_id);
    $is_duplicate = get_post_meta($post_id, '_icl_lang_duplicate_of', true);

    if (!$is_duplicate && !$has_duplicates) {
        //now lets create duplicates of all new posts in all languages used for translations
        foreach ($langs as $language_code => $v) {
            $iclTranslationManagement->make_duplicate($post_id, $language_code);
        }
    }
}

add_action('wpuf_add_post_after_insert', 'wpml_duplicate_on_publish');

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.