skip to Main Content

I want to add a disclaimer text to every post with a link. From what I’ve seen before, the basic format is below:

add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {

    // Check if we're inside the main loop in a single post page.
    if ( is_single() && in_the_loop() && is_main_query() ) {

       return esc_html__("This website is intended to inform and educate, and shouldn't be taken as financial advice. It also uses affiliate links, including those from Amazon. If you make a purchase via one of the links marked with an *, we might receive a small commission. Find out more here.").$content;
    }

    return $content;
}

What’s the extra bit of code I need to turn "Find out more here" into a link to a specific page on the website?

I haven’t seen anything that includes a link as well as text.

2

Answers


  1. You can edit your return string to include an hyperlink (or any HTML), like this:

    return ("This website is intended to inform and educate, and shouldn't be taken as financial advice. It also uses affiliate links, including those from Amazon. If you make a purchase via one of the links marked with an *, we might receive a small commission. <a href='/url-of-info-page/'>Find out more here</a>.".$content;
    

    You will need to remove the esc_html part, since this will escape the HTML you are returning, rendering it useless.

    Login or Signup to reply.
  2. The links or other HTML getting removed because you are using esc_html__ which is basically escape the HTML.

    In order to fix this either you can remove esc_html__ or use wp_kses_post function.

    add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
    
    function filter_the_content_in_the_main_loop( $content ) {
    
        // Here we are checking if we're inside the main loop in a single post page.
        if ( is_single() && in_the_loop() && is_main_query() ) {
    
            // Here is the disclaimer text with a link.
            $disclaimer = "This website is intended to inform and educate, and shouldn't be taken as financial advice. It also uses affiliate links, including those from Amazon. If you make a purchase via one of the links marked with an *, we might receive a small commission. <a href='https://yourwebsite.com/your-disclaimer-page'>Find out more here</a>.";
    
            // Here we are returning the disclaimer followed by the content.
            return wp_kses_post( $disclaimer ) . $content;
        }
    
        return $content;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search