WordPress.org

Ready to get started?Download WordPress

Forums

Where to insert add_rewrite_rule? (5 posts)

  1. Bughy
    Member
    Posted 5 years ago #

    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/)

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    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.

  3. Bughy
    Member
    Posted 5 years ago #

    Done that, but it says

    Error 404 - Not Found

    I'm using WordPress 2.6.3
    help :(

  4. mrpaint
    Member
    Posted 5 years ago #

    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 :D

    Hope it helps

  5. serdominik
    Member
    Posted 5 years ago #

    How to update cache?

    better explained

Topic Closed

This topic has been closed to new replies.

About this Topic