7

When I go to the url mysite.com/photos/120 I'm wanting to get the 120 out.

In the PHP I want to be able to grab these "parameters" like the url was mysite.com?page=photos&id=120 or even just mysite.com/photos?id=120

Can I leverage rewrite rules? Or do I need to do regex in php to get what I want?

**** EDIT 1 ****

For the life of me I can't get this to work. Based on the answers given here is what I have so far:

add_action( 'init', 'rewrite_photo_url' );
function rewrite_photo_url() {
    add_rewrite_rule( '^photos/([^/]*)/?','index.php?page=photos&photo_id=$matches[1]', 'top' );
}

add_rewrite_tag('%id%','([0-9]+)');

print(get_query_var('photo_id'));

I suspect I'm missing a concept somewhere?

**** EDIT 2 ****

I'm starting to see that perhaps this needs to be in functions.php so I now have:

function rewrite_photo_url() {
    add_rewrite_rule( '^photos/([^/]*)/?','index.php?page=photos&photo_id=$matches[1]', 'top' );
}
function register_custom_query_vars( $vars ) {
    array_push( $vars, 'photo_id' );
    return $vars;
}

add_action( 'init', 'rewrite_photo_url');
add_filter( 'query_vars', 'register_custom_query_vars', 1 );

Now I just need to know how to get my desired var in my page template. I've tried

print(get_query_var('photo_id'));

But that's not doing the trick.

3
  • The only thing worse than no answers is three good answers. #dilemma
    – Jacksonkr
    Apr 11, 2012 at 4:14
  • 1
    You're mixing the answers... you use photo_id as your variable in the rewrite rule, but add id instead of photo_id with add_rewrite_tag()!
    – EAMann
    Apr 11, 2012 at 15:58
  • The add_rewrite_tag should probably be inside the rewrite_photo_url callback... Apr 11, 2012 at 16:00

5 Answers 5

12

You can add your own rewrite rule which will let you tweak the query via URL parameters:

add_action( 'init', 'rewrite_photo_url' );
function rewrite_photo_url() {
    add_rewrite_rule( 'photos/([^/]+)/?$','index.php?page=photos&photo_id=$matches[1]', 'top' );
}

If you need to use a custom variable, i.e. 'photo_id', you have to register the variable so it will be recognized by the query:

add_filter( 'query_vars', 'register_custom_query_vars', 10, 1 );
// 1 is the arg number 
function register_custom_query_vars( $vars ) {
    array_push( $vars, 'photo_id' );
    return $vars;
}
4
  • You don't want the caret in your rewrite rule up there. WordPress prefixes all rewrite rules with one for you; also, you might want to add a $ at the end so WordPress can still add endpoint permalinks if necessary. Also, I'd change the asterisk to +, since that will only get a match if there really is a photo id present. Other than that, all solid code. :D Apr 11, 2012 at 13:56
  • soo I think I finally figured out that this needs to be in my theme's functions.php -- well now I think everything is working, but I'm not sure how to retrieve my value on the actual photos.php. Should it just be print(get_query_var('photo_id')); anywhere on the page?
    – Jacksonkr
    Apr 11, 2012 at 16:12
  • print( get_query_var( 'photo_id' ) ); should work. Are you sure that your template is the one being used? A quick test of echo-ing some static text near the get_query_var() should do the trick. Apr 12, 2012 at 1:49
  • Thanks Rachel, your answer helped me a lot. Thanks a ton :) Sep 13, 2014 at 16:22
5

You can use add_rewrite_tag to register your custom query variable ('id' in question):

add_rewrite_tag('%id%','([0-9]+)');

(The regex tells it only to accepts digits). Then to create your rewrite rule you can use add_rewrite_rule

(both of these should be hooked onto init).

add_rewrite_rule('^photos/([0-9]+)/?','index.php?p=1234&id=$matches[1]','top');

where 1234 is the ID of your 'photos' page. You will need to flush rewrite rules once after adding these (go to the Permalink settings page). Then as @DanielBachhuber says, you can use out get_query_var( 'id' ) to get the ID.

Note, while the regex in the add_rewrite_tag means this will only accept digits - you should probably still sanitize with intval (in any case it may be a string representation of a digit).

0
2

Depending on how the rules are being generated, you can use the get_query_var() function to get the value of the 'photo' query var. If it's done properly, 'photo' should be an available query var. You'll need to sanitize the value with intval() or similar of course.

1

After many days of doing research on this, the answer was this simple:

$wp_query->query_vars['page']

wrap that in an intval and viola.

0

Hi Guys this is what worked for me:

    $pagination_args = array(
    'base'            => user_trailingslashit( trailingslashit( remove_query_arg( 's', strtok(get_pagenum_link( 1 ), '?') ) ) . 'page/%#%/'.rtrim(str_replace('%2F','',substr(get_pagenum_link( 1 ),strlen(strtok(get_pagenum_link( 1 ), '?')))),'/'), 'paged' ),
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('«'),
    'next_text'       => __('»'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
);

The base value is what really works the magic. No need to make any changes to the base value. I was pointed in the right direction by reading this post: https://wordpress.org/support/topic/plugin-simple-pagination-page2-and-_get-parameters

1
  • Please do not add duplicate content. It's enough to add your answer to one question. If you think both questions are the same, then please flag them for closing. Thank you.
    – kaiser
    Mar 6, 2015 at 0:05

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.