WordPress.org

Support

Support » How-To and Troubleshooting » Where to insert add_rewrite_rule?

Where to insert add_rewrite_rule?

  • Sorry, this problem was discussed, and rezolved with the code:

    function add_my_var($public_query_vars) {
        $public_query_vars[] = 'myvar';
        return $public_query_vars;
    }
    
    add_filter('query_vars', 'add_my_var');
    
    function do_rewrite() {
        add_rewrite_rule('mypage/([^/]+)/?$', 'index.php?pagename=mypage&myvar=$matches[1]','top');
    }
    
    add_action('init', 'do_rewrite');

    Where should I add that code? What file should I use and what section?

    I have a plugin installed that sets the URL’s like this: wordress/pagename/?var=xt
    i want it to be like:
    wordpress/pagename/xt

    Where wordpress is the wordpress base (http://domain.com/wordpress/)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Samuel Wood (Otto)
    WordPress.org Tech Dude

    @otto42

    Where should I add that code? What file should I use and what section?

    You should add it into a plugin or into your theme’s functions.php file.

    Preferably you would put it in the plugin that it is affecting.

    Done that, but it says

    Error 404 – Not Found

    I’m using WordPress 2.6.3
    help 🙁

    I’m using version 2.7.1

    As I look in ./wp-includes/classes.php:145

    $rewrite = $wp_rewrite->wp_rewrite_rules();

    Look in ./wp-includes/rewrite.php:1598

    function wp_rewrite_rules() {
    		$this->rules = get_option('rewrite_rules');
    		if ( empty($this->rules) ) {
    			$this->matches = 'matches';
    			$this->rewrite_rules();
    			update_option('rewrite_rules', $this->rules);
    		}
    
    		return $this->rules;
    	}

    So, they only use the cached rules. If we want to use our rules, we should update the cache. For example, I update in my options page when $_GET[‘updated’] == ‘true’

    PS: I also call add_rewrite_rule in init in case other plugin update cache later 😀

    Hope it helps

    serdominik
    Member

    @serdominik

    How to update cache?

    better explained

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Where to insert add_rewrite_rule?’ is closed to new replies.