The pre_ping WordPress PHP action fires just before pinging back links found in a post.
Usage
add_action('pre_ping', 'your_custom_function', 10, 3); function your_custom_function($post_links, $pung, $post_id) { // your custom code here }
Parameters
$post_links
(string[]): Array of link URLs to be checked (passed by reference).$pung
(string[]): Array of link URLs already pinged (passed by reference).$post_id
(int): The post ID.
More information
See WordPress Developer Resources: pre_ping
Examples
Skip Pinging Specific URLs
Skip pinging specific URLs by removing them from the $post_links
array.
add_action('pre_ping', 'skip_specific_ping', 10, 3); function skip_specific_ping(&$post_links, $pung, $post_id) { $skip_url = 'https://example.com/skip-me/'; $post_links = array_diff($post_links, array($skip_url)); }
Ping Only Once
Ensure URLs are pinged only once by checking if they are already in the $pung
array.
add_action('pre_ping', 'ping_only_once', 10, 3); function ping_only_once(&$post_links, $pung, $post_id) { $post_links = array_diff($post_links, $pung); }
Add Custom Data to Pings
Add custom data (e.g., a user agent) to pings by modifying the $post_links
array.
add_action('pre_ping', 'add_custom_data_to_pings', 10, 3); function add_custom_data_to_pings(&$post_links, $pung, $post_id) { $user_agent = 'Your-Custom-User-Agent'; foreach ($post_links as &$link) { $link .= '?user_agent=' . urlencode($user_agent); } }
Log Pings to a File
Log all ping attempts to a file for later analysis.
add_action('pre_ping', 'log_pings', 10, 3); function log_pings($post_links, $pung, $post_id) { $log_file = '/path/to/your/log-file.txt'; $log_entry = '[' . date('Y-m-d H:i:s') . '] Post ID: ' . $post_id . ' - Pinging: ' . implode(', ', $post_links) . PHP_EOL; file_put_contents($log_file, $log_entry, FILE_APPEND); }
Disable Pinging Completely
Disable pinging by clearing the $post_links
array.
add_action('pre_ping', 'disable_pinging', 10, 3); function disable_pinging(&$post_links, $pung, $post_id) { $post_links = array(); }