• Hi,

    Preview link for static front page translations are broken, if redirect_lang option (“The front page url contains the language code instead of the page name or page id”) is enabled from the settings.

    Following filter fixes the issue by adding post name to the preview link for translated static front pages:

    
    /**
     * Fix broken front page preview link in Polylang, when redirect_lang option is enabled.
     *
     * @param $preview_link
     * @param $post
     *
     * @return mixed
     */
    function valu_fix_front_page_preview_link( $preview_link, $post ) {
    
    	// Check that post type is page
    	if ( 'page' !== get_post_type( $post ) ) {
    		return $preview_link;
    	}
    
    	// Check that redirect_lang option is enabled
    	if ( 1 !== PLL()->options['redirect_lang'] ) {
    		return $preview_link;
    	}
    
    	// Get language of the current page
    	$language = PLL()->model->post->get_language( $post->ID );
    
    	// Check that current page is a translation of the default front page
    	if ( $post->ID === $language->page_on_front AND pll_default_language() != $language->slug ) {
    
    		// Get front page link with postname
    		$preview_link_with_postname = esc_url( trailingslashit( get_permalink( $post->ID ) . $post->post_name ) );
    
    		// Apply new preview link
    		$preview_link = str_replace( PLL()->links_model->home_url( $language ), $preview_link_with_postname, $preview_link );
    
    	}
    
    	return $preview_link;
    
    }
    
    add_filter( 'preview_post_link', 'valu_fix_front_page_preview_link', 30, 2 );
    

    Related to: https://wordpress.org/support/topic/preview-front-page-broken/

  • The topic ‘Solution for broken front page preview links’ is closed to new replies.