WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to make variables in URL look like the permalink structure? (10 posts)

  1. frankpereiro
    Member
    Posted 2 years ago #

    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.

    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 http://planeta-beisbol.com/tal-dia-como-hoy/dia/18/mes/04 or http://planeta-beisbol.com/tal-dia-como-hoy/18/04

    I've read the codex but still can't find the solution. They explain the permalinks for pages and posts but not for custom GET variables in the URL...

    Any help is apreciated. Thanks.

  2. Peter Baylies
    Member
    Posted 2 years ago #

    Frank,

    Here's a tutorial explaining how to do this with WordPress; it also might be possible to have your webserver do this for you before the query even gets to WordPress, for example by using rewrite rules in your .htaccess file if you're using Apache.

  3. frankpereiro
    Member
    Posted 2 years ago #

    Thanks Peter, but that din't help me much.

    So far I got this:

    function add_directory_rewrite() {
    add_rewrite_tag("%dia%", '(.+)');
    add_rewrite_tag("%mes%", '(\d+)');
    add_rewrite_rule('^tal-dia-como-hoy/(.+)/(\d+)', 'index.php?p=3393/?dia=$matches[1]&mes=$matches[2]', 'top');
    }
    
    add_action( 'init', 'add_directory_rewrite' );

    and for testing, I got this code in my page:

    echo get_query_var("dia").'/'.get_query_var("mes");

    In my page it retreaves correctly "18/04" but if I follow this URL "planeta-beisbol.com/tal-dia-como-hoy/18/04" it sends me straight to my homepage

    But I think I'm getting there, after 2 days finally some improvement LOL

  4. Peter Baylies
    Member
    Posted 2 years ago #

    Frank,

    instead of a / and another ? in your rewrite rule I think that should be an &, and shouldn't you be interpolating variables there? See if this helps:

    add_rewrite_rule('^tal-dia-como-hoy/(.+)/(\d+)', "index.php?p=3393&dia={$matches[1]}&mes={$matches[2]}", 'top');
  5. frankpereiro
    Member
    Posted 2 years ago #

    Same thing, Pete. Doesn't give me any errors but still shows the homepage.

    I think we're close, but what could it possibly be?

  6. Peter Baylies
    Member
    Posted 2 years ago #

    Frank,

    I may be mistaken about interpolating here; read through these posts for a better example.

  7. frankpereiro
    Member
    Posted 2 years ago #

    Pete, I got this stuff finally working, dude...

    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

    Greetings, Pete and everybody. I hope this code is useful for everybody

  8. Peter Baylies
    Member
    Posted 2 years ago #

    Nice job, glad to hear you got it working!

  9. frankpereiro
    Member
    Posted 2 years ago #

    Maybe it would be better to have this rewrite business as a plugin rather than having it in the functions.php?

    I mean, if I switch themes, I would need to write this function again whereas with a plugin I will never forget

  10. Peter Baylies
    Member
    Posted 2 years ago #

    Frank,

    That's what I generally tend to do as well.

Topic Closed

This topic has been closed to new replies.

About this Topic