3

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;
}

2 Answers 2

10

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!

4
  • 1
    Thanks for the response tollmanz, I tried your code, but the problem is, it replaces ALL titles including that of pages..Here is a reference of where its being done: titaniumtrack.com/changelog/titanium-backup/3-0-6
    – shahz
    Aug 22, 2011 at 4:46
  • I am actually trying to add this filter only to custom post types with identified "custom_version"..
    – shahz
    Aug 22, 2011 at 4:55
  • Edited...I check that the post_ID is associated with a post of post type "custom_version" before manipulating the title. Sorry about the initial version. I didn't realize that it needed to be for only the one CPT.
    – tollmanz
    Aug 22, 2011 at 4:57
  • You may have Yoast installed; if you do you'll want to look at add_filter 'wpseo_title'. Jul 5, 2018 at 10:36
0

I think WooCommerce did a good job on showing possible ways to verify if you really want to override the title using the_title filter.

Sometimes, you may change the title that will appear in your category list, and other places you don't want to.

Here is how they do:

# plugins/woocommerce/includes/wc-page-functions.php

function wc_page_endpoint_title( $title ) {
    global $wp_query;
    if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) {
        $endpoint       = WC()->query->get_current_endpoint();
        $action         = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
        $endpoint_title = WC()->query->get_endpoint_title( $endpoint, $action );
        $title          = $endpoint_title ? $endpoint_title : $title;
        remove_filter( 'the_title', 'wc_page_endpoint_title' );
    }
    return $title;
}

add_filter( 'the_title', 'wc_page_endpoint_title' );

More important than rewriting the title, are the methods and checks they do...

! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() etc

Keep those in mind before overrind titles you don't want to, or in places you shouldn't.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .