WordPress.org

Ready to get started?Download WordPress

Forums

Language url Prefix for WordPress. (1 post)

  1. Arturo emilio
    Member
    Posted 5 months ago #

    I've seen a lot of threads asking the same, but all of them seem really confusing and i couldn't get the code to work properly.

    WordPress handles the redirects and has an API for that, however the Codex is really confusing also doesn't really explain how to get to work the rewrite hooks and i don't understand the workings of that API.
    I'm trying to include the language prefix in one plugin but i can't seem to get it to work properly.
    This is the code i've got so far:

    function add_rewrite_rules( $wp_rewrite )
    {
            $keytag = '%lang%';
    	$wp_rewrite->add_rewrite_tag($keytag, 'en', 'lang=en');
    	$keywords_structure = $wp_rewrite->root . "$keytag/";
    	$keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
    	$wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules;
    
            $wp_rewrite->add_rewrite_tag($keytag, 'en/.+?', 'lang=en');
    	$keywords_structure = $wp_rewrite->root . "$keytag/";
    	$keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
    // Always add your rules to the top, to make sure your rules have priority
          $wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules;
    }
    add_action('generate_rewrite_rules', 'add_rewrite_rules');
    
    function query_vars($public_query_vars) {
    	$public_query_vars[] = "lang";
    	return $public_query_vars;
    }
    add_filter('query_vars', 'query_vars');
    add_action('init', 'flush_rewrite_rules');
    function flush_rewrite_rules()
    {
    		global $wp_rewrite;
    		$wp_rewrite->flush_rules();
    }

    This will make possible to detect the query lang for urls like :
    http://www.arturoemilio.es/en/
    or
    http://www.arturoemilio.es/en/proba/ (for page)

    However although i get the query_var LANG = en, it doesn't do the rest of the redirects so, it doesn't show you the page (home, proba) but takes you to the list of entries page:
    for example url : http://www.arturoemilio.es/ideas-y-proyectos/

    array (
      'page' => '',
      'pagename' => 'ideas-y-proyectos',
    )

    but the url http://www.arturoemilio.es/en/ideas-y-proyectos/ gives this

    array (
      'lang' => 'en',
    )

    I have to say that the home is an static page (not the las entries).
    What am i missing here?

    Note: The hooks are only active to admins, so it will not work right know for visitors.
    Thanks

Reply

You must log in to post.

About this Topic

  • RSS feed for this topic
  • Started 5 months ago by Arturo emilio
  • This topic is not resolved
  • WordPress version: 3.8.1