How To Disable Self Pingbacks in WordPress Without a Plugin
By default, WordPress blog automatically send a pingback to itself any time you link to one of your own internal old post. It would be nice if this self-pingback feature can be turned off in the dashboard.
Self-pings, or pings within your own blog, are found useful by some, annoying by others. Those who find them useful feel that self-pingbacks is a good approach to increase inter-linking between posts.
You can stop self-pings if you wish. You can simply manually delete them once you get them in the WordPress dashboard, or you can use one of the 2 methods below to automatically disable self-pings to your own blog.
What are Pingbacks?
Pingbacks are automated cross referencing system between blogs. Pingbacks are designed that you receive a notification when another blog links to one of your posts. For more details, read my previous tutorial: Trackbacks and Pingbacks in WordPress.
What About a Plugin?
There are plugins that disable self pingbacks but you really don´t need a plugin for such a simple task. Also I encourage you to read my previous post about unnecessarily adding plugins to your theme. Personally, I am not a fan of plugins, I prefer to use a snippet of code instead.
How Self Pingbacks are Created
The best way to learn about self pingbacks is to create an internal link from one post to another on your blog. You will then find a pingback in your comment section.
If Comments are closed for an older post, and you linked to it from a newer post on the same blog, then self-pingbacks will not be sent by WordPress. This is the only case, that WordPress will not send self-pingbacks if you decide not to take action by choosing either of the methods below.
2 Methods to Disable Self Pingbacks
If you don´t want to get self-pingbacks on your blog, you have 2 choices to stop self pingbacks from showing up as comments on your posts. Either method is just fine.
METHOD1 | No Self-Pingbacks | functions.php
One way to disable self-pings is to add CODE-1 to your theme´s functions.php file. Open the functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-1. Save the file and upload it to the server.
CODE-1 :: Method1 to disable Self Pingbacks
Note: To scroll within the code: You can also click on the code window and use your keyboard´s arrow keys.
<?php
/* No Self Pings */
//Pass the variable by reference to the function, so the function can modify the variable.
function no_self_ping (&$links) {
$home = get_option( 'home' );
foreach ( $links as $l => $link )
//Find the position of the first occurrence of a substring in a string.
//($a === $b) Identical operator. TRUE if $a is equal to $b, and they are of the same type.
if ( 0 === strpos( $link, $home ) )
//Unset the variable
unset($links[$l]);
}
//Hooks the function to the specific action (pre_ping)
add_action( 'pre_ping', 'no_self_ping' );
?>
CODE-1 Notes:
Self pingbacks are now disabled. Source WordPress forum.
Some themes, or theme frameworks, have more variations of the functions.php file, so look at their documentation and see where best to add the code.
CODE-1 is tested to work on WordPress version 3.3.1
CODE-1 References:
- get_option()
- add_action()
- strpos()
- unset()
- Passing by Reference
METHOD2 | No Self-Pingbacks | Use Relative Path for Links
Another option to avoid self pinging, is to use relative path for the links in your post instead of absolute path. In other words, Replace the absolute links of your internal posts with relative links.
//Replace the Absolute link of the internal post
<a href="https://bacsoftwareconsulting.com/wordpress-cat/tips-for-maximum-wordpress-performance-and-speed/">Absolute path</a>
//With the following Relative link:
<a href="/index.php/wordpress-cat/tips-for-maximum-wordpress-performance-and-speed/" title="14 Tips ...">Relative Path</a>
Of course, relative Links in your case are most likely different than in my case. It all depends on the structure of your blog’s directory.
For more information on Absolute versus Relative Paths see tutorial1 and tutorial2.
General Warning
When you add several PHP code blocks in your theme´s funtions.php file, make sure that you do NOT leave any white space (spaces, newline) before the opening PHP tag (<?php) or after the closing PHP tag (?>). The correct way is as follows:
<?php
//Some Code here beetween the opening PHP tag (above)
//and the closing PHP tag (below)...
?>
<?php
//Some other Code here ...
?>
In the above code, if you leave any white space or a newline between lines 4 and 5, you will get the following error: “Warning: Cannot modify header information – headers already sent by…”
Your Turn to Talk
You now have the choice to disable self-pingabcks, without a plugin, and keep WordPress blog from pinging itself.
Some bloggers like to use the self-pinging feature as they find it beneficial for better inter-linking between posts, which I understand their point. The majority of the time I tend to delete self-pingbacks.