Using WordPress ‘pingback_post’ PHP action

The pingback_post WordPress PHP action fires after a post pingback has been sent.

Usage

add_action('pingback_post', 'your_custom_function', 10, 1);

function your_custom_function($comment_id) {
    // your custom code here

    return $comment_id;
}

Parameters

  • $comment_id (int): The ID of the comment related to the pingback.

More information

See WordPress Developer Resources: pingback_post

Examples

Log pingback events

Log every pingback event in a custom log file.

add_action('pingback_post', 'log_pingback_events', 10, 1);

function log_pingback_events($comment_id) {
    // Get comment data
    $comment = get_comment($comment_id);

    // Log pingback data
    error_log('Pingback from ' . $comment->comment_author_url . ' on post ID ' . $comment->comment_post_ID);

    return $comment_id;
}

Send email notification on new pingback

Send an email notification to the site admin when a new pingback is received.

add_action('pingback_post', 'email_notification_on_pingback', 10, 1);

function email_notification_on_pingback($comment_id) {
    // Get comment data
    $comment = get_comment($comment_id);
    $post = get_post($comment->comment_post_ID);

    // Prepare email content
    $subject = 'New Pingback Received on ' . $post->post_title;
    $message = 'A new pingback has been received on the post "' . $post->post_title . '" from ' . $comment->comment_author_url;

    // Send email
    wp_mail(get_option('admin_email'), $subject, $message);

    return $comment_id;
}

Add custom metadata to pingbacks

Add custom metadata to pingbacks for later analysis or display.

add_action('pingback_post', 'add_custom_metadata_to_pingbacks', 10, 1);

function add_custom_metadata_to_pingbacks($comment_id) {
    // Add custom metadata to the comment
    add_comment_meta($comment_id, 'custom_key', 'custom_value');

    return $comment_id;
}

Moderate pingbacks based on domain

Automatically mark pingbacks as spam if they originate from a specific domain.

add_action('pingback_post', 'moderate_pingbacks_by_domain', 10, 1);

function moderate_pingbacks_by_domain($comment_id) {
    // Get comment data
    $comment = get_comment($comment_id);
    $domain_to_moderate = 'example.com';

    // Check if pingback is from the specified domain
    if (strpos($comment->comment_author_url, $domain_to_moderate) !== false) {
        // Mark the pingback as spam
        wp_set_comment_status($comment_id, 'spam');
    }

    return $comment_id;
}