Using WordPress ‘add_ping()’ PHP function

The add_ping() WordPress PHP function adds a URL to the list of those already pinged.

Usage

Here’s an example of how you might use the add_ping() function:

$post_id = 42; // Let's assume that 42 is the ID of the post we want to add a ping to
$ping_url = 'https://example.com'; // The URL we want to add to the ping list

add_ping($post_id, $ping_url); // Use the function to add the URL to the ping list

Parameters

  • $post (int|WP_Post – Required): The ID or the object of the post you want to add a ping to.
  • $uri (string|array – Required): The URI you want to add to the ping list. This can be a string or an array of URIs.

More information

See WordPress Developer Resources: add_ping()

Examples

Add a single ping URL

This example shows how to add a single ping URL to a post.

$post_id = 10;
$ping_url = 'https://example.com/news';
add_ping($post_id, $ping_url);

Add multiple ping URLs

This example demonstrates how to add multiple ping URLs to a post using an array of URLs.

$post_id = 10;
$ping_urls = array('https://example1.com', 'https://example2.com');
add_ping($post_id, $ping_urls);

Add a ping to a WP_Post object

This example demonstrates how to add a ping to a post using a WP_Post object.

$post = get_post(10); // Get the WP_Post object
$ping_url = 'https://example.com/news';
add_ping($post, $ping_url);

Add a ping to the latest post

In this example, we get the most recent post and add a ping URL to it.

$recent_posts = wp_get_recent_posts(array('numberposts' => 1)); // Get the most recent post
$recent_post = array_shift($recent_posts); // Get the first post from the array
$ping_url = 'https://example.com/news';
add_ping($recent_post['ID'], $ping_url);

Add a ping to all posts in a category

This example adds a ping URL to all posts in a specific category.

$category_posts = get_posts(array('category' => 5)); // Get all posts in category 5
$ping_url = 'https://example.com/news';

foreach ($category_posts as $post) {
    add_ping($post->ID, $ping_url);
}

Each of these examples shows how you might use the add_ping() function in a real-world situation, from adding a single ping to a specific post to adding pings to all posts in a category.