Make WordPress Core

Changeset 62417


Ignore:
Timestamp:
05/26/2026 12:17:59 AM (18 hours ago)
Author:
ramonopoly
Message:

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 localdevelopment, 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.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r62233 r62417  
    31653165
    31663166/**
     3167 * Determines whether pings should be disabled for the current environment.
     3168 *
     3169 * By default, all pings (outgoing pingbacks, trackbacks, and ping service
     3170 * notifications, as well as incoming pingbacks and trackbacks) are disabled
     3171 * for non-production environments ('local', 'development', 'staging').
     3172 *
     3173 * @since 7.1.0
     3174 *
     3175 * @return bool True if pings should be disabled, false otherwise.
     3176 */
     3177function wp_should_disable_pings_for_environment() {
     3178    $environment_type = wp_get_environment_type();
     3179    $should_disable   = 'production' !== $environment_type;
     3180
     3181    /**
     3182     * Filters whether pings should be disabled for the current environment.
     3183     *
     3184     * Returning false re-enables pings in non-production environments.
     3185     * Returning true disables pings even in production.
     3186     *
     3187     * @since 7.1.0
     3188     *
     3189     * @param bool   $should_disable  Whether pings should be disabled. Default true
     3190     *                                for non-production environments, false for production.
     3191     * @param string $environment_type The current environment type as returned by
     3192     *                                 wp_get_environment_type().
     3193     */
     3194    return apply_filters( 'wp_should_disable_pings_for_environment', $should_disable, $environment_type );
     3195}
     3196
     3197/**
     3198 * Removes outgoing ping callbacks in non-production environments.
     3199 *
     3200 * Hooked to `do_all_pings` at priority 1 so it runs before the default
     3201 * priority 10 callbacks. Does not remove `do_all_enclosures`.
     3202 *
     3203 * @since 7.1.0
     3204 */
     3205function wp_maybe_disable_outgoing_pings_for_environment() {
     3206    if ( wp_should_disable_pings_for_environment() ) {
     3207        remove_action( 'do_all_pings', 'do_all_pingbacks' );
     3208        remove_action( 'do_all_pings', 'do_all_trackbacks' );
     3209        remove_action( 'do_all_pings', 'generic_ping' );
     3210    }
     3211}
     3212
     3213/**
     3214 * Rejects incoming trackbacks in non-production environments.
     3215 *
     3216 * Hooked to `pre_trackback_post` which fires in `wp-trackback.php` before the
     3217 * trackback is processed. Calls `trackback_response()` which sends an XML error
     3218 * response and terminates the request.
     3219 *
     3220 * @since 7.1.0
     3221 */
     3222function wp_maybe_disable_trackback_for_environment() {
     3223    if ( wp_should_disable_pings_for_environment() ) {
     3224        trackback_response( 1, __( 'Trackbacks are disabled in non-production environments.' ) );
     3225    }
     3226}
     3227
     3228/**
     3229 * Removes the pingback XML-RPC method in non-production environments.
     3230 *
     3231 * @since 7.1.0
     3232 *
     3233 * @param string[] $methods An array of XML-RPC methods, keyed by their methodName.
     3234 * @return string[] Modified array of XML-RPC methods.
     3235 */
     3236function wp_maybe_disable_xmlrpc_pingback_for_environment( $methods ) {
     3237    if ( wp_should_disable_pings_for_environment() ) {
     3238        unset( $methods['pingback.ping'] );
     3239    }
     3240
     3241    return $methods;
     3242}
     3243
     3244/**
    31673245 * Pings back the links found in a post.
    31683246 *
  • trunk/src/wp-includes/default-filters.php

    r62349 r62417  
    422422add_action( 'do_all_pings', 'do_all_trackbacks', 10, 0 );
    423423add_action( 'do_all_pings', 'generic_ping', 10, 0 );
     424
     425// Disable pings (pingbacks, trackbacks, and ping service notifications) in non-production environments.
     426add_action( 'do_all_pings', 'wp_maybe_disable_outgoing_pings_for_environment', 1, 0 );
     427add_action( 'pre_trackback_post', 'wp_maybe_disable_trackback_for_environment', 10, 0 );
     428add_filter( 'xmlrpc_methods', 'wp_maybe_disable_xmlrpc_pingback_for_environment' );
     429
    424430add_action( 'do_robots', 'do_robots' );
    425431add_action( 'do_favicon', 'do_favicon' );
Note: See TracChangeset for help on using the changeset viewer.