1

I'm wondering how I can modify a function without touching the template file.

The following function is called in my theme in the template-functions.php file:

    if( ! function_exists( 'book_landing_page_footer_credit' ) ) :
/**
 * Footer Credits 
 */
function book_landing_page_footer_credit(){

    echo '<div class="site-info">';
    esc_html_e( 'Copyright &copy;&nbsp;', 'book-landing-page' ); 
    echo esc_html( date_i18n( 'Y' ) );
    echo ' <a href="' . esc_url( home_url( '/' ) ) . '">' . esc_html( get_bloginfo( 'name' ) ) . '&#46;&nbsp;</a>';
    printf('<a href="%1$s">Book Landing Page By %2$s</a>&#46;&nbsp;', esc_url( __( 'http://raratheme.com/wordpress-themes/book-landing-page/', 'book-landing-page' ) ), 'Rara Theme');
    printf( esc_html__( 'Powered by %s', 'book-landing-page' ), '<a href="'. esc_url( __( 'https://wordpress.org/', 'book-landing-page' ) ) .'" target="_blank">WordPress&#46;</a>' );
    echo '</div>';

}
endif;

Which is referenced in the template-hooks.php file like so:

add_action( 'book_landing_page_footer', 'book_landing_page_footer_credit', 40 );

My question is, how can I modify that function without touching the template files?

I've tried the following but it doesn't work:

if (!function_exists('new_credit')) {
    function new_credit() {
        echo 'This is my site';
    }
    add_filter('book_landing_page_footer_credit', 'new_credit');
}

1 Answer 1

1

Oviously, you'll have to edit something if you want to modify it. So, your best course of action is to create a child theme, with its own function file.

A filter, as you tried, won't work, because there is no filter hook in the function that you are trying to modify. What you can do, is remove the action that is printing the footer and then add a new action on the same hook. Like this:

add_action ('wp_head','wpse252108_remove_add_action');

function wpse252108_remove_add_action() {
  remove_action ('book_landing_page_footer', 'book_landing_page_footer_credit', 40);
  add_action ('book_landing_page_footer', 'new_credit', 40)
  }

Because it's unclear where in the flow template-hooks.php, the place where the original action is added, is loaded, it is possible that wp_head is too early to do the add/remove action. Perhaps wp_footer is better, though that in turn might be too late.

1
  • The template file template-hooks.php is actually called in the themes function.php file. I tried using wp_footer but that didn't work...wp_head worked just fine.
    – brandozz
    Commented Jan 11, 2017 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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