• Hi. We are using wpml plugin to have 2 languages multidomain setup of the website and we have a problem, on the dutch version it shows the English posts. How to set this plugin to pick the duch versions?
    Thank you

    wpml 4.3.6
    WPP 5.1.0
    wp 5.4.2

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @lecreati,

    Please read here.

    Thread Starter lecreati

    (@lecreati)

    All the posts are translated (double-checked it now), if this is what you are referring, i don’t really care if the popularity counting is on the English side I just want it to show the right version

    Plugin Author Hector Cabrera

    (@hcabrera)

    Ah, my apologies. I see what you mean now, @lecreati.

    OK, so I just set up a multidomain site on my local development environment and it works* for me:

    WPML multidomain setup

    WPP widget on the frontend with multiple domains

    * works if the Ajaxify widget option (Settings > WordPress Popular Poss > Tools, under Data) is disabled. If it’s enabled, WPML apparently doesn’t detect the current language correctly and defaults to the main language instead. If this is indeed your case then now you know what’s going on, otherwise it might be a configuration/setup issue on your part.

    What I described above is very likely a bug on WPML’s side since I can’t reproduce this behavior when WPML’s Language URL format option is set to anything else but “A different domain per language” -even with the Ajaxify widget option enabled- so right now your options are:

    • Disable the Ajaxify widget option (if you’re using a caching plugin though this will make your popular posts list remain static: it won’t get updated unless your site’s cache gets regenerated, which could take hours, days or even weeks depending on the configuration you’re using on your site); or
    • Go to WPML > Languages and set Language URL format to “Different languages in directories” (and then you’d lose the ability to have multiple domains per language on your site); or
    • Reach out to the WPML team and report the problem so they can look into it. Feel free to share the information I shared here as part of your report.

    If there’s anything I can do to help don’t hesitate to ask.

    Thread Starter lecreati

    (@lecreati)

    I customized the look of the aoutput and using Themer from Beaver Builder
    here is my code:

    /**
     * Builds custom HTML.
     *
     * With this function, I can alter WPP's HTML output from my theme's functions.php.
     * This way, the modification is permanent even if the plugin gets updated.
     *
     * @param	array	$mostpopular
     * @param	array	$instance
     * @return	string
     */
    function my_custom_popular_posts_html_list( $popular_posts, $instance ){
    	$output = '<ul class="wpp-list">';
        $post_number = 1;
    	// loop the array of popular posts objects
    	foreach( $popular_posts as $popular_post ) {
    		
    		$stats = array(); // placeholder for the stats tag
    
    		
    		
    		// Author option checked
    		if ( $instance['stats_tag']['author'] ) {
    			$author = get_the_author_meta( 'display_name', $popular_post->uid );
    			$display_name =  $author ;
    			$stats[] = '<span class="wpp-author">' . sprintf( __( 'by %s', 'wordpress-popular-posts' ), $display_name ). '</span>';
    		}
    
    		// Date option checked
    		if ( $instance['stats_tag']['date']['active'] ) {
    			$date = date_i18n( $instance['stats_tag']['date']['format'], strtotime( $popular_post->date ) );
    			$stats[] = '<span class="wpp-date">' . sprintf( __( 'posted on %s', 'wordpress-popular-posts' ), $date ) . '</span>';
    		}
    
    		// Category option checked
    		if ( $instance['stats_tag']['category'] ) {
    			$post_cat = get_the_category( $popular_post->id );
    			$post_cat = ( isset( $post_cat[0] ) )
    			  ? '<a href="' . get_category_link( $post_cat[0]->term_id ) . '">' . $post_cat[0]->cat_name . '</a>'
    			  : '';
    
    			if ( $post_cat != '' ) {
    				$stats[] = '<span class="wpp-category">' . sprintf( __( 'under %s', 'wordpress-popular-posts' ), $post_cat ) . '</span>';
    			}
    		}
    		
    	
    		
    		
    		
    
            $output .= '<li><div class="popular_number">'.$post_number.'</div>
                    <div class="popular_info">
                        <h2><a href="' . get_permalink( $popular_post->id ) . '" title="' . esc_attr( $popular_post->title ) . '">'. $popular_post->title .'</a></h1>
    
                        
                            <div class="autor-block-top">
                                <div class="autor-block-top_info-block">
                                    <div class="autor-block-top_info-block_name">'. $display_name .'</div>
                                   
                                </div>
                            </div>
                       
                    </div>
                </li>';
    	
             $post_number++;
    	}
    	
    	$output .= '</ul>';
    
    	return $output;
    }
    add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );

    and then add it with shortcode [wpp stats_author=1 stats_date=1 stats_date_format=’j M, Y’ post_html='<span>{thumb} {text_title} </span>’]

    Plugin Author Hector Cabrera

    (@hcabrera)

    I’m pretty sure it should work with your code as well, it’s not a widget-specific feature: the list should still translate into the current language (but I not near a computer right now to check, it’s lunch time over here so I’ll get back to you on that some time later today.)

    Thread Starter lecreati

    (@lecreati)

    No problem. Thank you, Héctor

    Plugin Author Hector Cabrera

    (@hcabrera)

    Beware: long read ahead.

    Alright, so here’s the deal: it’s because of wpp_custom_html.

    This filter hook gives you a raw array of popular posts objects, meaning none of them have been parsed by WPP. You get popular posts exactly how WPP gets them from the database: untranslated. This filter hook is more like an “advanced” mode geared towards developers, basically a “here’s the data, do whatever you want/need with it.” WPP assumes that you will do whatever you need with the list so it outputs the way you want it to, and in this case this also means you’d need to use WPML’s APIs to translate the posts yourself before displaying them on screen (hence why you’re seeing posts in English :P)

    I may change that behavior in the future though, you’re not the first person having this issue and this is not explained anywhere on the documentation either. Will look into that in the future.

    For the time being, here’s a different approach:

    1. Remove your my_custom_popular_posts_html_list() function from your functions.php file, we’re not going to need it anymore.

    2. Add this function in its place instead:

    /**
     * Parses custom content tags in WordPress Popular Posts.
     *
     * @param  string  $html    The HTML markup from the plugin.
     * @param  integer $post_id The post/page ID.
     * @return string
     */
    function wpp_parse_tags_in_popular_posts($html, $post_id){
    
        // Replace custom content tag {author_display_name} 
        // with author's display name
        if ( false !== strpos($html, '{author_display_name}') ) {
            // Get author's display name
            $author_id = get_post_field('post_author', $post_id);
            $display_name = get_the_author_meta('display_name' , $author_id);
    
            // Replace {author_display_name} with the actual 
            // display name
            $html = str_replace('{author_display_name}', $display_name, $html);
        }
    
        return $html;
    
    }
    add_filter("wpp_parse_custom_content_tags", "wpp_parse_tags_in_popular_posts", 10, 2);

    What this code does: it registers a new custom Content Tag called {author_display_name} that displays the, pardon the redundancy, display name of the post author on the list. We’re going to use that in our shortcode next. For more details: wpp_parse_custom_content_tags.

    3. Replace your shortcode with this one:

    [wpp post_html='<li><div class="popular_number">{item_position}</div><div class="popular_info"><h2><a href="{url}" title="{title_attr}">{text_title}</a></h1><div class="autor-block-top"><div class="autor-block-top_info-block"><div class="autor-block-top_info-block_name">{author_display_name}</div></div></div></div></li>']

    This shortcode uses the exact same HTML markup you had in my_custom_popular_posts_html_list(), with a couple of tweaks: it uses a few existing Content Tags ({item_position}, {url}, {title_attr}, and {text_title}) and our new Content Tag {author_display_name}. These tags will be replaced by actual code when WPP renders your popular posts list (try it out!)

    For more Content Tags check Settings > WordPress Popular Posts > Parameters, you’ll find them nearly at the end of the page.

    With this setup WPML does display the popular posts list in the right language – even under the multidomain configuration. At least it did during my tests on my local dev environment.

    Thread Starter lecreati

    (@lecreati)

    It does show the name of the post but its sims don’t pick up the styling – no formating.
    Why it could be?

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hard to say, I copied the HTML code from your PHP into the shortcode exactly as it was into the shortcode. Maybe something’s interfering with the shortcode itself? Or something else is missing that’s messing up with the styles? Can’t really tell. You’ll need to do some debugging there to find out what’s going on (or consider getting a developer to look into it for you if you don’t have the time and/or the experience to look into it yourself.)

    Other than that, I’m glad to know that the original problem has been fixed so thanks for reporting back.

    Thread Starter lecreati

    (@lecreati)

    The funny part it works on English version but not on Dutch

    Plugin Author Hector Cabrera

    (@hcabrera)

    Well, it’s exactly the same code for both versions so it doesn’t make much sense that the list looks good on English but not on Dutch (unless you’re using separate shortcodes for each language?)

    If you do figure out what’s causing it (it’s quite late over here, I should be sleeping already but I’m wide awake haha) please do leave a comment here to let me know since I’m curious about it.

    Thread Starter lecreati

    (@lecreati)

    Have a good night) for me is late too) Will play with it next week. Also have a great 4th of july

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Setup for WPML: shows english versions of the posts on dutch version of the webs’ is closed to new replies.