Using WordPress ‘discover_pingback_server_uri()’ PHP function

The discover_pingback_server_uri() WordPress PHP function is used to find a pingback server URI based on a provided URL. It checks the HTML for the rel="pingback" link and X-Pingback headers. The function prioritizes X-Pingback headers first, returning them if available, since the check for rel="pingback" incurs more overhead.

Usage

$pingback_uri = discover_pingback_server_uri('http://example.com');
echo $pingback_uri;

In this example, we’re finding the pingback server URI for http://example.com. The function will return the URI if it’s available.

Parameters

  • $url (string) – Required. This is the URL to ping.
  • $deprecated (string) – Optional. Not used. Default is an empty string.

More information

See WordPress Developer Resources: discover_pingback_server_uri()

Examples

Finding Pingback URI from a URL

$website_url = 'http://example.com';
$pingback_uri = discover_pingback_server_uri($website_url);
echo "The pingback URI for the website is: " . $pingback_uri;

This example finds the pingback URI for http://example.com and prints it.

Handling No Pingback URI

$website_url = 'http://example.com';
$pingback_uri = discover_pingback_server_uri($website_url);
if (empty($pingback_uri)) {
    echo "No pingback URI found.";
} else {
    echo "The pingback URI for the website is: " . $pingback_uri;
}

This example checks if a pingback URI exists for http://example.com. If not, it prints a message to inform the user.

Using Pingback URI in a Function

function get_pingback($website_url) {
    $pingback_uri = discover_pingback_server_uri($website_url);
    return $pingback_uri;
}
$uri = get_pingback('http://example.com');
echo "The pingback URI for the website is: " . $uri;

In this example, we’re encapsulating the discover_pingback_server_uri() in a function, and using it to get the pingback URI for http://example.com.

Multiple URLs

$website_urls = array('http://example1.com', 'http://example2.com', 'http://example3.com');
foreach ($website_urls as $website_url) {
    $pingback_uri = discover_pingback_server_uri($website_url);
    echo "The pingback URI for " . $website_url . " is: " . $pingback_uri . "\n";
}

This example finds the pingback URIs for a list of URLs and prints them.

Handling Errors

$website_url = 'http://example.com';
try {
    $pingback_uri = discover_pingback_server_uri($website_url);
    if (empty($pingback_uri)) {
        throw new Exception("No pingback URI found.");
    } else {
        echo "The pingback URI for the website is: " . $pingback_uri;
    }
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

This example throws an exception if no pingback URI is found for http://example.com, providing better error handling.