url rewriting – How to apply multiple rewrite_rule to a single custom post type url?

I have a link that I have create by wordpress re_write rule
as mention below”

custom post type is “bollywood”

Link: domain/bollywood/old-movies-of-hero1-with-hero2

where hero1 and hero2 is the same post of same custom post type “bollywood”

Rule:
add_rewrite_rule(
‘^bollywood/old-movies-of-((^/)+)-to-((^/)+)/?$’,
‘index.php?post_type=bollywood&hero1=$matches(1)&hero2=$matches(2)’,
‘top’
);

when I open single post page of this custom psot type it will be as

domain/bollywood/hero1

but I want as
domain/bollywood/old-movies-of-hero1

can we do this so the both links work fine

domain/bollywood/old-movies-of-hero1-with-hero2

domain/bollywood/old-movies-of-hero1

url rewriting – Bilingual WP site: How to achieve different URL sturcture rule based on its language?

A wordpress website with bilingual setup, with three languages:

  • English (“primary language”) : “en”
  • Traditional Chinese : “zh-hant”
  • Simplified Chinese : “zh-hans”

The bilingual setup is currently achieved with the polylang plugin.

I would like to have different URL structure based on the post’s language.

For English,

example.com/blog/%category%/%post_title%

But for non-English blog post URL, I would like to use post id instead of post_title:

example.com/blog/zh-hant/%category%/%post_id%

url rewriting – How to add custom rewrite rules and point to specific templates

How can I add a custom rewrite rule so I can catch paths like these:

/products/imports/filters
/products/imports/elements
/products/imports/parts

And point them to a template file, for examples:

sector.php

My code:

function sector_rewrite_rule() {
    add_rewrite_rule(
        '(.?.+?)/((^/)*)/((^/)*)/?$',
        'index.php?pagename=$matches(1)/$matches(2)&sector=$matches(3)',
        'top'
    );
}
add_action('init', 'sector_rewrite_rule', 10, 0);

But I get 404 when I tried with the URLs above.

I have a standard page called import created under products already:

products/
   imports/

So I want to catch any word right after this page:

/products/imports/

wp query – Multisite – how to stop rewriting and redirecting to wrong locations?

How can I stop my wordpress multisite from rewriting my URLs from:

/news/?paged=20

to

/news/page/20/

I have this set up in my functions.php

function add_query_vars_filter( $vars ) {
  $vars() = 'paged'; // variable for pagination.
  $vars() = 'p'; // variable page for pagination.
  return $vars;
}
add_filter('query_vars', 'add_query_vars_filter');

And I have no ideas why WordPress keeps rewriting my URLs.

Sometimes, it just redirects to:

/en/hello-world/

That is completely wrong!

Any ideas?

url rewriting – Rewrite custom post type URL in search

I have a website in which I have a custom post type (guest authors from CoAuthors Plus). With a plugin I managed to make post of custom type “guest author” searchable by WordPress legacy search.

Now, the authors are correctly shown in search results. Although, they are linked to a wrong page, /?post_type=guest-author&p=2148, which brings to a 404.

