0

I have found quite a lot of post on the subject of WP rewrite but I can't find the right words to describe this problem.

I'm trying to accomplish the following: mysite.com/examentraining/vak/biologie/havo To be rewritten to /examentraining/vak?vakname=biologie&level=havo

So when the user types mysite.com/examentraining/vak/biologie/havo into their adress bar they should be shown the mysite.com/examentraining/vak/ page, but the application should be able use the last two value's between slashes as get parameters (so for the shortcode script the url must look like /examentraining/vak?vakname=biologie&level=havo)

I have tried the http://codex.wordpress.org/Function_Reference/WP_Rewrite. But the example closest to mine uses an number and only on variable. And at this point the syntax doesn't make enough sense to me to adapt it to my needs. Also when I want to use two variable do I have to call my_insert_query_vars( $vars ) twice?

I hope there isn't a question exactly like mine that I have not found.

EDIT after more than 2 weeks I still have not solved the problem.

I have made the following but it doesn't seem to work, the rules are imported correctly (checked with wordpress plugin, the plugin even says the rule matches the request url). But it doesn't rewrite.

 // Adding a new rule
    function my_insert_rewrite_rules($rules)
    {
        $newrules = array();
        $newrules['examentraining/training/([0-9]{1,})/?$'] = 'index.php/?p=1320&id=$matches[1]';   
        return $newrules + $rules;
    }

// Adding the id var so that WP recognizes it
    function my_insert_query_vars($vars)
    {
        array_push($vars, 'id');
        return $vars;
    }

I tried one of the simpler examples first, I want this code to rewrite example.com/examentraining/training/513/ to example.com/?p=1320&id=513 So I want to open the page with the page id of 1320 (this works) and pass the id parameter to this page so my php code can use the variable either via $wp_query->query_vars['trainingid'] or via $_GET['trainingid'] (visiting the url /?p=1320&id=513 works correctly). The only thing that changes in this url is the last part with the id (513 in the example).

Could anybody tell me what is wrong with my rewrite rule?

EDIT 2

I have since read that it is not smart to use such a generic get parameter as id with wordpress so I changed that and I tried another way of getting the page (Als works when called directly from wordpress)

/ Adding a new rule
    function my_insert_rewrite_rules($rules)
    {
        $newrules = array();
        $newrules['examentraining/training/([0-9]+)/?$'] = 'index.php?pagename=examentraining/training&trainingid=$matches[1]';
        return $newrules + $rules;
    }

// Adding the id var so that WP recognizes it
    function my_insert_query_vars($vars)
    {
        array_push($vars, 'trainingid');
        return $vars;
    }

For the sake of testing I flush my rules every init.

I will keep searching but I really hope somebody can point me in the right direction.

9
  • What is /examentraining/vak/?
    – s_ha_dum
    Feb 8, 2014 at 14:25
  • @s_ha_dum /examentraing/vak/ leads to a page on my wordpress website, and it should be kept that way. Feb 8, 2014 at 17:37
  • I assumed it was a "page" on your site of some kind, and I am not trying to change that, but I am trying to work out what "kind" of page it is. It is a proper page post type page?
    – s_ha_dum
    Feb 8, 2014 at 17:38
  • @s_ha_dum It's an post, just your standard post with some information and a shortcode that want's to use the get parameters but still wants pretty url's. Feb 9, 2014 at 19:21
  • 1
    "...still wants pretty url's." -- so you want this, mysite.com/examentraining/vak/biologie/havo, and not this /examentraining/vak?vakname=biologie&level=havo? Your question strongly suggests just the opposite.
    – s_ha_dum
    Feb 9, 2014 at 19:24

1 Answer 1

0

I have finally solved my rewrite rule problem. I used the class outlined in the answer of this wordpress.stackoverflow question. After including that class I used the following code to make it work:

 // These query vars can be retrieved on the page by using get_query_var( 'queryvar1' );
    $options = array(
            'query_vars' => array(
                'queryvar1',
                'queryvar2',
                'queryvar3',
                'queryvar4',
                'queryvar5',
                'queryvar6'
            ),
            'rules'      => array(
            'uncategorized/somename/([^/]+)/?$'=> 'index.php?p=1020&ident=$matches[1]'

            )
        );
        $rewrite = new AddRewriteRules( $options );
        add_action( 'wp_head', array(
            &$rewrite,
            'flush_rules'
        ) );
        add_action( 'generate_rewrite_rules', array(
            &$rewrite,
            'add_rewrite_rules'
        ) );
        add_filter( 'query_vars', array(
            &$rewrite,
            'add_query_vars'
        ) );

I hope this is useful for somebody with the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.