Apply the_title filter to post titles AND backend auto social-sharing plugin, but not nav menu

Solution

function append_album_review_to_title( $title, $id = NULL ) {
    if ($id) {
        if ( get_post_type( $id ) == 'album_review' ){
            return 'Album Review: ' . $title;
        } else {
            return $title;
        }
    } else {
        return 'Album Review: ' . $title;
    };
}
add_filter('the_title', 'append_album_review_to_title', 10, 2);

Explanation

First trying this:

function append_album_review_to_title( $title, $id ) {
    if ( get_post_type( $id ) == 'album_review' ){
        return 'Album Review: ' . $title;
    } else {
        return $title;
    }
}
add_filter('the_title', 'append_album_review_to_title', 10, 2);

I noticed that on the backend, the social sharing plugin I am using to auto-share posts would return warnings like missing parameter 2 and Notice: Trying to get property of non-object..., and so it occurred to me that unlike the front end (nav menu and the loop), in the backend, apparently an $id is not being passed to the_title and so I can use this as a conditional.

Checking for $id will, for my purposes, tell me that if it is true, we are on front end, if it is false, then we are at backend post view.

This code accomplishes what I need (ie, modify post title in the loop, do NOT modify nav menu items, and modify the_title for use by a backend social sharing plugin):

Leave a Comment