WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Permalink structure (7 posts)

  1. foo bar
    Member
    Posted 8 years ago #

    Hello,
    what I would like to do is adding a rewrite-rule that redirects me to a certain wordpress-page and passes an additional variable, like this:

    WORDPRESS/index.php/singapore/hello

    --> index.php?page_id=67&gallery=hello

    Thats what I do now:
    add_action('rewrite_rules_array',array($this,'singaporeRewriteRules'));

    function singaporeRewriteRules($rewrite)
    {
    global $wp_rewrite;
    // add rewrite tokens
    $keytag_token = '%gallery%';
    $wp_rewrite->add_rewrite_tag($keytag_token, '(.+)', 'page_id='.get_option('singapore_page').'&gallery=');
    $keywords_structure = $wp_rewrite->root ."singapore/$keytag_token";
    $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
    return ( $rewrite + $keywords_rewrite);
    }

    This is what I get in the rewrite_rules_array:

    [index.php/singapore/(.+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?page_id=67&gallery=$matches[1]&feed=$matches[2]

    [index.php/singapore/(.+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?page_id=67&gallery=$matches[1]&feed=$matches[2]

    [index.php/singapore/(.+)/page/?([0-9]{1,})/?$] => index.php?page_id=67&gallery=$matches[1]&paged=$matches[2]

    [index.php/singapore/(.+)/?$] => index.php?page_id=67&gallery=$matches[1]

    Well, nice, but it doesn't work. What do I do?

  2. foo bar
    Member
    Posted 8 years ago #

    No ideas? :-[

  3. qrof
    Member
    Posted 8 years ago #

    This thread might help out, it helped me. Especially the part about refreshing wp_rewrite option!

  4. katesgasis
    Member
    Posted 8 years ago #

    Are adding rewrite rules or concatenating? return($rewrite + $keywords_rewrite) looks funny. Shouldn't it be return($rewrite . $keywords_rewrite) ?

  5. foo bar
    Member
    Posted 8 years ago #

    You do this because you combine two arrays, rather than two strings kategasis.

    And thank you qrof..I will read this and if it helps I will post a solution.

  6. sargant
    Member
    Posted 8 years ago #

    I found that in order to receive extra variables over GET that I had specified in a rewrite, I had to specify them as "safe" query variables like so:

    add_filter('query_vars', 'add_my_safe_query_vars');

    function add_my_safe_query_vars($original_vars)
    {
    $extras = array('foo', 'bar');
    return array_merge($original_vars, $extras);
    }

    You can then access them as get_query_var('foo') etc.

  7. foo bar
    Member
    Posted 7 years ago #

    Okay, the solution was more or less my own stupidity. You don't get the variables like you might expect in $_POST, $_GET or $_REQUEST. You have to include them as a global.

    For the example I started out with it would be

    function xyz()
    {
    global $gallery;

    //...
    }

    And also sargant was completly right - don't forget that!

Topic Closed

This topic has been closed to new replies.

About this Topic