0

I added below script in my website to setup custom language slug like th, se etc.

After adding this below script my pages are working like.

Issue: but my blog pagination like throwing 404 error.

Script custom permalink and rewrite rules

/*---------------Page permalinks ---------------*/
    add_filter( 'page_link', 'prefix_custom_link_option', 10, 3 );
    
    function prefix_custom_link_option($link, $post_id , $sample){
        global $wp_rewrite;
        $category = get_the_terms($post_id, "nk-post-translation");
        $post = get_post($post_id);
        if (!empty($category) && $category[0]->slug == "th") {
            $link = str_replace(home_url(),home_url('th'), $link);
        } elseif (!empty($category) && $category[0]->slug == "se") {
            $link = str_replace(home_url(),home_url('se'), $link);
        } else {
            $link = str_replace(home_url(),home_url(), $link);
        }
        return $link;
    }
    
    
    /*-----------------page rewrite rule -----------*/
    add_filter('page_rewrite_rules', 'my_new_page_rewrite_rules');
    function my_new_page_rewrite_rules($page_rewrite) {
        $rules['^th/(.*)'] = 'index.php?&pagename=$matches[1]'; //for thai pages
        $rules['^se/(.*)'] = 'index.php?&pagename=$matches[1]';  //for swedish pages
        $rules['^(.*)'] = 'index.php?&pagename=$matches[1]';   //for default or english pages
        return $rules;
    }
    
    
    add_filter('post_link', 'custom_permalink', 10, 3);
    add_filter('post_rewrite_rules', 'wp_insertMyRewriteRules');
    
    //rewrite_rules_array
    
    add_filter('init', 'flushRules');
    
    // creating of post permalink from taxonomy slug
    function custom_permalink($permalink, $post, $leavename) {
        $category = get_the_terms($post->ID, "nk-post-translation");
        if (!empty($category) && $category[0]->slug == "th") {
            $permalink = str_replace(home_url(),home_url('th'), $permalink);
        } elseif (!empty($category) && $category[0]->slug == "se") {
            $permalink = str_replace(home_url(),home_url('se'), $permalink);
        } else {
            $permalink = str_replace(home_url(),home_url(), $permalink);
        }
        return $permalink;
    }
    
    //flush the existing rules of taxonomy slug rules
    function flushRules() {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
    
    // inserting new rules of taxonomy slug
    function wp_insertMyRewriteRules($post_rewrite) {
        $post_type = 'post';
        $rules['^th/(.*)$'] = 'index.php?post_type=' . $post_type . '&post=$matches[1]&name=$matches[1]'; 
        $rules['^se/(.*)$'] = 'index.php?post_type=' . $post_type . '&post=$matches[1]&name=$matches[1]'; 
        $rules['^(.*)$'] = 'index.php?post_type=' . $post_type . '&post=$matches[1]&name=$matches[1]'; 
        return $rules;
    }

Your Answer

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

Browse other questions tagged or ask your own question.