How to make variables in URL look like the permalink structure?

I’ve been looking for the answer to this everywhere but with no success and that’s why I’m asking here.

I don’t even know if the question is the right one.

Read More

In any case, I’ll try to explain.

I have a WordPress page with variables in the URL, like this: http://planeta-beisbol.com/tal-dia-como-hoy/?dia=18&mes=04

The content changes depending on the day and month of the year because is gathered from the databse.

My question is: How can I change the URL to look something like planeta-beisbol.com/tal-dia-como-hoy/dia/18/mes/04 or planeta-beisbol.com/tal-dia-como-hoy/18/04

Any help is apreciated. Thanks.

Related posts

Leave a Reply

3 comments

  1. The following two functions should point you in the right direction:

    function wpse49393_query_vars($query_vars)
    {
        $query_vars[] = 'myvar';
        return $query_vars;
    }
    add_filter('query_vars', 'wpse49393_query_vars');
    
    function wpse49393_rewrite_rules_array($rewrite_rules)
    {
        $rewrite_rules['path/to/page/([^/]+)/?$'] = 'index.php?pagename=path/to/page&myvar=$matches[1]';
        return $rewrite_rules;
    }
    add_filter('rewrite_rules_array', 'wpse49393_rewrite_rules_array');
    

    I would give credit where it is due, but I cannot remember where I originally found this code. I just pulled this out of a project that I am currently working on. To use it, change myvar to the name of your variable in the first function as well as here: &myvar=$matches[1]. Change path/to/page/ to the actual path of your page at two locations in the second function.

  2. Thanks to this post for the heads up: http://zagar.biz/2011/wordpress-creating-custom-permalinks/

    My final code ended up being like this:

    In the theme’s functions.php file

    add_filter('rewrite_rules_array','mycode_add_rewrite_rules');
    
    // Add all rewrite rules here
    // ** Remember to Flush Rewrite rules when adding new rules.
    // this is done by visiting the Settings > Permalinks in WP admin.
    
    function mycode_add_rewrite_rules($rules){
        $newrules = array();
        $newrules['tal-dia-como-hoy/([^/]+)/([^/]+)/?'] = 'index.php?pagename=tal-dia-como-hoy&dia=$matches[1]&mes=$matches[2]';
        return $newrules + $rules;
    }
    
    add_filter('query_vars','mycode_add_rewrite_query_vars');
    
    // Add all slugs so that WP recognizes it
    function mycode_add_rewrite_query_vars($vars){
        array_push($vars, 'dia');
        array_push($vars, 'mes');
        return $vars;
    }
    

    Then, in the page I created. This is just a regular wordpress page, not a template or anything like that

    global $wp;
    
    // connect to your external DB, etc...
    
    if($wp->query_vars["dia"]){
    
    echo "Permalinks are working";
        // load up your external data here and display single resource
            // if the beard slug doesn't exist, redirect to 404
    } else {
        // load up whatever you want to display for the index version.
    echo "Nothing shows up but it's working anyway";
    }
    

    I have to edit the page to make it look good with my brand new permalinks.

    This is the page I’ll using with this code: http://planeta-beisbol.com/tal-dia-como-hoy/19/04/

    The “19/04” part means: 19th of April

    I hope this code is useful for everybody