Site icon WP Smith

How to Replace Functions Regardless of the Genesis Hook (via Gary Jones)

Recently, Gary Jones (@GaryJ) posted a bit of code for Jared Atchison (@jaredatch) on Twitter for a plugin that he is working on that will remove functions regardless of where they get hooked. It is a great piece of code. If you find this helpful, please support Gary or tell him how much you appreciate him.

[php]<?php
// Run at get_header to catch all customisations in functions.php

add_action( 'get_header', 'jared_remove_stuff_whichever_hook_they_are_on' );
/**
* Replace some genesis_* functions hooked into somewhere for some jared_* functions
* of the same suffix, at the same hook and priority
*
* @author Gary Jones
*
* @global array $wp_filter
*/
function jared_remove_stuff_whichever_hook_they_are_on() {

global $wp_filter;

// List of genesis_* functions to be replaced with jared_* functions.
// We save some bytes and add the ubiquitous 'genesis_' later on.
$functions = array(
'do_doctype',
'do_nav',
'do_subnav',
'header_markup_open',
'header_markup_close',
'post_info',
'post_meta',
'do_loop',
'footer_markup_open',
'footer_markup_close'
);

// Loop through all hooks (yes, stored under the $wp_filter global)
foreach ( $wp_filter as $hook => $priority) {

// Loop through our array of functions for each hook
foreach( $functions as $function) {

// has_action returns int for the priority
if ( $priority = has_action( $hook, 'genesis_' . $function ) ) {

// If there's a function hooked in, remove the genesis_* function
// from whichever hook we're looping through at the time.
remove_action( $hook, 'genesis_' . $function, $priority );

// Add a replacement function in at the same time.
add_action( $hook, 'jared_' . $function, $priority );
}
}
}

}[/php]