1

What I've got so far

I've finally got the functions.php plugin to append a string to custom post titles of a certain post type with this function:

function append_album_review_to_title( $title ) {
    global $post;
    $text = 'Album Review: ';

    if ( get_post_type( $post->ID ) == 'album_review' && in_the_loop() ){
        return $text . $title;
    }
    else {
        return $title;
    }
}
if(function_exists('add_filter')) {
    add_filter('the_title', 'append_album_review_to_title');
}

and though it does almost everything I need it to, it still doesn't append the string to titles as they are posted via auto-posting plugins.

The Problem

The plugin I am using is NextScripts: Social Networks Auto Poster and everywhere I can see the title, the desired string is appended but when this plugin auto-posts, the string is not appended.

The Question

How can I change my function to also apply to the titles as they are output by auto-posting plugins?


UPDATE 1

Why in_the_loop()?

I use in_the_loop() because if I don't, the string gets appended to seemingly every link on the page. I think this may happen because the post_type I am appending to is a custom type. I just tried removing it, and of course "Album Review: " was added to every single link on the page.

Will %FULLTITLE% only work with manual <meta>s?

I tried using %FULLTITLE% without manual <meta> tags as you described, but it doesn't seem to append the string. The reason why I bothered to try it this way is because YOAST plugin (as you said) not only already includes these tags, but because it already appends "Album Review: " to the title where necessary. So, when I inspect source, in the <head>, the tags are like:

<meta property="og:title" content="Album Review: [the rest of title]">

Is YOAST's appending not enough, and I need to do the manual tags?

(Apologies for this form of communication; WordPress StackExchange still won't allow me to comment or upvote.)

1
  • 1
    Why in the world would you need the following useless check: if(function_exists('add_filter')) {}. Also, you know that in_the_loop() only target posts in the main loop Commented Feb 23, 2016 at 10:45

1 Answer 1

1

Whoa, this Plugin was a nightmare to look through.

But I got a solution for you. In the description for your links, you can use the placeholder %FULLTITLE% instead of %TITLE%. %FULLTITLE% applies the filters for the title.

From nxs_functions_adv.php Lines 19 & 20:

if (preg_match('/%TITLE%/', $msg)) { $title = nxs_doQTrans($post->post_title, $lng); $msg = str_ireplace("%TITLE%", $title, $msg); }                    
if (preg_match('/%FULLTITLE%/', $msg)) { $title = apply_filters('the_title', nxs_doQTrans($post->post_title, $lng));  $msg = str_ireplace("%FULLTITLE%", $title, $msg); }          

Yes, this is actually how the code is formatted in the plugin.

This solves your first issue.

The next thing to do is to add Open Graph information to your header, and applying the title filter to it. For example for facebook (original from WPBeginner):

//Adding the Open Graph in the Language Attributes
function f711_add_opengraph_doctype( $output ) {
    return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes', 'f711_add_opengraph_doctype');

//Lets add Open Graph Meta Info

function f711_insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;
    echo '<meta property="og:title" content="' . get_the_title() . '"/>';
    echo '<meta property="og:type" content="article"/>';
    echo '<meta property="og:url" content="' . get_permalink() . '"/>';
    echo '<meta property="og:site_name" content="' . get_bloginfo( 'title' ) . '"/>';
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
        echo '<meta property="og:image" content="' . $default_image . '"/>';
    }
    else{
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
        echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "
";
}
add_action( 'wp_head', 'f711_insert_fb_in_head', 5 );

IMPORTANT be sure that you do not already include this information. For example YOAST SEO does that - and this could get confusing for facebook. They use this information as the title of their link boxes.

Finally, I'd suggest an alteration to your function: lose the in_the_loop() to allow the filter to be applied everywhere. Also, as @PieterGoosen pointed out, I skipped the needless check for function_exists().

function append_album_review_to_title( $title ) {

    global $post;
    $text = 'Album Review: ';

    if ( get_post_type( $post->ID ) == 'album_review' ){
        return $text . $title;
    } else {
        return $title;
    }

}
add_filter('the_title', 'append_album_review_to_title');
1
  • Much appreciated, thank you, up voted! When I originally asked and you answered, I couldn't comment or up vote. Since then, I've actually figured out the answer in a different way. I tried your way but due to inexperience, couldn't get it to work and also didn't want to mess with Yoast's meta as the site content/writers want to keep the backend control. I've found the answer in a diff direction and have posted it here. Again, thank you so much for the insight! Commented Mar 14, 2016 at 16:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.