Created
March 11, 2014 20:29
-
-
Save trepmal/9494300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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