Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created March 11, 2014 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/9494300 to your computer and use it in GitHub Desktop.
Save trepmal/9494300 to your computer and use it in GitHub Desktop.
<?php
add_action( 'wp_head', function() {
?><link rel="shortcut icon" href="<?php echo site_url( 'favicon.ico' ); ?>" /><?php
});
$redirect_favicon = new Redirect_Favicon();
class Redirect_Favicon {
function __construct() {
add_action( 'wp_loaded', array( &$this, 'wp_loaded' ) );
add_filter( 'rewrite_rules_array', array( &$this, 'rewrite_rules_array' ) );
add_filter( 'query_vars', array( &$this, 'query_vars' ) );
add_action( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
}
// flush_rules() if our rules are not yet included
function wp_loaded() {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['^favicon.ico$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function rewrite_rules_array( $rules ) {
$newrules = array();
$newrules['^favicon.ico$'] = 'index.php?favicon';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function query_vars( $vars ) {
array_push( $vars, 'favicon' );
return $vars;
}
function pre_get_posts( $query ) {
if ( is_admin() ) return;
if ( ! isset( $query->query_vars['favicon'] ) ) return;
wp_safe_redirect( plugins_url('favicon.ico', __FILE__ ) );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment