Skip to content

Instantly share code, notes, and snippets.

@alettieri
Last active December 14, 2015 06:29
Show Gist options
  • Save alettieri/5042590 to your computer and use it in GitHub Desktop.
Save alettieri/5042590 to your computer and use it in GitHub Desktop.
URL Rewriting in WordPress
<?php
/* Custom Rewrite rules for video embedding */
function flush_player_rules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter("wp_loaded","flush_player_rules");
function add_player_rewrite($rules){
$newrule = array();
$newrule['(video-embed)/(\d*)$'] = 'index.php?pagename=$matches[1]&vid_id=$matches[2]';
return $newrule + $rules;
}
add_filter("rewrite_rules_array","add_player_rewrite");
function add_player_vars($vars){
array_push($vars,"vid_id");
return $vars;
}
add_filter("query_vars","add_player_vars");
<?php
// Getting the value, you should be able to use $wp_query->query_vars property
// http://codex.wordpress.org/Class_Reference/WP_Query
if( isset($wp_query->query_vars['vid_id']) ) {
$id = $wp_query->query_vars['vid_id'];
// ...do things with the $id
}
// OR http://codex.wordpress.org/Function_Reference/get_query_var
$id = get_query_var( "vid_id" );
if( $id ) {
// ...do things with the $id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment