First of all: don’t edit third party theme directly!
Use what is called a child theme so the next time you update the main theme, your changes won’t be overwritten by the new files.
In your child theme, you will have a functions.php file, where you override the main functionalities from WordPress by hooking actions and filters.
In order to customise the title of your blog, you can hook the filter “wp_title
” like so:
add_filter('wp_title', 'my_custom_title');
function my_custom_title( $title )
{
// Return my custom title
return sprintf("%s %s", $title, get_bloginfo('name'));
}
To add a custom title to each page, you’d better off using a meta box with a custom field. There’s no simple way of doing this but here’s a concise snippet for adding the metabox with poor validation (use carefully):
<?php
add_action( 'add_meta_boxes', 'so_add_post_title_metabox' );
add_action( 'save_post_page', 'so_save_post_title_metabox_data' ); // in this case, saving post of post_type "page"
function so_add_post_title_metabox() {
add_meta_box( 'post_title_meta', __( 'Title', 'domain-name' ), 'so_post_title_metabox_callback', 'page' ); // same as above
}
function so_post_title_metabox_callback($post) {
$post_id = $post->ID;
$post_title_meta = get_post_meta( $post_id, 'post_title_meta', true);
?>
<label><?php _e( 'Custom post title', 'domain-name' )?></label>
<input type="text" name="post_title_meta" value="<?php echo $post_title_meta ?>">
<?php
}
function so_save_post_title_metabox_data($post_id) {
if ( !isset($_POST['post_title_meta'] ) ) {
return $post_id;
}
$post_title_meta = sanitize_text_field( $_POST['post_title_meta'] );
update_post_meta( $post_id, 'post_title_meta', $post_title_meta );
}
To retrieve your custom field and render the custom title:
<?php
add_filter( 'wp_title', 'so_custom_title' );
function so_custom_title( $title ) {
global $post;
$post_id = $post->ID;
if( $custom_title = get_post_meta( $post_id, 'post_title_meta', true ) ) {
return esc_html( $custom_title );
}
return $title;
}
Another way (not recommended in this case) to do that programmatically is by using template parts in your child theme.
There’s also plenty of third party plugins to ease the job like WP SEO from Yoast (full featured SEO plugin) and others that will provide just that functionality (can’t mention as I don’t use any).