How To Update A Wp Plugin From Fillezilla
Add Text Before And After Content In WordPress Using Hooks (Complete Guide)
This mail service will help you learn how to add or modify text before or after all WordPress content like posts or pages.
The best role?
You don't need to exist a WordPress or PHP expert.
(In other words, I volition provide you with step by step instructions on how to add or modify text in WordPress posts and pages)
Let's dig right into it!
Office 1
Understanding WordPress Hooks: Actions And Filters
In this part I volition tell you lot about WordPress actions and filters.
Why?
Because these are mechanism essential to how WordPress works.
We will brand use of these mechanisms to add together text before or afterward the content of posts or pages.
If you already know what a WordPress hook is, you lot can skip to Part 2 of this guide.
If you know about how to write a WordPress plugin and simply want the plugin code for adding text before or after all WordPress posts, then skip to Part 3.
♦ WordPress: 59.nine%
♦ Joomla: 5.eight%
♦ Drupal: 3.viii%
♦ Squarespace: two.4%
♦ Magento: 2%
♦ Wix: 1.seven%
♦ Blogger: ane.v%
♦ PrestaShop: one.5%
♦ Others: 21.4%
%
Of All Websites Use WordPress
Yous probably already know this:
WordPress is i of the most used CMS covering almost sixty% of all websites using a CMS.
w3techs
Spider web Engineering science Surveys
WordPress content is made up of posts and pages. Every time a page or post is displayed, WordPress passes that content through a series of filters. Every time a user performs an activeness, WordPress besides passes that through a filter.
These two mechanisms are called WordPress hooks, and there are 2 types of hooks:
- WordPress filters
- WordPress actions
Why is this important?
We can utilise those hooks to alter how WordPress works. In our case, we can use filters to add together text before, later or fifty-fifty inside posts and pages.
All WordPress hooks have an identifier. Check the WordPress documentation for the full list of WordPress filters and actions.
Since we will be using filters to modify the text of a post or page, we will merely focus on them.
WordPress Content Filter the_content
When WordPress displays the content of a post or folio it will ever run the filter named the_content
. Nosotros can inform WordPress that we want to implement our own behavior for that filter.
From WordPress'southward documentation:
The
the_content
filter is used to filter the content of the post after it is retrieved from the database and before it is printed to the screen.A plugin (or theme) can register as a content filter with the lawmaking:
<?php add_filter( 'the_content', 'filter_function_name' ) ?>Where 'filter_function_name' is the part WordPress should phone call when the content is beingness retrieved. Note that the filter function must return the content after it is finished processing, or site visitors volition see a blank page and other plugins also filtering the content may generate errors.
filter_function_name should exist unique function name. It cannot match any other function name already declared.
This is important to empathize:
Filters are meant to work as a pipeline. One filter implementation output is the next one'southward input.
And so, in a filter, nosotros receive an input, practise our processing on that input, and laissez passer the modified input as an output for other filters.
This goes on and on until the final output is displayed on the screen.
In the next sections we will run across practical examples of filter implementations for adding and modifying text earlier and afterwards post and pages content.
Part 2
Two Methods To Add A WordPress Custom Filter:
Using The Theme Or Using A Plugin
Now that we know how to create our own "the_content" filter, where practise we place it?
That's an like shooting fish in a barrel i:
Y'all can implement custom filters in two ways:
- Modifying the site's theme
- Creating a custom WordPress plugin
If you lot are impatient and already know how to create a WordPress plugin, you can skip to Part 3, where nosotros start with utilize case plugin implementations that volition add together content before and after a WordPress post.
As we've seen, in gild to accept WordPress run our filter, nosotros need to annals our filter. Merely where exercise nosotros do that? We demand to put that lawmaking where WordPress volition pick it up and execute it.
Method 1
Placing Our Custom WordPress Filter In The Theme
The quickest would exist to place the filter in the theme's function.php file. Go to your WordPress admin panel and open up Appearance > Editor and select your theme from the drib down on the right. Then choose the Theme role file calledfunction.php.
In this file, add the code for the filter at the stop:
function my_content_filter($content){ //this is where nosotros volition implement our filter return $content; } add_filter( 'the_content', 'my_content_filter' );
A word of circumspection:
You tin can mess up your site if you lot are not careful with this file. That's why, I recommend the safer method:
Method 2
Creating A Plugin To Add together the_content
Filter
To create a WordPress plugin you lot need to have access to the server where WordPress is installed. Usually, you will have either a online admin tool like cPanel, or you will use a FTP customer tool like FilleZilla or WinSCP.
In my old mail How to use jQuery plugin with WordPress plugin tutorial I'm going quite in depth into how to create a complex WordPress plugin.
Just, not this time.
I'k merely going to tell you this:
Become into the WordPress folder, netherwp-content/plugins/ create a new foder add together-content-plugin.
In that folder, create a filefunctions.php and put this content in it:
/*
* Plugin Name: CodingDude Add Content Earlier/Later on Plugin
* Description: Plugin for adding content before/after a post in WordPress
* Version: ane.0
* Author: @codingdudecom
* Author URI: https://www.coding-dude.com
*/ function my_content_filter($content){ //this is where we volition implement our filter render $content; } add_filter( 'the_content', 'my_content_filter' );
And now, let's become to the main topic of this post: adding content before and after a WordPress post.
We will go through several specific utilize cases and the filter implementation for them.
Fix?
Part three
WordPress Plugin Utilize Case #i:
Add Text Earlier Or Afterward Mail service
In this office we will implement a WordPress plugin that will add some text before or subsequently the post content.
We will simply implement our role for the the_content filter.
We volition use two PHP variables $before and $after where we volition place the text to become before and after all the posts.
Let's begin!
Without further ado, here's the lawmaking for our plugin that will add content before or afterwards all our WordPress posts:
function my_content_filter($content){ $before = '<p>This content will get before WordPress posts</p>'; $afterwards = '<p>This content volition go after WordPress posts</p>'; //modify the incoming content $content = $earlier . $content . $subsequently;
return $content; } add_filter( 'the_content', 'my_content_filter' );
As I've said, I volition not go into technical details. The dot operator just concatenates text. The but thing to know here is that the text or HTML in the $before/$after
variables volition go before/subsequently all WordPress posts.
Our code has one small-scale problem:
The content of all posts, only as well ALL pages will be modified by this slice of code.
Maybe that'southward something y'all want, but what if you only want the text additions fabricated just in the posts?
Some other less obvious thing, as pointed out in the article Playing Squeamish With the_content, the_content
filter is called under other circumstances that might not be what you desire similar widgets or plugins run in the background.
And then, how practise we add content only in the posts?
We will use the is_single()
and is_main_query()
WordPress functions to make sure we are adding text in the right identify:
function my_content_filter($content){ //only add text before WordPress posts if(is_single() && is_main_query()){ $before = '<p>This content will become before WordPress posts</p>';
$after = '<p>This content will become after WordPress posts</p>';//modify the incoming content
$content = $before . $content . $afterward;
}
return $content;
}add_filter( 'the_content', 'my_content_filter' );
What kind of text can I add in the $before
and $after
variables?
Nosotros tin utilise regular text, HTML or even WordPress shortcodes. So this is actually a useful tool we take hither.
Here are just a few ideas about what you can use this plugin for:
- Adding Google Ads Or Other Advertizing Banners Before Or After All WordPress Posts
- Adding A Survey Or Newsletter Registration Form Afterward All Posts
- Adding A Share Box Or Like Box Before And Afterwards All Posts
- Add A General Text Bulletin Or Production Countdown For All Your Users
Part iv
WordPress Plugin Use Instance #two:
Add Text subsequently (or before) content
based on publish date
In this role we will take a look at another use case. Adding text earlier and subsequently content of sure posts.
In particular, we will just add to posts after a sure publish date.
I actually establish some questions online related to this, so it's a real life use case scenario.
Even more,
This will evidence you a uncomplicated technique for how to utilise our plugin selectively.
Let'south say for example that we take a SEO weblog. We've written a lot of articles about how Google indexing work.
Simply, Google announced it'southward 7th Penguin algorithm update On September 23, 2016. That means that a lot of posts on our blog might not be accurate.
We can solve that by adding content at the end of every post published before September 23, 2016 stating that the information might not be accurate equally of that date, and mayhap even link to a new post with upwardly-to-date data.
This is how to practise information technology:
function my_content_filter($content){ //only add text before WordPress posts if(is_single() && is_main_query() && get_the_date('Y-grand-d') < "2016-02-28"){ $before = '<p>This content volition go before WordPress posts</p>'; $after = '<p>This post might incorporate outdated data due to Penguin's update of Sep 23rd, 2016. Check out this post for up-to-engagement information.</p>'; //change the incoming content $content = $before . $content . $after; } render $content; } add_filter( 'the_content', 'my_content_filter' );
We are using the WordPress built-in function get_the_date()
to ensure that our filter is but applied to the posts nosotros want.
Part 5
WordPress Plugin Utilise Case #3:
How To Replace Old External Links With New Ones In All WordPress Posts Automatically
So far we've used our plugin to add together text earlier or afterward a mail'southward content.
But, if we become access to the mail content, can we not as well change it?
Of course nosotros tin!
There are a lot of use cases in which we can use this plugin exactly for changing the text of a post.
I'm into affiliate marketing.
That is, I'm referring people to certain commercial digital products I create review, and if users purchase that product I get a per centum. There are a lot of affiliate programs out at that place, the about well known is Amazon.
The tracking of transactions is fabricated via parameters in the URL that I refer. That's how the seller knows that the buyer came from me.
Recently, I've been hitting by the following news:
The affiliate program I am part of has decided to switch to a new way of tracking purchases. They've totally changed the affiliate referral URLs.
The problem?
Over the last few years I've been writing and writing articles which included referral URLs in the erstwhile style. Only, those will be pretty much useless once their new tracking system gets into place.
The old URL looks similar this: https://codecanyon.net/detail/infographic-charts-and-graphics-html-tags-library/6351274?ref=psddude
While the new URL looks like this: https://1.envato.market place/c/361744/275988/4415?subId1=codingdude&subId2=2907&u=https://codecanyon.cyberspace/detail/infographic-charts-and-graphics-html-tags-library/6351274
The solution?
My content plugin. I will testify you how to use it to supercede old affiliate links with new links in all WordPress posts automatically:
function my_content_filter($content){ //only add together text before WordPress posts if(is_single() && is_main_query()){ //reply all occurances of the old URL with the new URL $content = str_replace('https://codecanyon.net/item/infographic-charts-and-graphics-html-tags-library/6351274?ref=psddude', 'https://one.envato.market/c/361744/275988/4415?subId1=codingdude&subId2=2907&u=https://codecanyon.net/item/infographic-charts-and-graphics-html-tags-library/6351274',$content); } render $content; } add_filter( 'the_content', 'my_content_filter' );
A simple search and replace of text using the PHP functionstr_replace()
and we are done.
NOTE: Yous can use exactly the same techniques for a number of other things like for example replacing YouTube lookout link to an embed link. Only switch this in the code:
$content = str_replace('https://www.youtube.com/watch?v=', 'https://www.youtube.com/embed/',$content);
Conclusion
I promise this post was helpful and you lot found at least one new matter.
At present, it'due south your turn:
Are yous planning to requite this a try and utilise a plugin for adding text earlier or after a post content?
How near replacing the content in a mail service with this plugin?
Or maybe you have an first-class piece of content that you recollect I should add.
Either fashion, let me know by leaving a annotate below right now and don't forget to subscribe to our newsletter for programming related updates.
Source: http://www.coding-dude.com/wp/wordpress/add-text-before-after-content/
Posted by: chavarriaexected.blogspot.com
0 Response to "How To Update A Wp Plugin From Fillezilla"
Post a Comment