Support » Plugin: Polylang » How to know the language of a post through the WordPress REST API

  • Resolved MastaBaba

    (@mastababa)


    I want to access the posts in my multilingual blog through the now included WordPress JSON REST API.

    However, none of the offered feeds seem to include information on the language of the content.

    How to abstract this information?

Viewing 11 replies - 1 through 11 (of 11 total)
  • as his old brother xili-language since WP 2.3, polylang use taxonomy “language” to define the language of the post. So with rest-api, it is easy to search posts in a specific taxonomy.

    M.

    Thanks. This does not seem to be working, though. At least not out-of-the-box. The endpoint

    http://mysite.com/wp-json/wp/v2/taxonomies

    only returns the two default taxonomies, “category” and “post_tag”.

    Explicitly asking for the “language” taxonomy like so:

    http://mysite.com/wp-json/wp/v2/taxonomies/language

    returns a “Sorry, you are not allowed to do that.”

    In addition, the posts endpoint only seems to allow for matching posts on category or tag:

    https://developer.wordpress.org/rest-api/reference/posts/

    I suppose it’s possible to create a custom route which queries posts based on their language. The overview for this would be here:

    https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

    However, this hardly could be called ‘easy’ and requires customisation of the WordPress installation. :/

    Am I missing something obvious in relation to the REST API?

    • This reply was modified 11 months, 3 weeks ago by  .

    It is because Frédéric, polylang’s author has not set his taxonomy property ‘show_in_rest’ => true !

    Try to contact him.

    Thanks a bunch!

    MastaBaba

    (@mastababa)

    The plugin author is not planning to add this to the free version. The pro version will get something that could provide for this.
    To make this work yourself…

    Add something like this to your functions.php;

    function wpse_modify_taxonomy() {
        // get the arguments of the already-registered taxonomy
        $language_args = get_taxonomy( 'language' ); // returns an object
    
        // make changes to the args
        // in this example there are three changes
        // again, note that it's an object
        $language_args->show_in_rest = true;
    
        // re-register the taxonomy
        register_taxonomy( 'language', 'post', (array) $language_args );
    }
    // hook it up to 11 so that it overrides the original register_taxonomy function
    add_action( 'init', 'wpse_modify_taxonomy', 11 );
    Plugin Author Chouby

    (@chouby)

    @mastababa Ther is a much simpler and more performant solution with the filter register_taxonomy_args. This will avoid you to use register_taxonomy with is a slow function.

    MastaBaba

    (@mastababa)

    Thanks. But, it seems I don’t know how to use this. I get:

    Fatal error: Call to undefined function register_taxonomy_args()

    @mastababa,, I’m sharing you how I leverege post’s segmentation in my plugin..

    I will just copy / paste it here,, you’ll hve to remove $this from hooks if you use it in your functions.php file..

            // fix polylang language segmentation
            add_action( 'rest_api_init' , array( $this, 'polylang_json_api_init') );
            add_action( 'rest_api_init' , array( $this, 'polylangroute' ) );
    
        public function polylang_json_api_init(){
            global $polylang;
            $default = pll_default_language();
            $langs = pll_languages_list();
            $cur_lang = $_GET['lang'];
            if (!in_array($cur_lang, $langs)) {
                $cur_lang = $default;
            }
            $polylang->curlang = $polylang->model->get_language($cur_lang);
            $GLOBALS['text_direction'] = $polylang->curlang->is_rtl ? 'rtl' : 'ltr';
        }
    
        public function polylang_json_api_languages(){
            return pll_languages_list();
        }
    

    then on frontend I just read my url with lang parameter.. like this:

    `
    var lang = $(‘html’).attr(‘lang’); // read lang from html tag
    $.ajax( ‘/wp-json/wp/v2/mycustomslag?lang=’ + lang + ‘&per_page=100’ ).done(fn);

    Thanks!

    @gorostas
    Which file did you put these code in?
    I encounter this problem now 🙁

    hey @jim,

    just put it in your functions.php file

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.