Last updated on September 25, 2023

Disable Pingbacks and Trackbacks

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Prevent spam by disabling pingbacks.

To disable pingbacks and trackbacks in WordPress, you can use the wp_snippets_disable_pingbacks_trackbacks() function. This function will disable the pingbacks and trackbacks functionality by removing the necessary hooks.

function wpsnippets_disable_pingbacks_trackbacks() {
    // Disable pingbacks and trackbacks
    add_filter( 'xmlrpc_methods', function( $methods ) {
        unset( $methods['pingback.ping'] );
        return $methods;
    } );

    add_filter( 'wp_headers', function( $headers ) {
        unset( $headers['X-Pingback'] );
        return $headers;
    } );

    add_action( 'template_redirect', function() {
        global $wp_query;
        if ( is_singular() && $wp_query->post->ping_status == 'open' ) {
            $wp_query->post->ping_status = 'closed';
            update_post_meta( $wp_query->post->ID, '_pingme', 'closed' );
        }
    } );

    add_action( 'pre_ping', function( &$links ) {
        $links = array();
    } );
}
add_action( 'init', 'wpsnippets_disable_pingbacks_trackbacks' );

This code snippet disables pingbacks and trackbacks in WordPress by removing the necessary hooks and filters. It removes the pingback.ping method from the xmlrpc_methods filter, removes the X-Pingback header from the wp_headers filter, sets the ping status of singular posts to “closed” on the template_redirect action, and clears the pingback links on the pre_ping action.

You can use this code snippet in any WordPress project where you want to disable pingbacks and trackbacks. It helps to prevent spam and unnecessary requests on your website.

Examples

Example 1: Disabling Pingbacks and Trackbacks in WordPress

This example demonstrates how to disable pingbacks and trackbacks in WordPress by using the wp_snippets_disable_pingbacks_trackbacks() function.

function wp_snippets_disable_pingbacks_trackbacks() {
    update_option('default_pingback_flag', 0);
    update_option('default_ping_status', 'closed');
    update_option('default_trackback_flag', 0);
    update_option('default_trackback_status', 'closed');
}
add_action('init', 'wp_snippets_disable_pingbacks_trackbacks');

In this code example, the wp_snippets_disable_pingbacks_trackbacks() function is hooked to the init action. It updates the WordPress options default_pingback_flag, default_ping_status, default_trackback_flag, and default_trackback_status to disable pingbacks and trackbacks. By setting these options to 0 and 'closed', pingbacks and trackbacks will be disabled globally on your WordPress site.

Example 2: Disabling Pingbacks and Trackbacks for a Specific Post Type

This example demonstrates how to disable pingbacks and trackbacks for a specific post type in WordPress by using the wp_snippets_disable_pingbacks_trackbacks_post_type() function.

function wp_snippets_disable_pingbacks_trackbacks_post_type() {
    $post_type = 'your_post_type';
    $args = array(
        'public' => true,
        '_builtin' => false
    );
    $post_types = get_post_types($args, 'names');
    if (in_array($post_type, $post_types)) {
        update_post_type_support($post_type, 'trackbacks', false);
        update_post_type_support($post_type, 'pingbacks', false);
    }
}
add_action('init', 'wp_snippets_disable_pingbacks_trackbacks_post_type');

In this code example, the wp_snippets_disable_pingbacks_trackbacks_post_type() function is hooked to the init action. It first checks if the specified post type exists, and if so, it disables pingbacks and trackbacks for that post type by using the update_post_type_support() function.

Example 3: Disabling Pingbacks and Trackbacks for a Specific Post

This example demonstrates how to disable pingbacks and trackbacks for a specific post in WordPress by using the wp_snippets_disable_pingbacks_trackbacks_post() function.

function wp_snippets_disable_pingbacks_trackbacks_post($post_id) {
    update_post_meta($post_id, '_pingme', '0');
    update_post_meta($post_id, '_encloseme', '0');
}
add_action('save_post', 'wp_snippets_disable_pingbacks_trackbacks_post');

In this code example, the wp_snippets_disable_pingbacks_trackbacks_post() function is hooked to the save_post action. It updates the post meta _pingme and _encloseme to 0, effectively disabling pingbacks and trackbacks for the specific post identified by $post_id.

Last updated on September 25, 2023. Originally posted on October 13, 2023.

Leave a Reply

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