I’m trying to create a custom wordpress title for a posts made under a custom post. Basically, its a version changelog for an application. I would like to only input the version number in the title field, and the output has to be standardized with a string accordingly.

My custom post type is ‘custom_version’ and the title output that I’m looking for is “Application has been updated to version “.

I’ve understood that this can be achieved using add_filter and I’ve tried playing with this code for days now, but I’m not really a PHP Pro so help is much appreciated 🙂 Here’s the code:

add_filter('the_title', 'new_title');
function new_title($title) {
    global $post, $post_ID;
    $title['custom_version'] = 'Application has been updated to v'.$title;
    return $title;
}

1 Answer
1

The issue is that you are mixing up the $title variables. $title is the parameter passed to the new_title function, but then you use it as an array: $title['custom_version']. Try this:

add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
    if('custom_version' == get_post_type($id))
        $title="Application has been updated to v".$title;
    return $title;
}

I’d also highly recommend prefixing your function with a unique prefix because you may run into another plugin/theme using a function called new_title, which will reek havoc!

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *