This entry is part 3 of 7 in the Writing Your First WordPress Plugins, Basic to Advanced Series
- How to Begin Writing Your First WordPress plugin
- Structuring Your First WordPress Plugin
- Writing Your First WordPress Plugin Part 3
- Writing Your First WordPress Plugin Part 4
- Writing Your First WordPress Plugin Part 5
- Writing Your First WordPress Plugin Part 6
- Writing Your First WordPress Plugin Part 7 – Final
In part 3 of this tutorial series, I describe a couple of fundamental techniques for beginner WordPress plugin developers. This section details a very important function for WordPress plugins: how to add any kind of content to the end of a post. I show how it is done, and then also demonstrate some real world examples of where this technique is used.
The video covers several things:
- First – adding another file to our plugin to hold all “display” functions
- Second – Writing a function to display extra content at the end of a post
- Third – Real world examples of where this kind of function is used
Here’s the function we wrote:
1 2 3 4 5 6 7 8 9 | function mfwp_add_content($content) { if(is_single()) { $extra_content = ' This is my extra content'; $content .= $extra_content; } return $content; } add_filter('the_content', 'mfwp_add_content'); |
It is placed inside of the display-functions.php file that we have saved into our “includes” folder.
Download WIP Plugin
I realize that we begin to move a little bit deeper and deeper into WordPress plugin development and I like it ….even more – I’m still hungry ..
So… keep it up mate!
Cheers.
Hey Pippin,
Following the snippet you gave for appending information onto your User Bookmarks plugin, any reason this wouldn’t work?:
add_filter('upb_list_bookmarks','mouldings_ub_gf_button');
function mouldings_ub_gf_button($display) {
if(is_user_logged_in()) {
$display .= 'test';
}
return $display;
}
It doesn’t display that ‘test’ string. I’d like to have it appended onto the end of the bookmark list.
No, that will not work. The function that outputs the list, upb_list_bookmarks(), does not include a filter.
You will have to modify the plugin directly to do that.
Gotcha thanks
Hello Pippin, great website, thanks for your effort!
Is there Writing Your First WordPress Plugin Part 1 and Writing Your First WordPress Plugin Part 2 ?
Best regards
Srdjan
Yes, they are linked at the top of the post (see the gray box).
Pippin, Thanks a lot 🙂
great post sir Pippin.. you helped me a lot…
These are great tutorials. Ive been advanced theming for a while but use very few plugins, usually integrating whatever functionality a client needs into the theme itself. Dont’ know why Ive waited so long to do this… Ive done some work recently with WooCommerce and think I got some things to contribute. BTW the download on this page links to a WordPress-Stripe Integration.
Thanks!
Download link fixed!
It’s very helpful for beginners……..thanks a lot
Hello Pippin,
I download your WIP code from Tutorial #3, but it does not work.
<?php
/*our display functions for outputting information*/
function mfwp_add_content($content) {
if(is_single()) {
$extra_content = ' This is my extra content';
$content .= $extra_content;
}
return $content;
}
add_filter('the_content', 'mfwp_add_content');
Could you elaborate? In what way is it not working?
It does not show “This is my extra content”.
I even deleted my plugin. And simply copied yours. Still won’t work…
I am on the latest version of WP. That wouldn’t matter, right?
No worry now. It is my dumb mistake. I needed to click on the post to see the text. I thought it would show up at home page…
Glad to hear it is working!
good video! it helps me a lot and straight to the point 🙂
How would you add extra content to the beginning of the post?
It’s very similar. Instead of doing
$content .= $extra_content;
you can do$content = $extra_content . $content;
.Where do you assign the post’s content to “$content” in mfwp_add_content ($content). Also, you refer to “the_content” as a function in “add_filter()”, isn’t it the hook tag? Thanks for the great tutorial.
$content gets passed to the callback function automatically by WordPress.
“the_content” is an action hook and it is also a function, the_content().