Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top