Using WordPress ‘pre_trackback_post’ PHP action

The pre_trackback_post WordPress PHP action fires before a trackback is added to a post.

Usage

add_action('pre_trackback_post', 'your_custom_function', 10, 6);

function your_custom_function($post_id, $trackback_url, $charset, $title, $excerpt, $blog_name) {
    // Your custom code here

    return $post_id;
}

Parameters

  • $post_id (int) – Post ID related to the trackback.
  • $trackback_url (string) – Trackback URL.
  • $charset (string) – Character set.
  • $title (string) – Trackback title.
  • $excerpt (string) – Trackback excerpt.
  • $blog_name (string) – Blog name.

More information

See WordPress Developer Resources: pre_trackback_post

Examples

Block Trackbacks from a Specific Domain

function block_specific_domain_trackbacks($post_id, $trackback_url, $charset, $title, $excerpt, $blog_name) {
    if (strpos($trackback_url, 'example.com') !== false) {
        exit;
    }

    return $post_id;
}
add_action('pre_trackback_post', 'block_specific_domain_trackbacks', 10, 6);

Change Trackback Charset

function change_trackback_charset($post_id, $trackback_url, $charset, $title, $excerpt, $blog_name) {
    $charset = 'UTF-8';

    return $post_id;
}
add_action('pre_trackback_post', 'change_trackback_charset', 10, 6);

Add Prefix to Trackback Title

function add_prefix_to_trackback_title($post_id, $trackback_url, $charset, $title, $excerpt, $blog_name) {
    $title = 'Trackback: ' . $title;

    return $post_id;
}
add_action('pre_trackback_post', 'add_prefix_to_trackback_title', 10, 6);

Limit Trackback Excerpt Length

function limit_trackback_excerpt_length($post_id, $trackback_url, $charset, $title, $excerpt, $blog_name) {
    $excerpt = wp_trim_words($excerpt, 20, '...');

    return $post_id;
}
add_action('pre_trackback_post', 'limit_trackback_excerpt_length', 10, 6);

Customize Blog Name in Trackbacks

function customize_blog_name($post_id, $trackback_url, $charset, $title, $excerpt, $blog_name) {
    $blog_name = 'My Custom Blog Name';

    return $post_id;
}
add_action('pre_trackback_post', 'customize_blog_name', 10, 6);