0

If I want to change the footer credits, I would usually copy the footer.php to a child theme and edit the following snippet:

<div class="powered-by">
    <?php
    printf(
        /* translators: %s: WordPress. */
        esc_html__( 'Powered by %s.', 'WordPress' ),
        '<a href="' . esc_url( __( 'https://wordpress.org/', 'WordPress' ) ) . '">WordPress</a>'
    );
    ?>
</div>

I'm trying to learn how to use hooks in WordPress and was wondering how I could do the same thing I did above but with the help of custom hooks in functions.php. What would be the right way to go about it?

3
  • 2
    That snippet contains no hooks, so there isn’t anything for you to hook onto. IMO using the built-in template hierarchy with a child theme is the simplest way to do this. When you or anyone else comes back to the theme later, it’s then obvious how your change works. If you have a really compelling reason to do this in your functions file you could possibly hook into get_template_part somehow but it seems a pretty convoluted way to do something that the template hierarchy does for you already. – Andy Macaulay-Brook Mar 31 at 10:23
  • @AndyMacaulay-Brook Oh I see. That makes sense. Can you add that as an answer so I could accept it. A dummy example of a snippet with something to hook on to vs the above with nothing to hook on to would be great. Thanks! – AndrewL64 Mar 31 at 10:25
  • added as an answer – Andy Macaulay-Brook Mar 31 at 11:02
1

That snippet contains no hooks, so there isn’t anything for you to hook onto.

If instead the file had something like:

<div class="powered-by">
    <?php
        echo apply_filters( 'credit-text', 'Powered by <a href="https://wordpress.org/">WordPress</a>' );
    ?>
</div>

Then you could do something like:

add_filter( 'credit-text', 'wpse385946_credit_text' );

function wpse385946_credit_text( $credit ) {

    return 'Site by <a href="/">Me!</a>';

}

There's an added complication with the translation functions in the original theme: you don't want to use variables in them if the text in the variable might need translating. If your theme is for a private site rather than for publication, then translation might not be an issue for you.

IMO using the built-in template hierarchy with a child theme is the simplest way to do this. When you or anyone else comes back to the theme later, it’s then obvious how your change works. If you have a really compelling reason to do this in your functions file you could possibly hook into get_template_part somehow but it seems a pretty convoluted way to do something that the template hierarchy does for you already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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