Disable pings/trackbacks for local, development, and staging environments

When WP_ENVIRONMENT_TYPE is not `production`, disable pingbacks and trackbacks.  

Otherwise, when `WP_ENVIRONMENT_TYPE` is `local`, `development`, or `staging`, pingbacks and trackbacks are sent when posts are published. This creates confusion on the receiving end and is unnecessary for testing workflows.  

Props arcangelini, cagrimmett, ramonopoly, tyxla, khushipatel15.  

Fixes #64837.


Built from https://develop.svn.wordpress.org/trunk@62417


git-svn-id: http://core.svn.wordpress.org/trunk@61698 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ramonopoly
2026-05-26 00:18:45 +00:00
parent e138887f3a
commit 0484004a0b
3 changed files with 85 additions and 1 deletions
+78
View File
@@ -3163,6 +3163,84 @@ function generic_ping( $post_id = 0 ) {
return $post_id;
}
/**
* Determines whether pings should be disabled for the current environment.
*
* By default, all pings (outgoing pingbacks, trackbacks, and ping service
* notifications, as well as incoming pingbacks and trackbacks) are disabled
* for non-production environments ('local', 'development', 'staging').
*
* @since 7.1.0
*
* @return bool True if pings should be disabled, false otherwise.
*/
function wp_should_disable_pings_for_environment() {
$environment_type = wp_get_environment_type();
$should_disable = 'production' !== $environment_type;
/**
* Filters whether pings should be disabled for the current environment.
*
* Returning false re-enables pings in non-production environments.
* Returning true disables pings even in production.
*
* @since 7.1.0
*
* @param bool $should_disable Whether pings should be disabled. Default true
* for non-production environments, false for production.
* @param string $environment_type The current environment type as returned by
* wp_get_environment_type().
*/
return apply_filters( 'wp_should_disable_pings_for_environment', $should_disable, $environment_type );
}
/**
* Removes outgoing ping callbacks in non-production environments.
*
* Hooked to `do_all_pings` at priority 1 so it runs before the default
* priority 10 callbacks. Does not remove `do_all_enclosures`.
*
* @since 7.1.0
*/
function wp_maybe_disable_outgoing_pings_for_environment() {
if ( wp_should_disable_pings_for_environment() ) {
remove_action( 'do_all_pings', 'do_all_pingbacks' );
remove_action( 'do_all_pings', 'do_all_trackbacks' );
remove_action( 'do_all_pings', 'generic_ping' );
}
}
/**
* Rejects incoming trackbacks in non-production environments.
*
* Hooked to `pre_trackback_post` which fires in `wp-trackback.php` before the
* trackback is processed. Calls `trackback_response()` which sends an XML error
* response and terminates the request.
*
* @since 7.1.0
*/
function wp_maybe_disable_trackback_for_environment() {
if ( wp_should_disable_pings_for_environment() ) {
trackback_response( 1, __( 'Trackbacks are disabled in non-production environments.' ) );
}
}
/**
* Removes the pingback XML-RPC method in non-production environments.
*
* @since 7.1.0
*
* @param string[] $methods An array of XML-RPC methods, keyed by their methodName.
* @return string[] Modified array of XML-RPC methods.
*/
function wp_maybe_disable_xmlrpc_pingback_for_environment( $methods ) {
if ( wp_should_disable_pings_for_environment() ) {
unset( $methods['pingback.ping'] );
}
return $methods;
}
/**
* Pings back the links found in a post.
*
+6
View File
@@ -421,6 +421,12 @@ add_action( 'do_all_pings', 'do_all_pingbacks', 10, 0 );
add_action( 'do_all_pings', 'do_all_enclosures', 10, 0 );
add_action( 'do_all_pings', 'do_all_trackbacks', 10, 0 );
add_action( 'do_all_pings', 'generic_ping', 10, 0 );
// Disable pings (pingbacks, trackbacks, and ping service notifications) in non-production environments.
add_action( 'do_all_pings', 'wp_maybe_disable_outgoing_pings_for_environment', 1, 0 );
add_action( 'pre_trackback_post', 'wp_maybe_disable_trackback_for_environment', 10, 0 );
add_filter( 'xmlrpc_methods', 'wp_maybe_disable_xmlrpc_pingback_for_environment' );
add_action( 'do_robots', 'do_robots' );
add_action( 'do_favicon', 'do_favicon' );
add_action( 'wp_before_include_template', 'wp_start_template_enhancement_output_buffer', 1000 ); // Late priority to let `wp_template_enhancement_output_buffer` filters and `wp_finalized_template_enhancement_output_buffer` actions be registered.
+1 -1
View File
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '7.1-alpha-62416';
$wp_version = '7.1-alpha-62417';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.