3

I want to manipulate the html-title tag, so <title> these not the content of the title-tag!

I use child-themes and it's inevitable, because the template have to be easy to update. So I have to use the functions.php or the header.php in the child-folder.

At moment I try it with the functions.php

At moment I have following situation:

<!-- Start WP_HEAD
================================================== -->

<!-- This site is optimized with the Yoast WordPress SEO plugin v1.5.3.3 - https://yoast.com/wordpress/plugins/seo/ -->
<title>My awesome title-content</title>
<meta name="robots" content="noindex,follow,noodp,noydir"/>
<link rel="canonical" href="WEBSITE" />
<meta property="og:locale" content="de_DE" />

and I want to have these situation:

The focus are on the title-tag

<!-- Start WP_HEAD
================================================== -->

<!-- This site is optimized with the Yoast WordPress SEO plugin v1.5.3.3 - https://yoast.com/wordpress/plugins/seo/ -->
<title itemprop="name" id="company_name">My awesome title-content</title>
<meta name="robots" content="noindex,follow,noodp,noydir"/>
<link rel="canonical" href="WEBSITE" />
<meta property="og:locale" content="de_DE" />

my current try in the functions.php in the child-folder are:

add_action('get_header', 'blog_template_add_ob_start');
add_action('wp_head', 'blog_template_add_ob_end_flush', 100);
function blog_template_add_ob_start() {
    ob_start('blog_template_add_filter_wp_head_output');
}
function blog_template_add_ob_end_flush() {
    ob_end_flush();
}
function blog_template_add_filter_wp_head_output($output) {

    if (is_single()) {
        $output = preg_replace('/<title>(.*?) - (.*?)<\/title>/', '<title><span itemprop="name" id="company_name">$2</span> &raquo; $1</title>', $output);
        #$output = str_ireplace('<meta property="og:url" content="' . $url . '" />', '<meta     property="og:url" content="' . esc_attr(esc_url($altUrl)) . '" />', $output);
    }
    return $output;
}

my first try was these: first:

function add_itempromp_to_title( $str )
{
    #$str = preg_replace('/<title>(.*?) - (.*?)<\/title>/', '<title><span itemprop="name" id="company_name">$2</span> &raquo; $1</title>', $str);
    return $str;
}   
add_filter( 'wp_head', 'add_itempromp_to_title', 99 );

I had try these: Manipulating wp_head content but isn't work anymore.

Does anyone have an idea?

The Yoast-Plugin-Version are 1.5.3.3

The WP-Version are 3.8.3

6
  • Your Approach will NEVER WORK. A span tag is not allowed in the Title Tag. Only Text is allowed in the Title Tag. Meta Tags and Script Tags along with the Title Tag in the Head Section. See How to Write Title Tags For Search Engine Optimization
    – eyoung100
    Commented Jun 24, 2014 at 19:34
  • You might be able to add this as a meta tag, but I'm assuming you want to use it for some purpose in WordPress, plus the search engine will crawl the meta tag for help in indexing the site.
    – eyoung100
    Commented Jun 24, 2014 at 19:46
  • okay, but this solve not the mainproblem, that I can't manipulate the generated head of yoast plugin. Now I try to edit a meta-tag with the same result, it doesn't work.
    – Wombat
    Commented Jun 24, 2014 at 20:02
  • First of all, what are you trying to accomplish by editing the head section? I think I know what you're trying to do, but I want clarification before I give you an approach.
    – eyoung100
    Commented Jun 24, 2014 at 20:08
  • after I have read your comment I change my idea of editing the title-tag. But now the idea is to manipulate the og:site-meta tag, that also be add by the yoast seo plugin. So I want add to this meta-tag a itemprop. As I know is this compatible with the wc3-standard.
    – Wombat
    Commented Jun 25, 2014 at 23:58

2 Answers 2

2

I don't think adding <span itemprop> to the <title> is a good idea... If you can not use any existing tags like <h1> for that, you may consider adding meta tag: <meta itemprop="name" content="Company Name">. You can add this using wp_head, but keep in mind it should be within itemscope.

If you really going after changing title tag, Yoast SEO has it's own filter 'wpseo_title', so you need to call that one.

add_filter('wpseo_title', 'filter_title'); function filter_title($title) { // do something to the $title here return $title; }

0

I still don't know why you're trying to pass the item property through the head tag. I have a gut feeling you're trying to tell Google abut a particular item. For example:

  • itemprop is brown_shoes
  • itemprop is red_hat

In each of these cases, you want Google to know that you sell brown shoes or a red hat.

This would be better suited if you created a wp_products table in your database, and in that table, put the following:

  1. product_id - This will be the Primary Key
  2. product_desc - Description
  3. product_image - URL to image
  4. product_price - Price
  5. product_keywords - These will be keywords that you can pass to the meta tag

Create a ProductPage Template, using a Custom WP_Query and this Plugin, URL Params, to pass the product_id field to every page. In the HEAD SECTION of the template add:

<meta name="keywords" content="<? php echo $product_keywords; ?>"

Since PHP completes the WP_Query before rendering the page, this is perfectly acceptable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.