WordPress: automatically append text to the content of every post or page

Wordpress help

Sometimes you want to include a short note under all posts in your blog - without installing an additional plugin. For me the rule applies: What you can do without a plugin, you should do without, because every plugin inflates WordPress again and the installed plugins often have a significant overhead of functions that you actually don't need. Another point that speaks against more and more plugins is of course the security of WordPress. Most attacks by hackers run through outdated or insecure-programmed WordPress plugins. The WordPress core is usually very secure if you take care of the updates.

Solution via functions.php

I wanted to include an addition under the articles in my blog that indicates that I participate in the Amazon partner program. According to Amazon's new regulation in 2018, the phrase "As an Amazon partner, I earn from qualified purchases." be set up on every page that has Amazon advertising. Since I didn't want to edit all of these pages individually, I just add the note under each post.

In order to add such small functions, WordPress offers the possibility to include the corresponding PHP code snippets in the folder of the theme in the file "functions.php". If you don't already have this file, it's best to create it and copy the code below into it.

The "the_content" hook makes it possible

With "hooks", WordPress offers flexible options to expand or change the functions that run in WordPress. We now use the the_content hook to change the content of the post or page before it is displayed on the website. So that the addition is only built into pages and articles and not displayed in category overviews or the like, there are also queries in_the_loop() and is_main_query() for use. Here is the finished script:

add_filter ('the_content', 'filter_the_content_in_the_main_loop'); function filter_the_content_in_the_main_loop ($ content) {// Check whether we are in the loop of a post or a page if ((is_single () OR is_page ()) && in_the_loop () && is_main_query ()) {// The HTML part for the font you can change or expand as you like return $ content. ' Note: As an Amazon partner, I earn from qualified purchases. '; } return $ content; }

If you only want the reference to articles and not to pages (such as imprint or similar), then change the if statement as follows:

if ( is_single() && in_the_loop() && is_main_query() ) {

If you have any questions about the usage or suggestions for improving the code, I look forward to your comment.

-
Do you like my blog? Then I would be happy to receive a short review on Google. Easy leave something here for a moment - that would be great, thank you!

8 comments

  1. Frank says:

    Thank you for your contribution - that's exactly what I was looking for.
    Unfortunately it doesn't work for me.
    But I also have little idea about the subject.
    If I compare it with other function calls, they still have an add_action () or something similar with them.
    If you have a tip, I would be happy to hear from you.
    Thank-you! :-)
    Frank

    • Sir Apfelot says:

      Hi Frank! 1000 thanks for your hint. In fact, the "add_filter ()" call was missing. Without it, the function is not built into the hook. I added it. Please try if it works with the new code! LG! Jens

  2. sandra says:

    Hello Frank,
    thank you very much for your contribution! That's exactly what I was looking for :)
    I still have one question: Is there a way to read the focus keyword from Yoast and insert it into the text above?
    Eg note: As an Amazon partner, I earn money from qualified purchases from [Focus keyword] (e.g. Schaltraum).
    Best regards,
    sandra

    • Jens Kleinholz says:

      Hi Sandra! I think all the variables and also the focus keyword can only be used in the snippet. So in meta title and meta description. With PHP programming you might be able to extract it and somehow put it in the sentence, but I have no idea how to do that.

  3. Norbert says:

    Hi Jens,
    a question about this PHP snippet. When I insert this code, a text is displayed under posts (so far so good), but also under Woocommerce product pages. This text shouldn't actually be there. How do I have to adapt the code. Thank you very much.

    gruß
    Norbert

    • Jens Kleinholz says:

      Hello Norbert! Good question, next question! : D I have no clue about WooCommerce. I suppose you would have to add a query like "if (is_page ())" or something like that, but with the detection of whether it is a WooCommerce product page. But unfortunately I don't know exactly how that works, because I don't know the WooCommerce peculiarities ... :(

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * .