Change page title from plugin

There’s a filter for that:

function wpse_alter_title( $title, $id )
{
    // $id = $post->ID;
    // alter the title here
    return $title;
}

If you want to alter the “Protected” and “Private” titles, then you need other filters:

// Preserve the "%s" - else the title will be removed.
function wpse_alter_protected_title_format( $title )
{
    return __( 'Protected: %s' );
}

function wpse_alter_private_title_format( $title )
{
    return __( 'Private: %s' );
}

Last but not least, you must add your filter callbacks early enough.

function wpse_load_alter_title()
{
    add_filter( 'the_title', 'wpse_alter_title', 20, 2 );
    add_filter( 'protected_title_format', 'wpse_alter_protected_title_format' );
    add_filter( 'private_title_format', 'wpse_alter_private_title_format' );
}
add_action( 'init', 'wpse_load_alter_title' );

Leave a Comment