• Resolved lbineau

    (@lbineau)


    Hi everyone,
    I am trying to make some ajax working with Polylang and I have a problem with language.
    It is always detected as default language and not the current language.
    So I manage to force the language in $_POST[‘query_vars’] to find just posts of current language => orderby=date&order=DESC&lang=en&post_status=publish, but translations into loop-category.php are still in french, the default one => “Lire plus”
    Any idea how to force/initialize polylang into the AJAX function with the current language of the user ?
    Maybe it is because Ajax is rendered by admin-ajax.php and the language is not the same as frontend part ?

    functions.php

    function html5blank_header_scripts()
    {
        // write variables into DOM
        global $wp_query;
        wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'query_vars' => json_encode( $wp_query->query )
        ));
    }
    
    /*------------------------------------*\
    	Ajax
    \*------------------------------------*/
    add_action( 'wp_ajax_nopriv_ajax_pagination', 'my_ajax_pagination' );
    add_action( 'wp_ajax_ajax_pagination', 'my_ajax_pagination' );
    function my_ajax_pagination() {
        var_dump(pll_current_language()); // always the default language (FR)
        $posts = new WP_Query( $_POST['query_vars'] ); // orderby=date&order=DESC&lang=en&post_status=publish so it loads only posts with lang=en...
        $results = array();
        $GLOBALS['wp_query'] = $posts;
        $results['html'] = load_template_part('loop', 'category'); // but the language into the render is stil the default one (FR)
        echo json_encode($results);
    
        die();
    }
    function load_template_part($template_name, $part_name=null) {
        ob_start();
        get_template_part($template_name, $part_name);
        $var = ob_get_contents();
        ob_end_clean();
        return $var;
    }

    loop-category.php

    <?php while (have_posts()) : the_post(); ?>
    <?php the_title(); // OK : EN language thanks to the WP_query &lang=en param ?>
    <?php _e( 'Read more', 'html5blank' ); // KO : FR language => Lire plus ?>
    <?php endwhile; ?>

    https://www.ads-software.com/plugins/polylang/

Viewing 1 replies (of 1 total)
  • Thread Starter lbineau

    (@lbineau)

    Ok I found how to do it, you have to call admin-ajax.php?lang=code.
    So I have to wait pll_current_language() to be available with pll_language_defined action.
    Then the ajax function was called with the right language.

    function.php

    add_action('pll_language_defined', 'on_language_defined');
    // Set some JS variables
    function on_language_defined()
    {
        if ($GLOBALS['pagenow'] != 'wp-login.php' && !is_admin()) {
    
            // write variables into DOM
            global $wp_query;
            wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
                'ajaxurl' => admin_url( 'admin-ajax.php' ) . '?lang='.pll_current_language(), // the language parameters is usefull to call ajax function with language set
                'query_vars' => json_encode( $wp_query->query )
            ));
    
        }
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Ajax current language’ is closed to new replies.