Reputation: 972
I am using a wordpress theme called Kalium along with some modified parts for my site here: https://www.idee-creative.co.uk
I have added custom fields on every page type so I can easily add my own title and description tags so they are customisable on each and every page. The code I have used for that is here:
<title><?php the_field('seo_page_title'); ?></title>
<meta name="description" content="<?php the_field('seo_page_description'); ?>"/>
Pulling in the custom fields and displaying them in the header of my pages.
The trouble I am having is that, when I look at the source code of a page, Wordpress seems to add it's own tag. So my site has two tags. I would prefer to keep my own customised ones and get remove the Wordpress version.
I can't seem to find where they are coming from, I've checked my header.php file and there doesn't seem to be anything pulling the title tag in apart from my own custom code above... Here's the complete header.php code if it helps:
<?php
/**
* Kalium WordPress Theme
*
* Laborator.co
* www.laborator.co
*/
// Get Menu Type To Use
$main_menu_type = get_data( 'main_menu_type' );
?>
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" <?php language_attributes(); ?>> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" <?php language_attributes(); ?>> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" <?php language_attributes(); ?>> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html <?php language_attributes(); ?>> <!--<![endif]-->
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php the_field('seo_page_title'); ?></title>
<meta name="description" content="<?php the_field('seo_page_description'); ?>"/>
<!-- Inclide Schema Markup File
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<?php include('json-ld.php'); ?><script type="application/ld+json"><?php echo json_encode($payload); ?></script>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php
if ( apply_filters( 'kalium_show_header', true ) ) :
// Theme Borders
if ( get_data( 'theme_borders' ) ) :
get_template_part( 'tpls/borders' );
endif;
// Mobile Menu
include locate_template( 'tpls/menu-mobile.php' );
// Top Menu
if ( $main_menu_type == 'top-menu' || get_data( 'menu_top_force_include' ) ) {
include locate_template( 'tpls/menu-top.php' );
}
// Sidebar Menu
if ( $main_menu_type == 'sidebar-menu' || get_data( 'menu_sidebar_force_include' ) ) {
include locate_template( 'tpls/menu-sidebar.php' );
}
endif;
?>
<div class="wrapper" id="main-wrapper">
<?php
// Kalium Start Wrapper
do_action( 'kalium_wrapper_start' );
// Show Header
if ( apply_filters( 'kalium_show_header', true ) ):
// Main Header
get_template_part( 'tpls/header-main' );
endif;
?>
++++++++++++UPDATE++++++++++++
This is the function I have found that creates the title I believe... Its in a hidden away include file in the parent theme...
// Open Graph Meta
function kalium_wp_head_open_graph_meta() {
global $post;
// Only show if open graph meta is allowed
if ( ! apply_filters( 'kalium_open_graph_meta', true ) ) {
return;
}
// Do not show open graph meta on single posts
if ( ! is_singular() ) {
return;
}
$featured_image = $post_thumb_id = '';
if ( has_post_thumbnail( $post->ID ) ) {
$post_thumb_id = get_post_thumbnail_id( $post->ID );
$featured_image = wp_get_attachment_image_src( $post_thumb_id, 'original' );
}
// Excerpt, clean styles
$excerpt = kalium_clean_excerpt( get_the_excerpt(), true );
?>
<meta property="og:type" content="article"/>
<meta property="og:title" content="<?php echo esc_attr( get_the_title() ); ?>"/>
<meta property="og:url" content="<?php echo esc_url( get_permalink() ); ?>"/>
<meta property="og:site_name" content="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"/>
<meta property="og:description" content="<?php echo esc_attr( $excerpt ); ?>"/>
<?php if ( is_array( $featured_image ) ) : ?>
<meta property="og:image" content="<?php echo $featured_image[0]; ?>"/>
<link itemprop="image" href="<?php echo $featured_image[0]; ?>" />
<?php if ( apply_filters( 'kalium_meta_google_thumbnail', true ) ) : $thumb = wp_get_attachment_image_src( $post_thumb_id, 'thumbnail' ); ?>
<!--
<PageMap>
<DataObject type="thumbnail">
<Attribute name="src" value="<?php echo $thumb[0]; ?>"/>
<Attribute name="width" value="<?php echo $thumb[1]; ?>"/>
<Attribute name="height" value="<?php echo $thumb[2]; ?>"/>
</DataObject>
</PageMap>
-->
<?php endif; ?>
<?php endif;
}
add_action( 'wp_head', 'kalium_wp_head_open_graph_meta', 5 );
++++++++++++ UPDATE 2 ++++++++++++
Sorry for the constant updates, but I also have this in another part of the theme:
// Title Parts
function kalium_wp_title_parts( $title, $sep, $seplocation ) {
$kalium_separator = apply_filters( 'kalium_wp_title_separator', ' – ' );
if ( empty( $sep ) ) {
return $title;
}
$title_sep = explode( $sep, $title );
if ( ! is_array( $title_sep ) ) {
return $title;
}
if ( $seplocation == 'right' ) {
$title = str_replace( $sep . end( $title_sep ), $kalium_separator . end( $title_sep ), $title );
} else {
$title = str_replace( reset( $title_sep ) . $sep, reset( $title_sep ) . $kalium_separator, $title );
}
return $title;
}
add_filter( 'wp_title', 'kalium_wp_title_parts', 10, 3 );
Upvotes: 1
Views: 4702
Reputation: 15949
You should use the wp_title hook like so :
add_filter( 'wp_title', 'my_custom_title_function', 20 );
and then just define the function
function my_custom_title_function( $title ) {
// use own function to produce title, for example:
// return str_replace('Old title', 'New title', $title);
}
or in your case it might be for example:
function my_custom_title_function( $title ) {
return the_field('seo_page_title');
}
( assuming the_field
is a valid and available function at that stage. )
if you want to totally remove the <title>
tag :
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
Or in a simpler way (a child theme,)
remove_theme_support( 'title-tag' )
You can also use wp_head
with ob_start
and such, but not recommended ..
After op edit :
If there is another filter , just remove it ..
remove_filter( 'wp_title', 'kalium_wp_title_parts', 99); / or 1
I also suggest you read a bit about wp actions, filters and hooks . it's fundamental for wp development ..
Upvotes: 2