Add Snippet To Project
Are you tired of receiving trackbacks from your own website? Self trackbacks can clutter up your comment section and create confusion for your readers. Thankfully, there is a simple solution: disabling self trackbacks. In this article, we’ll show you how to easily disable self trackbacks in WordPress, allowing you to keep your comment section clean and organized.
function wpturbo_disable_self_trackbacks( $links ) {
foreach ( $links as $key => $link ) {
if ( strpos( $link, home_url() ) !== false ) {
unset( $links[$key] );
}
}
return $links;
}
add_action( 'pre_ping', 'wpturbo_disable_self_trackbacks' );
The code snippet above shows how to disable self trackbacks in WordPress. Self trackbacks occur when your own site sends a trackback to itself. This can create unnecessary noise in your comments section or trackback notifications.
The function wpturbo_disable_self_trackbacks()
is defined to handle the disabling of self trackbacks. It takes an array of trackback links as a parameter and iterates through each link using a foreach loop.
Inside the loop, the strpos()
function is used to determine if the link contains the URL of the current site. The home_url()
function retrieves the home URL of the site. If the link contains the URL of the current site, the unset()
function is called to remove the link from the array of trackback links.
Finally, the modified array of trackback links is returned using the return
statement.
To ensure that the wpturbo_disable_self_trackbacks()
function is called at the appropriate time, we use the add_action()
function and pass it the pre_ping
hook. The pre_ping
hook is fired before trackbacks are processed, so this is the ideal time to disable self trackbacks.
By adding the wpturbo_disable_self_trackbacks()
function to the pre_ping
action hook, WordPress will call this function whenever trackbacks are being processed, effectively disabling self trackbacks.