Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Chouby

    (@chouby)

    If you have some PHP skills, that should be achievable using the ‘pll_save_post’ action (more or less the ‘save_post’ action fired once the language and translations are set) and $GLOBALS['polylang']->model->get_post_language($post_id) to get the post language.

    Thread Starter Tigr

    (@tigr)

    So, basically, something along these lines is adequate:

    function tigra_add_polylang_language_tag( $post_id ) {
            global $polylang;
            // if variable exists and not null
            if (isset($polylang) ) {
                    if ( !$polylang->get_post_language($post_id) ) {
                            $polylang->set_post_language($post_id, pll_default_language());
                    }
                    $post_lang = $polylang->get_post_language($post_id);
                    $post_tags = wp_get_post_terms($post_id, 'post_tag');
                    if ( ! in_array($post_lang->slug, $post_tags) ) {
                            wp_set_post_tags($post_id, $post_lang->slug, true);
                    }
            }
    }
    add_action( 'save_post', 'tigra_add_polylang_language_tag' );

    But the problem is, if the author changes the language, I’ll get a second tag with a second language… Should I just get the list of currently configured languages and wipe the tags out? I am open to suggestions on how best to handle that part…

    Plugin Author Chouby

    (@chouby)

    As stated above, you should use ‘pll_save_post’ hook instead of ‘save_post’, it will avoid you setting the default language as it is already done. You should also use $polylang->model->get_post_language() and $polylang->model->set_post_language() rather than $polylang->get_post_language() and $polylang->set_post_language() (if you set WP_DEBUG to true, Polylang will issue warnings).

    Then to solve your issue, you could try using this (although not tested, I guess you will catch the idea):

    $languages = $polylang->get_model->get_languages_list(array('fields' => 'slug'));
    $post_tags = get_the_tags($post_id);
    $post_tags = wp_list_pluck($post_tags, 'slug');
    $post_tags = array_diff($post_tags, $languages);
    wp_set_post_tags($post_id, $post_tags);

    Thread Starter Tigr

    (@tigr)

    I also want to make sure that the post language is set even if the pll_save_post is not called. I cannot remember where I saw that but there seemed to be some talk of the language not being set on posts submitted from mobile devices. So, just to be on the safe side, I am using “save_post”. Or am I being idiotic here?

    Thanks for the idea, the following seems to do the trick:

    function tigra_add_polylang_language_tag( $post_id ) {
            global $polylang;
            if (isset($polylang) ) {
                    if ( !$polylang->model->get_post_language($post_id) ) {
                            $polylang->model->set_post_language($post_id, pll_default_language());
                    }
                    $post_lang = $polylang->model->get_post_language($post_id);
                    $languages = $polylang->model->get_languages_list(array('fields' => 'slug'));
                    $post_tags = get_the_tags($post_id);
                    $post_tags = wp_list_pluck($post_tags, 'name');
                    $post_tags = array_diff($post_tags, $languages);
                    if ( empty($post_tags) ) {
                            $post_tags = array();
                    }
                    array_push($post_tags, $post_lang->slug);
                    wp_set_post_tags($post_id, $post_tags);
            }
    }
    add_action( 'save_post', 'tigra_add_polylang_language_tag' );

    Plugin Author Chouby

    (@chouby)

    If you prefer to use save_post, I advise to use a lower priority to come after Polylang, which uses priority 21 for the reason explained in code comment:

    // priority 21 to come after advanced custom fields (20) and before the event calendar which breaks everything after 25

    Thread Starter Tigr

    (@tigr)

    Bloody hell 🙂 Ok, that is really good to know. I’ll make it a 22 then. Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Add a taxonomy automatically based on language?’ is closed to new replies.