• Hi all,

    This is driving me insane! Hope someone experienced will help me with this. I don’t have a lot of experienced in WordPress. I have read many articles and threads in other forums, I think posting here might speed up things.

    I am trying to write a pdf plugin for a site that should return a pdf file for a post when the post url has /format/pdf at the end, such that:

    http://worksheets.mathematicsi.com/indices-worksheet/format/pdf

    …for example returns;

    http://worksheets.mathematicsi.com/indices-worksheet/?format=pdf

    I almost did it but each time I visit the url ( POST/format/pdf ) WordPress redirects to the post url instead. I was wondering whether someone might help me with a proper code. It is very simple but I don’t know how WordPress executes everything in the background. I also tried: remove_filter(‘template_redirect’, ‘redirect_canonical’); but that returns a 404 error.

    I am fine with other functions of the project it is just getting the rewrite rule POST/format/pdf to point to: POST/?format=pdf

    I also tried to get the rewrite rule variable/’query’ but was unable to. I think I can’t just use $_GET, I have to register them.

    Thanks.

Viewing 1 replies (of 1 total)
  • Thread Starter Muxh

    (@caciocode)

    This is my code:

    function add_my_rule() {
        global $wp;
    	add_rewrite_rule('(.?.+?)/format(/[0-9]+)?/?$','index.php?pagename=$matches[1]&format=$matches[3]','top');
        /*global $wp_rewrite;
        $wp_rewrite->flush_rules();*/
    }
    add_action('init', 'add_my_rule');
    
    function add_custom_page_variables( $public_query_vars ) {
    $public_query_vars[] = 'format';
    $public_query_vars[] = 'pagename';
    
    return $public_query_vars;
    
    } // End add_custom_page_variables()
    add_filter( 'query_vars', 'add_custom_page_variables' );

    I do flush the rewrite rules each time the plugin is enabled with
    flush_rewrite_rules();

    …and the rules seem to be registering fine with WordPress, just not working the way I would like them to.

Viewing 1 replies (of 1 total)

The topic ‘Using WordPress rewrite rules’ is closed to new replies.