Disable self pings on a multisite

On a project, the client wanted to disable any pingsbacks from it’s own site. I found a snippet for that and there is also a plugin with this snippet. But this will only work for the current page. What if you want to disable pingbacks from one site of a multisite to another?

The extended snippet

Everytime a post or page is posted or updated, WordPress will try to send a pingback to any URL in the content. To modify the list of links to ping, we can use the action pre_ping with a very simple callback function:

function multisite_no_self_ping( &$links ) {
	$sites     = get_sites();
	$site_urls = array();
	
	foreach ( $sites as $site ) {
		$site_urls[] = get_home_url( $site->blog_id );
	}
	
	foreach ( $links as $key => $link ) {
		foreach ( $site_urls as $site_url ) {
			if ( 0 === strpos( $link, $site_url ) ) {
				unset( $links[ $key ] );
			}
		}
	}
}
add_action( 'pre_ping', 'multisite_no_self_ping' );

First we get all sites and the home URLs for each of them. Then we iterate over all the links WordPress would ping. If any of those links is starting with any of the home URLs, we remove it from the list of links to ping. As the list of links is passed by reference, we don’t have to return anything.

Result

That’s it. By using this simple snippet, we can disable all pingbacks on a multisite. Those pingbacks are usually deleted by most users anyways. The best way to add this snippet to your WordPress installation is through a plugin, so I prepared one for you as a Gist.

What do you think? Do you usually delete internal pingbacks? Or do you have any use case, where they are useful?

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

Your email address will not be published. Required fields are marked *