Using WordPress ‘pre_remote_source’ PHP filter

The ‘pre_remote_source’ WordPress PHP filter lets you modify the pingback remote source in WordPress before it’s processed.

Usage

function my_pre_remote_source($remote_source, $pagelinkedto) {
    // Modify the remote source.
    return $remote_source;
}
add_filter('pre_remote_source', 'my_pre_remote_source', 10, 2);

Parameters

  • $remote_source (string): The response source for the page linked from.
  • $pagelinkedto (string): The URL of the page linked to.

Examples

Customizing the remote source

function my_custom_remote_source($remote_source, $pagelinkedto) {
    // Modify the remote source.
    $remote_source = 'https://example.com/custom-source';

    return $remote_source;
}
add_filter('pre_remote_source', 'my_custom_remote_source', 10, 2);

This code customizes the remote source by changing it to ‘https://example.com/custom-source’.

Appending a query parameter to the remote source

function append_query_param($remote_source, $pagelinkedto) {
    // Append the query parameter.
    $remote_source = $remote_source . '?utm_source=pingback';

    return $remote_source;
}
add_filter('pre_remote_source', 'append_query_param', 10, 2);

This code appends a query parameter utm_source=pingback to the remote source.

Blocking pingbacks from specific domains

function block_specific_domains($remote_source, $pagelinkedto) {
    $blocked_domains = array('example1.com', 'example2.com');

    foreach ($blocked_domains as $domain) {
        if (strpos($remote_source, $domain) !== false) {
            return '';
        }
    }

    return $remote_source;
}
add_filter('pre_remote_source', 'block_specific_domains', 10, 2);

This code blocks pingbacks from the domains ‘example1.com’ and ‘example2.com’ by emptying the remote source if it contains any of the blocked domains.

Allowing pingbacks only from a specific domain

function allow_specific_domain($remote_source, $pagelinkedto) {
    $allowed_domain = 'example.com';

    if (strpos($remote_source, $allowed_domain) === false) {
        return '';
    }

    return $remote_source;
}
add_filter('pre_remote_source', 'allow_specific_domain', 10, 2);

This code allows pingbacks only from the domain ‘example.com’ by emptying the remote source if it doesn’t contain the allowed domain.

Removing a path segment from the remote source

function remove_path_segment($remote_source, $pagelinkedto) {
    $path_to_remove = '/unwanted-segment';

    $remote_source = str_replace($path_to_remove, '', $remote_source);

    return $remote_source;
}
add_filter('pre_remote_source', 'remove_path_segment', 10, 2);

This code removes the path segment ‘/unwanted-segment’ from the remote source.