1

I am trying to create a custom translation for my website.

Quick Story:

I am facing "404 error-Page Not Found' issues with pages after adding custom rules in functions.php

$newrules['^th/(.*)$'] = 'index.php?name=$matches[1]';
$newrules['^se/(.*)$'] = 'index.php?name=$matches[1]';

with the above code, posts with /th/ slug are working perfectly fine but my pages with /th/page-name-1 ... throwing 404 error-Page Not Found

So here is the complete story:

First I created a parent page "Home Thai" https://mywebsite.com/th/ and under that parent page "Home Thai", I added the rest of the Thai language pages, so results coming like this

mywebsite.com/th/page-name-1
mywebsite.com/th/page-name-2
mywebsite.com/th/page-name-3

Now I want to set up Thai Post URL with slug (/th), and to make this, I added the below new rules in functions.php

$newrules['^th/(.*)$'] = 'index.php?name=$matches[1]';
$newrules['^se/(.*)$'] = 'index.php?name=$matches[1]';

with the above code, posts are working perfectly with language slug (/th/, /se/). bit my pages with /th/page-name-1 ... throwing 404 error-Page Not Found

Script:

//Create a function to register a new language translation taxonomy
add_action('init','nk_add_translation_taxonomy');

function nk_add_translation_taxonomy(){
   
    global $post;
    //set the name of the taxonomy
    $taxonomy = 'nk-post-translation';
    //set the types for the taxonomy
    $object_type = array('post');

    //populate our array of names for our taxonomy
    $labels = array(
        'name'               => 'Post Translation',
        'singular_name'      => 'Post Translation',
        'search_items'       => 'Search Translation',
        'all_items'          => 'All Translation',
        'parent_item'        => 'Parent Translation',
        'parent_item_colon'  => 'Parent Translation:',
        'update_item'        => 'Update Translation',
        'edit_item'          => 'Edit Translation',
        'add_new_item'       => 'Add New Translation', 
        'new_item_name'      => 'New Translation',
        'menu_name'          => 'Post Translation',
              
    );
    
    //define arguments to be used 
    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'how_in_nav_menus'  => true,
        'public'            => false,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'post-translation')
    );
    
    //call the register_taxonomy function
    register_taxonomy($taxonomy, $object_type, $args); 
}

add_filter( 'post_link', 'custom_permalink', 10, 3 ); 
add_filter( 'rewrite_rules_array','customLangaugeSlugRules');
add_filter( 'init','flushRules'); 

// creating of post permalink from taxnonmy slug
function custom_permalink( $permalink, $post, $leavename ) {
    $category = get_the_terms($post->ID,"nk-post-translation"); 

    if (  !empty($category) && $category[0]->slug == "th" )
    {
      $permalink = trailingslashit( home_url('th/' . $post->post_name ) );
    }
    elseif (  !empty($category) && $category[0]->slug == "se" )
    {
      $permalink = trailingslashit( home_url('se/' . $post->post_name ) );
    }
    else
    {
      $permalink = trailingslashit( home_url( $post->post_name ) ); 
    }
    return $permalink;
}

// using because of its flush the exixsting rules of taxnonmy slug rules
function flushRules(){
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
}

// inserting new rules of taxnonmy slug
function customLangaugeSlugRules($rules)
{
    $newrules = array();

    $newrules['^se/(.*)$'] = 'index.php?name=$matches[1]';
    $newrules['^th/(.*)$'] = 'index.php?name=$matches[1]';

    return $newrules + $rules;
}
0

1 Answer 1

1

Why it happens

The error 404 on the th/<page slug> pages occurs because your custom rewrite rules overwrite the default Page rules which would otherwise load the correct Page (having the slug <page slug>).

How to fix the issue

If your permalink structure is /%postname%, i.e. example.com/<post slug>, then a simple fix to the error 404 is by using the post_rewrite_rules hook instead of rewrite_rules_array.

So all you need to do is:

// Change this:
add_filter( 'rewrite_rules_array','customLangaugeSlugRules');

// to this one:
add_filter( 'post_rewrite_rules','customLangaugeSlugRules');

Then flush the rewrite rules — but as the WP_Rewrite::flush_rules() documentation says: (*slightly reformatted)

Because this function can be extremely costly in terms of performance, it should be used as sparingly as possible – such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.

So keep that in mind, and you could actually flush the rules manually by simply going to the Permalink Settings admin page (wp-admin → Settings → Permalink).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.