I’d like to be able to get the URL, interprete it, and redirect to the correct page (which is in the form of /archives/author/name-surname/.

I’m trying to get it working with a rewrite URL, but I’m not able to catch the data and formulate the rewrite.

python – rewriting a global variable

I am checking if my global variable item have the new content of my function
when i do the print statement it works in the def scope

But the problem is that, when I call the class register() and check de object proof value item it returns (0) again, you can check it in the last line

I don’t know why

 class register():
    item = (0)
    print(f'item before function -> {item}')
    def __init__(self, quantity = '20', fabric_c = 'client', fabric_p = 'own'):
        self.quantity = quantity
        self.fabric_c = fabric_c
        self.fabric_p = fabric_p
        p_c = True
    def copy(self):
        global item
        if p_c == True:
             line = self.quantity + ' meters ' + self.fabric_c
             item = line
             print(f'printing description during execution function -> {line}')
    
        print(f'item after function -> {item}')
proof = register()
proof.copy()
proof.item

item before function -> (0)

printing description during execution function -> 20 meters client

item after function -> 20 meters client

(0)

mod rewrite – Apache recursive rewriting rules

I’m writing a PHP CMS and, for each page, I can have a subpage, and a page number, and a language switch, and a previous version.
Then, I can edit a page, or call a special page.

Examples :

http://example.com/info => http://example.com/index.php?page=info
http://example.com/info/versions => http://example.com/index.php?page=info&sub=versions
http://example.com/info/versions/2 => http://example.com/index.php?page=info&sub=versions&p=2
http://example.com/info/versions/2/EN => http://example.com/index.php?page=info&sub=versions&p=2&lang=EN
http://example.com/info/EN => http://example.com/index.php?page=info&lang=EN

I’m using these URL rewriting rules :

RewriteEngine on
RewriteRule ^((a-z0-9_-)+)$ ./index.php?page=$1 (L)
RewriteRule ^((a-z0-9_-)+)/((0-9){12})$ ./index.php?page=$1&bkp=$2 (L)
RewriteRule ^((a-z0-9_-)+)/((A-Z){2})$ ./index.php?page=$1&lang=$2 (L)
RewriteRule ^((a-z0-9_-)+)/((0-9)+)$ ./index.php?page=$1&p=$2 (L)
RewriteRule ^((a-z0-9_-)+)/((0-9)+)/((A-Z){2})$ ./index.php?page=$1&p=$2&lang=$3 (L)
RewriteRule ^((a-z0-9_-)+)/((a-z0-9_-)+)$ ./index.php?page=$1&sub=$2 (L)
RewriteRule ^((a-z0-9_-)+)/((a-z0-9_-)+)/((0-9){12})$ ./index.php?page=$1&sub=$2&bkp=$3 (L)
RewriteRule ^((a-z0-9_-)+)/((a-z0-9_-)+)/((A-Z){2})$ ./index.php?page=$1&sub=$2&lang=$3 (L)
RewriteRule ^((a-z0-9_-)+)/((a-z0-9_-)+)/((0-9)+)$ ./index.php?page=$1&sub=$2&p=$3 (L)
RewriteRule ^((a-z0-9_-)+)/((a-z0-9_-)+)/((0-9)+)/((A-Z){2})$ ./index.php?page=$1&sub=$2&p=$3&lang=$4 (L)
RewriteRule ^@((a-z0-9_-)+)$ ./index.php?edit=$1 (L)
RewriteRule ^@((a-z0-9_-)+)/((A-Z){2})$ ./index.php?edit=$1&lang=$2 (L)
RewriteRule ^!((a-z0-9_-)+)$ ./index.php?special=$1 (L)
RewriteRule ^!((a-z0-9_-)+)/((A-Z){2})$ ./index.php?special=$1&lang=$2 (L)

It works, but it’s creepy, and if I want to add an option, I have to edit my conf file.

So, I want to simplify it.
What I want :

  1. URL with just values, not variables name, separated by a slash.

http://example.com/page/.../...

  1. If the “page” name corresponds to a folder, ignore the folder.

I have a infos folder, but I want to rename my info page (see the example above) into infos (I want to be able to create a page named core without trying to access the core folder).

  1. The variables can be anywhere, except the first one (page) and should match the same regex pattern.

http://example.com/info/versions/EN will become http://example.com/index.php?info&versions&EN, then I’ll deal with the $_GET array with preg_grep.


If the page variable is empty, the home page will be loaded.
If the new rules work, I’ll check $_GET variables format in index.php

For the 1st variable, I want this pattern : (a-z0-9_-@!)+
For the other, I want this one : (A-Za-z0-9)+
I’m trying lots of solutions since 3 days, without succeeding. I don’t know Apache as well as I should so, can you help me ?

I will write an original article and Blog for $10

I will write an original article and Blog

Are you looking for some quality article writing for your website or blog? Are you struggling to get engaging and well researched content? Well, don’t worry anymore. You are at the right place.

I know people find it hard to find a combination of the following things.

  • SEO Optimized Content
  • Engaging Content
  • Well Researched content
  • Reader Friendly Content

My article writing process is 100 percent manual and I provide 100 percent copyscape unique articles along with copyscape report as a proof. I also keep a check on grammar quality to keep things flowing in my articles.

I can do article writing on diverse range of easy and tough topics. You just need to share your requirements and leave everything to me.

Note: I don’t write on Adult topics.

I am always available to communicate and I provide quick delivery of 24 hours.

If you are in need of some worthy content, just hit the ORDER NOW button of my gig and you will get results which will make you my repeat customer.

Have you read all this with great interest? Now, I am expecting to get a working relationship getting started.

.

custom post types – Pagination on taxonomy template not working after rewriting slugs

My pagination is not working on my taxonomy(sub-category) page. It keeps on loading my archive template. This happened after I applied function codes that re-write’s url and producing all parent and child category ony my permalink.

Heres my code under products-post-type.php

<?php

/*
** ----------
** Register Products Post Type
** ----------
*/
function products_post_type() {

    $labels = array(
        'name'                          => _x( 'Brands', 'Post Type General Name', 'text_domain' ),
        'singular_name'                 => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'                     => __( 'Products', 'text_domain' ),
        'name_admin_bar'                => __( 'Products', 'text_domain' ),
        'archives'                      => __( 'Products', 'text_domain' ),
        'attributes'                    => __( 'Product Attributes', 'text_domain' ),
        'parent_item_colon'             => __( 'Parent Product:', 'text_domain' ),
        'all_items'                     => __( 'All Products', 'text_domain' ),
        'add_new_item'                  => __( 'Add New Product', 'text_domain' ),
        'add_new'                       => __( 'Add New Product', 'text_domain' ),
        'new_item'                      => __( 'New Product', 'text_domain' ),
        'edit_item'                     => __( 'Edit Product', 'text_domain' ),
        'update_item'                   => __( 'Update Product', 'text_domain' ),
        'view_item'                     => __( 'View Product', 'text_domain' ),
        'view_items'                    => __( 'View Products', 'text_domain' ),
        'search_items'                  => __( 'Search Product', 'text_domain' ),
        'not_found'                     => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'            => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'                => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'            => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image'         => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'            => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'              => __( 'Insert into product', 'text_domain' ),
        'uploaded_to_this_item'         => __( 'Uploaded to this product', 'text_domain' ),
        'items_list'                    => __( 'Products list', 'text_domain' ),
        'items_list_navigation'         => __( 'Products list navigation', 'text_domain' ),
        'filter_items_list'             => __( 'Filter products list', 'text_domain' ),
    );

    $args = array(
        'label'                         => __( 'Product', 'text_domain' ),
        'labels'                        => $labels,
        'supports'                      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments'),
        'taxonomies'                    => array( 'product-categories' ),
        'public'                        => true,
        'show_ui'                       => true,
        'show_in_menu'                  => true,
        'menu_position'                 => 5,
        'menu_icon'                     => 'dashicons-screenoptions',
        'show_in_admin_bar'             => true,
        'show_in_nav_menus'             => true,
        'can_export'                    => true,
        'exclude_from_search'           => false,
        'publicly_queryable'            => true,
        'hierarchical'                          => false,
        'has_archive'                           => 'products',
        'show_in_rest'                          => true,
        'capability_type'               => 'post', //...or page
        'rewrite'                                   => array(
                                                                         'slug'                     => 'brands-item',
                                                                         'with_front'       => false,
                                                                         'hierarchical'   => true,
                                                                     ),
    );
    register_post_type( 'products', $args );

}
add_action( 'init', 'products_post_type', 0 );




/*
** ----------
** Register Products Taxonomy - Hierarchical
** ----------
*/

function products_taxonomy_hierarchical() {

    $labels = array(
        'name'                          => _x( 'Brand Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'                 => _x( 'Brand Category', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                     => __( 'Brand Categories', 'text_domain' ),
        'all_items'                     => __( 'All Brand Categories', 'text_domain' ),
        'parent_item'                   => __( 'Parent Brand Category', 'text_domain' ),
        'parent_item_colon'             => __( 'Parent brand category:', 'text_domain' ),
        'new_item_name'                 => __( 'New Brand Category', 'text_domain' ),
        'add_new_item'                  => __( 'Add New Brand Category', 'text_domain' ),
        'edit_item'                     => __( 'Edit Brand Category', 'text_domain' ),
        'update_item'                   => __( 'Update Brand Category', 'text_domain' ),
        'view_item'                     => __( 'View Brand Category', 'text_domain' ),
        'separate_items_with_commas'    => __( 'Separate brand categories with commas', 'text_domain' ),
        'add_or_remove_items'           => __( 'Add or remove brand categories', 'text_domain' ),
        'choose_from_most_used'         => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'                 => __( 'Popular Brand Categories', 'text_domain' ),
        'search_items'                  => __( 'Search Brand Categories', 'text_domain' ),
        'not_found'                     => __( 'Not Found', 'text_domain' ),
        'no_terms'                      => __( 'No items', 'text_domain' ),
        'items_list'                    => __( 'Brand Categories list', 'text_domain' ),
        'items_list_navigation'         => __( 'Brand Categories list navigation', 'text_domain' ),
    );
    $args = array(
        'labels'                        => $labels,
        'hierarchical'                  => true,
        'public'                        => true,
        'show_ui'                       => true,
        'show_admin_column'                     => true,
        'show_in_nav_menus'                     => true,
        'show_in_rest'                              => true,
        'rewrite'                                       => array(
                                                                             'slug'                     => 'brands-categories',
                                                                             'with_front'       => false,
                                                                             'hierarchical'   => true,
                                                                         ),
        // 'rewrite'                        => $rewrite,
    );
    register_taxonomy( 'product-categories', array( 'products' ), $args );

}
add_action( 'init', 'products_taxonomy_hierarchical', 0 );










add_action( 'generate_rewrite_rules', 'register_products_rewrite_rules' );
function register_products_rewrite_rules( $wp_rewrite ) {
    $new_rules = array(
        'brands/((^/)+)/?$' => 'index.php?product-categories=' . $wp_rewrite->preg_index( 1 ),
        'brands/((^/)+)/((^/)+)/?$' => 'index.php?post_type=products&product-categories=' . $wp_rewrite->preg_index( 1 ) . '&products=' . $wp_rewrite->preg_index( 2 ),
        'brands/((^/)+)/((^/)+)/page/(d{1,})/?$' => 'index.php?post_type=products&product-categories=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 3 ),
        'brands/((^/)+)/((^/)+)/((^/)+)/?$' => 'index.php?post_type=products&product-categories=' . $wp_rewrite->preg_index( 5 ) . '&products=' . $wp_rewrite->preg_index( 3 ),
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite->rules;
}



// A hacky way of adding support for flexible custom permalinks
// There is one case in which the rewrite rules from register_kb_rewrite_rules() fail:
// When you visit the archive page for a child section(for example: http://example.com/products/category/child-category)
// The deal is that in this situation, the URL is parsed as a Knowledgebase post with slug "child-category" from the "category" section
function fix_products_subcategory_query($query) {
    if ( isset( $query('post_type') ) && 'products' == $query('post_type') ) {
        if ( isset( $query('products') ) && $query('products') && isset( $query('product-categories') ) && $query('product-categories') ) {
            $query_old = $query;
            // Check if this is a paginated result(like search results)
            if ( 'page' == $query('product-categories') ) {
                $query('paged') = $query('name');
                unset( $query('product-categories'), $query('name'), $query('products') );
            }
            // Make it easier on the DB
            $query('fields') = 'ids';
            $query('posts_per_page') = 1;

            // See if we have results or not
            $_query = new WP_Query( $query );
            if ( ! $_query->posts ) {
                $query = array( 'product-categories' => $query('products') );
                if ( isset( $query_old('product-categories') ) && 'page' == $query_old('product-categories') ) {
                    $query('paged') = $query_old('name');
                }
            }
        }
    }

    return $query;
}
add_filter( 'request', 'fix_products_subcategory_query', 10 );


function products_article_permalink( $article_id, $section_id = false, $leavename = false, $only_permalink = false ) {
    $taxonomy = 'product-categories';
    $article = get_post( $article_id );

    $return = '<a href="';
    $permalink = ( $section_id ) ? trailingslashit( get_term_link( intval( $section_id ), 'product-categories' ) ) : home_url( '/brands/' );
    $permalink .= trailingslashit( ( $leavename ? "%$article->post_type%" : $article->post_name ) );
    $return .= $permalink . '/" >' . get_the_title( $article->ID ) . '</a>';
    return ( $only_permalink ) ? $permalink : $return;
}

function filter_products_post_link( $permalink, $post, $leavename ) {
    if ( get_post_type( $post->ID ) == 'products' ) {
        $terms = wp_get_post_terms( $post->ID, 'product-categories' );
        $term = ( $terms ) ? $terms(0)->term_id : false;
        $permalink = products_article_permalink( $post->ID, $term, $leavename, true );
    }
    return $permalink;
}
add_filter( 'post_type_link', 'filter_products_post_link', 100, 3 );

function filter_products_section_terms_link( $termlink, $term, $taxonomy = false ) {
    if ( $taxonomy == 'product-categories' ) {
        $section_ancestors = get_ancestors( $term->term_id, $taxonomy );
        krsort( $section_ancestors );
        $termlink =  home_url( '/brands/' );
        foreach ( $section_ancestors as $ancestor ) {
            $section_ancestor = get_term( $ancestor, $taxonomy );
            $termlink .= $section_ancestor->slug . "https://wordpress.stackexchange.com/";
        }
        $termlink .= trailingslashit( $term->slug );
    }

    return $termlink;
}
add_filter( 'term_link', 'filter_products_section_terms_link', 100, 3 );


?>

Thank you guys for helping me.

How to determine if module within the application is wort refactoring or rewriting?

We all know Joel Spolsky famous article to never rewrite working code.

How about if we don’t consider overall project but a module within? Module can be e.g.:

  • payment handling microservice in e-commerce application
  • data access layer (inline SQL to ORM)
  • front-end layer (MVC to Angular)

Are there circumstances that justifies module (not full project or system) rewrite?