$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>permalinks - Wordpress rewrite_rules_array rule not working properly|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

permalinks - Wordpress rewrite_rules_array rule not working properly

matteradmin10PV0评论

I want the WooCommerce way to create new rules for permalinks. I have checked WooCommerce core files and found that WooCommerce is using this filter for saving the rule in database. My current url is /?page=mpProfile. I have var dumped get_query_var('pagename'); on hit page, but hitting url shows blank. I want this url as . Any one have any idea over this part? I have been stuck on this for around 3 days.

Here is my code:

    add_action( 'wp_loaded','my_flush_rules' );
    add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
    add_filter( 'query_vars','my_insert_query_vars' );

    function my_flush_rules() {
        $rules = get_option( 'rewrite_rules' );

            if ( ! isset( $rules['(seller-login)/(.+)$'] ) ) {
                global $wp_rewrite;
                $wp_rewrite->flush_rules();
            }

    }

    function my_insert_rewrite_rules( $rules ) { 
        $newrules = array();
        $newrules['(seller-login)/(\d*)$'] = 'index.php?pagename=$matches[1]';
        // var_dump($newrules);     
        return $newrules + $rules;
    }

    function my_insert_query_vars( $vars ) {

        array_push($vars, 'pagename');

        return $vars;
    } 

I want the WooCommerce way to create new rules for permalinks. I have checked WooCommerce core files and found that WooCommerce is using this filter for saving the rule in database. My current url is http://amit/wcommerce/seller-login/?page=mpProfile. I have var dumped get_query_var('pagename'); on hit page, but hitting url shows blank. I want this url as http://amit/wcommerce/seller-login/mpProfile. Any one have any idea over this part? I have been stuck on this for around 3 days.

Here is my code:

    add_action( 'wp_loaded','my_flush_rules' );
    add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
    add_filter( 'query_vars','my_insert_query_vars' );

    function my_flush_rules() {
        $rules = get_option( 'rewrite_rules' );

            if ( ! isset( $rules['(seller-login)/(.+)$'] ) ) {
                global $wp_rewrite;
                $wp_rewrite->flush_rules();
            }

    }

    function my_insert_rewrite_rules( $rules ) { 
        $newrules = array();
        $newrules['(seller-login)/(\d*)$'] = 'index.php?pagename=$matches[1]';
        // var_dump($newrules);     
        return $newrules + $rules;
    }

    function my_insert_query_vars( $vars ) {

        array_push($vars, 'pagename');

        return $vars;
    } 
Share Improve this question edited Mar 18, 2019 at 17:00 mburesh 1035 bronze badges asked Sep 20, 2016 at 17:22 Amit ChauhanAmit Chauhan 296 bronze badges 4
  • when i hit amit/wcommerce/seller-login/mpProfile i need pagename variable so that i can use this variable right now it is showing page not found – Amit Chauhan Commented Sep 20, 2016 at 17:37
  • is seller-login a valid page/slug? – Ahmed Fouad Commented Sep 20, 2016 at 20:06
  • Yes it is a valid slug actually it is a page – Amit Chauhan Commented Sep 21, 2016 at 5:55
  • Possible duplicate, i think it's answered here wordpress.stackexchange/questions/239849/… – Ahmed Fouad Commented Sep 21, 2016 at 9:57
Add a comment  | 

1 Answer 1

Reset to default -2

things are working by using rewrite_rules_array filter 1 :- i want to know the difference between add_rewrite_url() and rewrite_rules_array filter also i want to add array of rules in rewrite_rules_array filter but currently its not working only one of the rule is working others are not thanks

Post a comment

comment list (0)

  1. No comments so far