1

Below is the code I wrote for header.php so that the description for a product category can be obtained and used for the <meta> description tag when viewing such a "page" on the site.

Is there a more "WordPress way" to do this such as a function I should be using instead of referencing the description property of the object returned by get_queried_obj()? I originally wanted to use term_description() but that adds <p> tags so I didn't end up going that route (I know I could strip them with a built-in function but that doesn't seem like good practice).

The elseif block below is the key piece of code:

<?php
if (is_single() || is_page()) {
    $description = get_post_meta(get_the_ID(), 'description', true);
} elseif (is_tax()) {
    //Product categories
    $obj_temp = get_queried_object();
    $description = $obj_temp->description;
} else {
    $description = get_bloginfo('description');
} 
if (trim($description) !== '') {
    ?>
    <meta name="description" content="<?php echo htmlspecialchars($description); ?>" />
    <?php
}
?>

1 Answer 1

1

Is there a more "WordPress way" to do this...

I don't really know what you mean by a more WordPress way, but in general, the way you are getting the term description is a more reliable way in doing it. This way, you do not need to access the $post global which so many people use. Just for interest sake, please see my post here on why to avoid the $post global

I would however get the queried object from the $GLOBALS['wp_the_query']->get_queried_object() which is even more reliable than get_queried_object(), and then pass that through get_term() to apply the term filters to the term

} elseif ( is_tax() ) {
    //Product categories
    $obj_temp    = get_term( $GLOBALS['wp_the_query']->get_queried_object() );
    $description = $obj_temp->description;
}

Another probable way is to get the term slug and taxonomy from the URL and then get the complete term object to get the description

} elseif ( is_tax() ) {
    //Product categories
    $obj_temp    = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
    $description = $obj_temp->description;
}        

EDIT - from comments

How is $GLOBALS['wp_the_query']->get_queried_object() more reliable than get_queried_object()

We actually had a discussion about this which lead to my answer to the following question

You should really read that as I explained everything in detail there. Just a quick proof of concept to add to that answer in this context, in any template, preferably a page or taxonomy template, run the following and see the results

var_dump( $GLOBALS['wp_the_query']->get_queried_object() );
var_dump( get_queried_object() );

query_posts( 's=crap' );


var_dump( $GLOBALS['wp_the_query']->get_queried_object() );
var_dump( get_queried_object() );

Also, what's an undesirable situation that might result if the term filters didn't get applied to the term

Term data is not sanitized, specially when used directly from a super global, so it may contain injected malicious code or hacked URL's to some hardcore porn site which you do not want. Applying the term filters sanitizes the data according to term field to make it save to use.

Sanitizing, validating and escaping data should be a habit, and not a debatable extra. Hackers love non-sanitized and non-validated data, they can inject any malicious code into your site and through that gain access to your site. The best advice I can ever tell you forever is, ALWAYS SANITIZE, VALIDATE AND ESCAPE all data according to data type, regardless of where it comes from (even if it comes from you yourself) or what it will be used for. This way, you make it extremely hard for hackers to hack your site

2
  • How is $GLOBALS['wp_the_query']->get_queried_object() more reliable than get_queried_object() (I'm not saying it isn't, just wondering since I don't know the answer)? Also, what's an undesirable situation that might result if the term filters didn't get applied to the term? Thanks
    – g491
    Commented Mar 15, 2016 at 0:46
  • Please see my updated answer. It was a bit to much for comments ;-) Commented Mar 15, 2016 at 4:33

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.