3

I'm trying to add a custom field to a post title (sample page). To do so, I'm following the instructions given in this previous question, but the variable is not appearing in the title as expected. In fact, the exact same title appears. Any help would be appreciated.

The previous instructions indicate that the following code should be added to functions.php:

function wpse_224340_document_title($title){
    global $post; // make sure the post object is available to us
    if(is_singular()){ // check we're on a single post
      $release_author = get_post_meta(get_the_ID(), "release_author", true);
      if($release_author != "")
        { $title["title"].= " (" . $release_author. ")";}
    }
    return $title;
}
add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });

The variable in my case is propertysq, not release_author. But a simple find/replace resulted in the following...

What occurs when I implement this code?

The current page title is: HS0525 - Chaweng Noi - Horizon Homes Koh Samui This title has been inserted automatically by a Wordpress plugin "Yoast SEO." But after I disable this automatic title insertion, and insert the above code, the title inserted onto the page is identical to the previous page title: HS0525 - Chaweng Noi - Horizon Homes Koh Samui

Possible Sources of Error

Possible Source of Error #1: The aforementioned plugin/mechanism that is forcing its own <title> onto the page. I am currently researching if this is the cause. I'd like to avoid completely removing the plugin, but I may have to.

Possible Source of Error #2: Would I need to edit the following line to correspond to my theme/functions?

$release_author = get_post_meta(get_the_ID(), "release_author", true);

I tried replacing it with the following line, but it did not work.

$propertysq = ale_get_meta('propertysq');

I should note:

  • This site does not have a proper test environment setup where I can easily see PHP errors and dump vars.
  • Per the previous answer, I also remembered to comment out the <title> tag in my header.php.

edit: Here is the exact code I've inserted into functions.php:

add_filter("document_title_parts", "wpse_224340_document_title");
function wpse_224340_document_title($title){
    global $post; // make sure the post object is available to us
    if(is_singular()){ // check we're on a single post
      $propertysq = get_post_meta(get_the_ID(), "propertysq", true);
      if($propertysq != "")
        { $title["title"].= " (" . $propertysq. ")";}
    }
    return $title;
}
add_filter("after_setup_theme", function(){ add_theme_support("title-tag"); });
  • What is ale_get_meta()? I don't think this is a WordPress function - is it a custom function you've defined somewhere? Can you also edit in the exact code you've used, rather than just the code from the linked answer? I am of course assuming you've replaced every instance of release_author, but it would be better to see the exact code you have in case there's anything there causing an issue. Finally, it's very hard to develop without a test environment - can you set one up? Or temporarily put this site into debug mode? – Tim Malone Sep 15 '16 at 6:34
  • Also, to make sure the meta key is actually there, can you try echo'ing it out? Or try adding some random text to the title to see if that is working. Basically, you want to try to isolate the issue to work out exactly what is causing it - whether it's the post, the custom field, the title tag.. etc. – Tim Malone Sep 15 '16 at 6:37
  • @TimMalone. ale_get_meta() is indeed a custom function defined by the theme. I tried it because, as a diagnostic, if I change my <title> tag to: <title><?php echo esc_attr(ale_get_meta('propertysq')); ?></title>, the value of propertysq is inserted as the page title, as expected. I think this also suffices for your suggestion on echoing out the meta key? Also, I added my exact code to my original question. – cag8f Sep 15 '16 at 12:48
  • @TimMalone And with regards to a test environment, I agree that we should have one. You're preaching to the choir. Unfortunately I've inherited this site though, so right now there is no budget for that. After this (hopefully) small edit though, I will research what exactly it would entail with a site like ours and broach it with management. – cag8f Sep 15 '16 at 12:49
  • @TimMalone, in trying to isolate, I've ascertained that something is re-adding a <title>, even after I remove the <title> code from header.php. Furthermore, this mysteriously re-added <title> Is identical to the <title> I remove from header.php, which is: <title><?php wp_title('|', true, 'right'); ?></title> I would say that <title> is set by the Yoast SEO plugin, but in the plugin settings, I've cleared the field that defines these page titles.Ive ascertained that the functions.php code is responsible for re-adding the identical <title>.So I think Im defining $propertysq incorrectly. – cag8f Sep 15 '16 at 12:54
2

I assume that you want to use propertysq custom field/post meta in the title and you use Yoast SEO. ( Correct me if I'm wrong )

So, use this is your functions.php

function wpse239252_hook_title($title) {
global $post; // make sure the post object is available to us
if(is_singular()){ // check we're on a single post
  $propertysq = get_post_meta(get_the_ID(), "propertysq", true);
  if($propertysq != "") { //check if not empty
    $title = $propertysq.' - '.$title;
  }
}
return $title;
}

add_filter('wpseo_title', 'wpse239252_hook_title', 15, 1);

This will add propertysq field to your title as 1,002Sq Mt - HS0525 - Chaweng Noi - Horizon Homes Koh Samui in your given example, where propertysq = 1,002Sq Mt.

P.S: To get post_meta propertysq, use $propertysq = get_post_meta(get_the_ID(), "propertysq", true);

Let me if it works for you.

  • Thanks for that @hkchakladar. I tried it out--I inserted that code into functions.php, then commented out the <title> tag in header.php, but it did not work. The page title did not change when I added the code to functions.php. The page title did change when I commented out <title> in header.php, but it changed to the URL of the page. In my original question, I was trying to insert only the propertysq field into the title as a diagnostic test. Once I figure out how to do that, I'll insert additional fields and text into the title. Cheers! – cag8f Sep 15 '16 at 2:26
  • Is your Yoast SEO plugin active ? It works when seo plugin is active. Also can you share your header php part where you commented out title tag – hkchakladar Sep 16 '16 at 6:32
  • Yes, the Yoast SEO plugin is active. My immediate task is to disable it, then continue troubleshooting. But that will require time. I'll post back here when I've done so. In the meantime, here is the <title> code I commented out in header.php: <!-- <title><?php wp_title('|', true, 'right'); ?></title> --> – cag8f Sep 16 '16 at 12:01
  • I'm now able to temporarily disable the Yoast SEO plugin, just for testing purposes. The code you suggested will concatenate the current title with the $propertysq value. For testing purposes though, how would I simply replace the title with the $propertysq value? Would it be $title = $propertysq; – cag8f Sep 21 '16 at 2:04
  • Yes its is concatenated with current title with meta value. I think you wanted to do this. And to replace, you are correct, use $title = $propertysq; – hkchakladar Sep 21 '16 at 4:22
1

Because so many people want to manipulate it, it is no longer fashionable to include <title> tags in your header. In stead you put wp_head there and allow manipulation of the title by supporting it in your theme:

add_action('after_setup_theme','wpse239252_theme_init');
  function wpse239252_theme_init () {   
    add_theme_support('title-tag');
    }

Now, all kinds of (SEO) plugins may want to work with that, using the wp_title filter. If you do something using this filter and a plugin kicks in later, everything you have done may be lost. Now, I don't know which plugins you have installed, but you can make sure your filter is last by giving it a high priority, like this:

add_filter( 'wp_title', 'wpse239252_title_filter', 9999, 2 );
  function wpse239252_title_filter ($title,$separator) {
    ... do stuff with $title ...
    return $title;
    }
  • Before I try any more code, I'm going to first disable my SEO plugin (Yoast SEO) to see if I can further isolate the issue. As you said, signs are pointing to something else manipulating <title> despite my best efforts. Disabling the plugin though will require some time and coordination first. I'll post my results back here. – cag8f Sep 16 '16 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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