To disable self pingbacks in WordPress, you can use the pre_ping
filter hook. This hook allows you to modify the list of URLs to ping before a post is published.
Here’s an example code snippet that disables self pingbacks:
function wpsnippets_disable_self_pingbacks( $links ) {
$home_url = get_option( 'home' );
foreach ( $links as $key => $link ) {
if ( false !== strpos( $link, $home_url ) ) {
unset( $links[$key] );
}
}
return $links;
}
add_filter( 'pre_ping', 'wpsnippets_disable_self_pingbacks' );
This code snippet defines a custom function wpsnippets_disable_self_pingbacks
that takes an array of pingback URLs as a parameter. It checks each URL in the array and removes any URLs that contain the home URL of the WordPress site. Finally, it returns the modified array of URLs.
The add_filter
function is used to hook the wpsnippets_disable_self_pingbacks
function to the pre_ping
filter. This ensures that the function is executed before the pingbacks are sent.
By using this code snippet, self pingbacks will be disabled, preventing your own site from sending pingbacks to itself. This can be useful to avoid cluttering your comments section with unnecessary pingbacks.
Examples
Example 1: Disabling self pingbacks using a filter hook
This code example demonstrates how to disable self pingbacks in WordPress by using the pre_ping
filter hook. It checks if the pingback originates from the same domain as the post being pinged and returns false
to disable the self pingback.
function wpsnippets_disable_self_pingbacks( $links ) {
foreach ( $links as $key => $link ) {
if ( strpos( $link, home_url() ) !== false ) {
unset( $links[$key] );
}
}
return $links;
}
add_filter( 'pre_ping', 'wpsnippets_disable_self_pingbacks' );
In this code, the wpsnippets_disable_self_pingbacks
function is hooked into the pre_ping
filter. It loops through each pingback link and checks if the link contains the home URL of the WordPress site. If it does, the link is unset from the array of pingback links. Finally, the modified array of links is returned.
Example 2: Disabling self pingbacks using a custom function
This code example demonstrates how to disable self pingbacks in WordPress by using a custom function that checks if the pingback originates from the same domain as the post being pinged.
function wpsnippets_disable_self_pingbacks( $links ) {
$current_url = get_permalink();
foreach ( $links as $key => $link ) {
if ( strpos( $link, $current_url ) !== false ) {
unset( $links[$key] );
}
}
return $links;
}
add_filter( 'pre_ping', 'wpsnippets_disable_self_pingbacks' );
In this code, the wpsnippets_disable_self_pingbacks
function is hooked into the pre_ping
filter. It retrieves the current post’s URL using the get_permalink
function and loops through each pingback link. If the link contains the current URL, it is unset from the array of pingback links. Finally, the modified array of links is returned.
Example 3: Disabling self pingbacks using a class method
This code example demonstrates how to disable self pingbacks in WordPress by using a class method that checks if the pingback originates from the same domain as the post being pinged.
class WPSnippets_Disable_Self_Pingbacks {
public function disable_self_pingbacks( $links ) {
$current_url = get_permalink();
foreach ( $links as $key => $link ) {
if ( strpos( $link, $current_url ) !== false ) {
unset( $links[$key] );
}
}
return $links;
}
}
$disable_self_pingbacks = new WPSnippets_Disable_Self_Pingbacks();
add_filter( 'pre_ping', array( $disable_self_pingbacks, 'disable_self_pingbacks' ) );
In this code, a class named WPSnippets_Disable_Self_Pingbacks
is defined with a disable_self_pingbacks
method. The method retrieves the current post’s URL using the get_permalink
function and loops through each pingback link. If the link contains the current URL, it is unset from the array of pingback links. Finally, the modified array of links is returned. The class instance is then created and the disable_self_pingbacks
method is hooked into the pre_ping
filter using the add_filter
function.