Skip to content

Instantly share code, notes, and snippets.

@MickeyKay
Last active August 29, 2015 14:04
Show Gist options
  • Save MickeyKay/6790b1c366a4b3bacabf to your computer and use it in GitHub Desktop.
Save MickeyKay/6790b1c366a4b3bacabf to your computer and use it in GitHub Desktop.
[WordPress] Override the default post title with a custom field.
// Only override the title on the front-end.
if ( ! is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
add_filter( 'the_title', 'PREFIX_override_post_title', 10, 2 );
}
/**
* Override the_title() with a custom field if it is specified.
*
* @param string $title Original post title.
*
* @return string $title Update post title.
*/
function PREFIX_override_post_title( $title, $id ) {
$custom_title = get_post_meta( $id, 'custom_title', true );
if ( ! empty( $custom_title ) ) {
$title = $custom_title;
}
return $title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment