Support » Plugins and Hacks » Hacks » How to make variables in URL look like the permalink structure?

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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.

    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

    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');

    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?

    Frank,

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

    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

    Nice job, glad to hear you got it working!

    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

    Frank,

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

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to make variables in URL look like the permalink structure?’ is closed to new replies.