This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Ricardo Alday 1 year ago.

Assigned support staff: Ricardo Alday.

Author Posts
October 11, 2018 at 10:08 pm #2809879

vincentL-10

I added a rewrite rule to a page with variable locations which gets applied on all pages using a specific template.

Everything is working okay except for a fact that when I save the English version of the page the rewrite rule for all french versions gets deleted and when I save the French version of the page all the english rules get deleted.

This is the code I am using

public function register_location_rewrite_rule() {

		// args to get all pages with the selected template
		$args = array(
		    'post_type' => 'page',
		    'posts_per_page' => -1,
		    'meta_query' => array(
		        array(
		            'key' => '_wp_page_template',
		            'value' => 'template-path/template-name.php'
		        )
		    )
		);

		// get all pages with the selected template 
		$the_pages = new WP_Query( $args );

		// for each page with the selected template add the rewrite rule 
		// that accepts the location attribute
		if( $the_pages->have_posts() ){
		    while( $the_pages->have_posts() ){
		        $the_pages->the_post();
		        add_rewrite_rule('^'.$the_pages->post->post_name.'/([^/]*)?','index.php?page_id='.get_the_ID().'&location=$matches[1]','top');
		    }
		}
		wp_reset_postdata();
	}
October 12, 2018 at 9:08 pm #2813067

Ricardo Alday

Hi,

Thank you for contacting WPML support.
We cannot provide extensive support when it comes to custom programming. That said, you might need to check the current language before the query.

Something like:

public function register_location_rewrite_rule() {
   $my_current_lang = apply_filters( 'wpml_current_language', NULL ); //Store current language
    
    do_action( 'wpml_switch_language', 'en' ); //Switch to the language in which page exist
    

        // args to get all pages with the selected template
        $args = array(
            'post_type' => 'page',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    'key' => '_wp_page_template',
                    'value' => 'template-path/template-name.php'
                )
            )
        );
 
        // get all pages with the selected template 
        $the_pages = new WP_Query( $args );
 
        // for each page with the selected template add the rewrite rule 
        // that accepts the location attribute
        if( $the_pages->have_posts() ){
            while( $the_pages->have_posts() ){
                $the_pages->the_post();
                add_rewrite_rule('^'.$the_pages->post->post_name.'/([^/]*)?','index.php?page_id='.get_the_ID().'&location=$matches[1]','top');
            }
        }
        wp_reset_postdata();

     do_action( 'wpml_switch_language', $my_current_lang ); //Switch again to current language
    }

Hope that points you in the right direction.

Best regards,
Ricardo

The topic ‘[Closed] Translation rewrite rule gets deleted on post update’ is closed to new replies.