简体   繁体   中英

Is there a way to manually control the pagename in WordPress?

I'm working on a bilingual website for a client, offering both English and French. I'm using the qTranslate plugin for the content, which is working great. However the client is requesting the the URLs need to be translated into French as well.

http://englishdomain.com/users

http://frenchdomain.com/utilisateurs

In the example URLs above, 'utilisateurs' needs to use the same page in the admin as 'users'. I already have support for englishdomain.com and frenchdomain.com working. I just need to find a way to tell WordPress to use the 'users' page when the pagename is 'utilisateurs'.

There are a lot of sections in this site, so I was thinking about setting up an array of all the English => French matches and then validating from there.

Interesting objective. The url is actually created based on the page slug, which is singular ... so there's no easy way pair /users/ with /utilisateurs/ . Or at least not a native way.

The best thing you can do is hook in to WordPress' url redirection methods and search a manually-configured array of English=>French page slugs.

Just off the top of my head, generate_rewrite_rules might be a good action hook to look at for starters.

Another possibility is to hook into the query_vars filter and manually replace any French page slugs with their English equivalents. Then your French URL should query the WordPress database for the exact same page as the English URL. You'll just need to be sure that the English slug exists in the database, or you'll run into problems.


Untested Example

The trick is to hook into WordPress' query system and dynamically replace parts of the query as you go. So let's say you start with an array like this (I only have the one element because I don't speak French):

$translations = array(
    'utilisateurs' => 'users'
);

Now we need a function that will take in a French string and return its English equivalent:

function translate_slug( $french ) {
    if( in_array( $french, array_keys( $translations ) ) {
        return $translations[$french];
    }
    return $french
}

Next, we hook onto the query_vars filter and replace our page slug variables with the desired English equivalents:

function filter_french_slugs() {
    global $wp_query;
    $wp_query->query_vars['pagename'] = translate_slug( $wp_query->query_vars['pagename'] );
    $wp_query->query_vars['name'] = translate_slug( $wp_query->query_vars['name'] );
}
add_filter( 'pre_get_posts', 'filter_french_slugs' );

This should convert things over for you, but as I said above it's untested as of yet ... just an idea of how you could potentially pull this off.

A way is capture the request before wp_query set the value (pre_get_posts); and/or to overwrite it in wp_query object.

This works for me with globals $_REQUEST and overwrite wp_query params, I don't check this with the original object $wp_query->get('param');

Example: Note: Instead use the global object, a best option is pass WP_Query object to the function and return it. See the Codex, wp_query is passed by reference (http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts).

function filter_french_slugs(WP_Query $query) {
    // Don't forget sanitize $_REQUEST params, for safety. Eg. array_map()
    // Your request param (example post_type):
    $original_post_type = translate_slug($_REQUEST['my_localized_post_type']);
    // Or gettext
    $original_post_type = __($_REQUEST['my_localized_post_type']); // Sanitize this param

    // XXX The most important:
    $query->set('post_type', $original_post_type);

    // You can also work through an array of all query_vars

    // XXX Important
    return $query;
}
add_filter('pre_get_posts', 'filter_french_slugs');

It's tricky. You can look at a plugin like WPML, but I don't think any of them will do what you want.

One possibility to explore is to use Urban Giraffe's Redirection plugin:

http://urbangiraffe.com/plugins/redirection/

There is a "pass through" action that can be used to do what you want. You would have to define redirection rules for each page, but it should do the trick.

Try the qTranslate slug with widget plugin.

It adds a qTranslate Slug translation widget to the edit page that allows you to specify a separate slug for each language.

As far as I can tell, it only works on posts and pages, though, not on taxonomies or custom post types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM