WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im trying to add a rewrite rule to make this mysite/?attachment_id=106

view like this mysite/series/106

Ive looked evetywhere in this site and others and its very confuse because there are lots of diferent ways to do it. Ive tried is editing functions.php file in my themes folder, adding this at the begining of the file:

add_rewrite_rule('series/([^/]+)/?$', 'index.php?attachment_id=$matches[1]', 'top');

And the latest one ive tried

add_filter( 'query_vars', 'wpa56345_query_vars' );

function wpa56345_query_vars( $query_vars ){
    $query_vars[] = 'attachment_id';
    return $query_vars;

add_action( 'init', 'wpa56345_rewrites' );

function wpa56345_rewrites(){

Please apologize my english (im from Argentina), and apologize if this was already answered. Thanks

share|improve this question
Your english seems as good as your coding instincts, kudos for that and for the good question, and welcome to WPSE :o) – brasofilo Jun 25 '12 at 3:15
up vote 2 down vote accepted

Place the code suggested below in functions.php of your desired theme, it worked for me with WP 3.4: http://matty.co.za/2009/11/custom-url-rewrites-in-wordpress/

share|improve this answer
thanks it worked! – federomano Jul 13 '12 at 0:13

This seem to work for me:

// Add your query variable

add_filter( 'query_vars', 'my_query_vars' );
function my_query_vars( $query_vars ) {
    array_push($query_vars, 'attachment_id');
    return $query_vars;

// Add your rule

add_filter( 'rewrite_rules_array', 'my_rewrite_rules_array' );
function my_rewrite_rules_array( $rules ) {
    $my_rules = array();
    $my_rules['series/(\d+)/?$'] = 'index.php?attachment_id=$matches[1]';
    return $my_rules + $rules;

// Flush the rules if your rule does not exist

add_action( 'wp_loaded', 'my_flush_rules' );
function my_flush_rules() {
    if (!$rules = get_option('rewrite_rules'))
        $rules = array();

    if (!isset($rules['series/(\d+)/?$'])) {
        global $wp_rewrite;

And also, if you want to debug rules, there's a useful snippet:

// Uncomment add_action to see what's happening

//add_action('wp', 'debug_rules');
function debug_rules() {
    global $wp, $wp_query, $wp_rewrite;
    echo $wp->matched_rule . ' | ' . $wp_rewrite->rules[$wp->matched_rule];

Hope it helps.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.