Disable External Pingacks on WordPress Posts and Only Allow ‘Self Pings’

If you want to receive only linkbacks from your own WordPress site, then try both of these:

But if you want to allow all that were sent as trackbacks (where the comment type is trackback), then just ignore the second snippet below, which I added because in default/core themes like Twenty Twenty-One, both pingbacks and trackbacks would each appear as a “Pingback” in the post’s comments section.

  1. This uses the xmlrpc_call action to disable pingbacks sent via the XML-RPC method:

    add_action( 'xmlrpc_call', 'disallow_external_xmlrpc_pingback', 10, 3 );
    function disallow_external_xmlrpc_pingback( $name, $args, $server ) {
        if ( 'pingback.ping' == $name && false === strpos( $args[0], home_url() ) ) {
            // Exit with a proper error.
            $server->error( new IXR_Error( 0, 'Sorry, trackbacks from remote sites are not allowed.' ) );
        }
    }
    
  2. This uses the pre_trackback_post action to disable trackbacks sent via the standard HTTP POST method (i.e. not using XML-RPC):

    add_action( 'pre_trackback_post', 'disallow_external_POST_trackback', 10, 2 );
    function disallow_external_POST_trackback( $tb_id, $tb_url ) {
        if ( false === strpos( $tb_url, home_url() ) ) {
            // Exit with a proper error.
            trackback_response( 1, 'Sorry, trackbacks from remote sites are not allowed.' );
        }
    }
    

And BTW, in the code in the question, $home is undefined and it should be $Home ( note the uppercase “H” and see this which says, “variable name is case-sensitive” 🙂 ).