Using WordPress ‘pings_open’ PHP filter

The pings_open WordPress PHP filter allows you to control whether a specific post is open to receiving pingbacks and trackbacks.

Usage

add_filter('pings_open', 'your_custom_function', 10, 2);
function your_custom_function($open, $post_id) {
    // Your custom code here
    return $open;
}

Parameters

  • $open (bool) – Indicates whether the current post is open for pings.
  • $post_id (int) – The ID of the post.

More information

See WordPress Developer Resources: https://developer.wordpress.org/reference/hooks/pings_open/

Examples

Disable Pings for a Specific Post

Disable pings for the post with ID 42.

add_filter('pings_open', 'disable_pings_for_specific_post', 10, 2);
function disable_pings_for_specific_post($open, $post_id) {
    if ($post_id == 42) {
        return false;
    }
    return $open;
}

Enable Pings Only for Certain Post Categories

Enable pings only for posts in the “Announcements” and “Events” categories.

add_filter('pings_open', 'enable_pings_for_categories', 10, 2);
function enable_pings_for_categories($open, $post_id) {
    $allowed_categories = array('announcements', 'events');
    $post_categories = wp_get_post_categories($post_id, array('fields' => 'slugs'));

    if (array_intersect($allowed_categories, $post_categories)) {
        return true;
    }
    return false;
}

Disable Pings for Posts Older than 30 Days

Disable pings for posts that are older than 30 days.

add_filter('pings_open', 'disable_pings_for_old_posts', 10, 2);
function disable_pings_for_old_posts($open, $post_id) {
    $post_date = get_post_time('U', true, $post_id);
    $days_old = (time() - $post_date) / DAY_IN_SECONDS;

    if ($days_old > 30) {
        return false;
    }
    return $open;
}

Enable Pings for Posts with Specific Tags

Enable pings only for posts with the “Featured” or “Important” tags.

add_filter('pings_open', 'enable_pings_for_tags', 10, 2);
function enable_pings_for_tags($open, $post_id) {
    $allowed_tags = array('featured', 'important');
    $post_tags = wp_get_post_tags($post_id, array('fields' => 'slugs'));

    if (array_intersect($allowed_tags, $post_tags)) {
        return true;
    }
    return $open;
}

Disable Pings for All Posts

Disable pings for all posts on the website.

add_filter('pings_open', '__return_false');

Enable Pings for Specific Author

Enable pings only for posts written by the author with ID 5.

add_filter('pings_open', 'enable_pings_for_author', 10, 2);
function enable_pings_for_author($open, $post_id) {
    $author_id = get_post_field('post_author', $post_id);

    if ($author_id == 5) {
        return true;
    }
    return $open;
}

Disable Pings for Private Posts

Disable pings for private (password-protected) posts.

add_filter('pings_open', 'disable_pings_for_private_posts', 10, 2);
function disable_pings_for_private_posts($open, $post_id) {
    $post_status = get_post_status($post_id);

    if ($post_status == 'private') {
        return false;
    }
    return $open;
}

Enable Pings Based on Custom Field Value

Enable pings only for posts with a custom field allow_pings set to true.

add_filter('pings_open', 'enable_pings_based_on_custom_field', 10, 2);
function enable_pings_based_on_custom_field($open, $post_id) {
    $allow_pings = get_post_meta($post_id, 'allow_pings', true);

    if ($allow_pings == 'true') {
        return true;
    }
    return false;
}

Enable Pings for Posts with a Minimum Word Count

Enable pings only for posts that have a word count greater than or equal to 500.

add_filter('pings_open', 'enable_pings_for_min_word_count', 10, 2);
function enable_pings_for_min_word_count($open, $post_id) {
    $post_content = get_post_field('post_content', $post_id);
    $word_count = str_word_count(strip_tags($post_content));

    if ($word_count >= 500) {
        return true;
    }
    return $open;
}

10. Disable Pings for Posts with Certain Post Formats

Disable pings for posts with the “Aside” or “Quote” post formats.

add_filter('pings_open', 'disable_pings_for_post_formats', 10, 2);
function disable_pings_for_post_formats($open, $post_id) {
    $disallowed_formats = array('aside', 'quote');
    $post_format = get_post_format($post_id);

if (in_array($post_format, $disallowed_formats)) {
return false;
}
return $open;
}