⚡ Rocket.net – Managed WordPress Hosting

MiltonMarketing.com  Powered by Rocket.net – Managed WordPress Hosting

Bernard Aybouts - Blog - MiltonMarketing.com

Approx. read time: 27.5 min.

Post: Extending WordPress with Plugins

Creating Events with Actions

Now that you’ve created your administrative panel and have stored your copyright text in the database, you can use it within WordPress. The most obvious place for this is in the footer of your theme.

Fortunately, most themes come with a wp_footer action that you can use to display the copyright text. But first, you need to write a function that will display the copyright text. See code below:

[html] function display_copyright()
{
if( $copyright_text = get_option(‘copyright_notices_text’ ) )
{
echo ‘

’ . $copyright_text . ‘

’;
}
}

[/html]

Because you are using an action hook to accomplish the task, there is no need to pass any arguments in the function. This is a distinct difference from filters that require an argument to be passed.

Now that you have created a semi-useful function for adding copyright text, you can add it to the theme via an action. For this, we use the action hook wp_footer like this:

[html] add_action(‘wp_footer’,’display_copyright’);[/html]

When you hook the display_copyright() function to the wp_footer hook, the function is added to the queue, to be executed when the wp_footer hook is fired in the header of the theme.

In order for this to work, the wp_footer(); function (which is a wrapper around the wp_footer action hook) must be included in the theme. The final code for this example looks like the code shown below:

[html] function display_copyright()
{
if( $copyright_text = get_option(‘copyright_notices_text’ ) )
{
echo ‘

’ . $copyright_text . ‘

’;
}
}
add_action(‘wp_footer’,’display_copyright’);[/html]

Modifying Content with Filters

The second kind of hook is called a filter. Filters, as their name suggests, take data, do something to it, and return modified data back. They are useful if you want to do something such as add an attribution notice to the end of every post or generate custom content for feeds.

As an example, you’ll see you how to add some copyright text to the end of every feed item to ensure that anyone using the feed cannot do so without some kind of enforced copyright notice (effective in combating spam blogs that scrape content and re-purpose it as their own).

The function for adding this code is shown below:

[html] function display_copyright_feed( $post_content )
{
if( !$copyright_text = get_option(‘copyright_notices_text’) )
return $post_content;
return $post_content . $copyright_text;
}[/html]

This function simply adds the attribution line to the end of the content and, for all intents and purposes, will work fine. Instead, though, you want to ensure that it only displays when the_content() function (which includes the_content filter) is executed in a feed. You do that by using conditional logic with is_feed(), as shown below:

[html] function display_copyright_feed( $post_content )
{
if( !$copyright_text = get_option(‘copyright_notices_text’) || !is_
feed() )
return $post_content;
return $post_content . $copyright_text;
}[/html]

WordPress has several expected coding conventions. They are enforced throughout the core and are often adopted by plugin and theme authors as well. The sample code shown earlier demonstrates one of those coding conventions by checking if the conditional logic is not true.

Checking for false (as in “this post is not in category 4”) is often easier and cleaner than checking for true. In this case, you performed a conditional check to ensure that the content was not in a feed.

If it is in a feed, you return the content exactly as you got it. However, if it is not in a feed, then you proceed to return the post content with the attribution line appended to the end. Finally, you need to hook this function to a hook like this:

[html] add_filter(‘the_content’,’display_copyright_feed’);[/html]

As with an action, hooking the display_copyright_feed() function to the the_content hook causes it to be fired every time the_content is fired in the Loop, the mechanism used by WordPress to generate and iterate over posts. Because this is a filter, the content_attribution() function expects the post content to be passed to it and it returns modified content back into the Loop.

The Loop is an integral part of WordPress. It is where the magic involving printing blog posts and content into a theme happens. For now, just understand that it is where WordPress processes the requirements for the selection of posts, fetches them from the database and returns the data to WordPress for processing. It is, notably, how WordPress is able to have a number of posts on one page, rendering the blog in expected reverse-chronological order.

The final code block for this section looks like the code shown below: (Using a filter to add a copyright notice to the end of every post.)

[html] function display_copyright_feed( $post_content )
{
if( !$copyright_text = get_option(‘copyright_notices_text’) || !is_
feed() )
return $post_content;
return $post_content . $copyright_text;
}
add_filter(‘the_content’,’display_copyright_feed’);[/html]

About the Author: Bernard Aybout (Virii8)

Avatar of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