Add Snippet To Project
Are you tired of receiving self-pings on your WordPress website? Self-pings occur when you link to your own content within your own posts, causing excessive notifications that clutter your comment section or inbox. Fortunately, there is a simple solution to disable self-pings using the pre_ping hook. In this article, we will guide you through the process of implementing this hook and preventing self-pings, ensuring a cleaner and more streamlined commenting experience on your WordPress site.
function wpturbo_disable_self_pings( $links ) {
$home_url = home_url();
foreach ( $links as $key => $link ) {
if ( false !== strpos( $link, $home_url ) ) {
unset( $links[$key] );
}
}
return $links;
}
add_action( 'pre_ping', 'wpturbo_disable_self_pings' );
The code snippet above demonstrates how to disable self pings in WordPress using the pre_ping
hook.
At the heart of this process is the wpturbo_disable_self_pings
function. This function accepts an array of pingback links as a parameter and performs the necessary checks to disable self pings.
Within the function, we first obtain the home URL of the WordPress site using the home_url()
function. This will serve as the basis for comparison later on.
Next, we iterate through each pingback link in the array using a foreach
loop, with the key and value assigned to the variables $key
and $link
, respectively.
Inside the loop, we use the strpos()
function to check if the home URL is present in the link. This function returns the position of the first occurrence of the home URL in the link, or false
if it is not found. Hence, we use the !==
operator to ensure that the home URL is not present in the link.
If the condition evaluates to true
, we use the unset()
function to remove the current pingback link from the array.
After looping through all the links, we return the modified array using the return
statement.
To integrate this function into the WordPress workflow, we use the add_action()
function to hook it into the pre_ping
action. This ensures that the wpturbo_disable_self_pings
function is executed whenever a pingback request is triggered before the pinging process.
By implementing this code snippet, self pings from the WordPress site’s own URLs will be disabled, preventing unnecessary notifications and keeping the comment section clean and organized.