0

I want to make a description input field inside the normal post adding page (in the dashboard) so each time I'm posting something I'll be able to handle the description as I like. Then I would target this name field to add something like this inside my header:

echo '<meta name="description" content="' . $mycustomfield . '" />' . "\n";

So at the end it would look like this (correct me if needed):

function mycustomdescription() {
   global $post;
   if ( is_single() ) {
      echo '<meta name="description" content="' . $mycustomfield . '" />' . "\n";
   }
}
add_action( 'wp_head', 'mycustomdescription' , 2 );

How do I do this in the right way?

0

If you want to fetch the data from a custom field named wpse_desc:

custom field

then here's one way to do that with your code snippet:

function wpse_custom_meta_description() 
{
    // Nothing to do
    if ( ! is_single() )
        return;

    // Fetch our custom field value
    $desc = get_post_meta( get_queried_object_id(), 'wpse_desc', true );

    // Output
    if( ! empty( $desc ) )
        printf( 
            '<meta name="description" content="%s" />',
            esc_attr( trim( $desc ) )
        );
}
add_action( 'wp_head', 'wpse_custom_meta_description' , 2 );

It's important to escape the value of the content attribute, because you don't want e.g. authors of your site to be able to write something like:

><script>alert('Hello World!');</script><

We might also want to strip the tags out with wp_strip_all_tags() and limit the word count with wp_trim_words(), that also uses wp_strip_all_tags(), but we should keep in mind that it would still allow very long words. If you need a maximum length, then you might need to look further into that, I'm sure there are various approaches out there.

Here we prefix the custom field name with the wpse_ prefix, to avoid possible name collisions.

Hope you can adjust it to your needs, like using post excerpt or post content fallbacks.

9
  • Thanks, birgire! But can you explain me something... Does this mean the $desc will be outputted outside the meta tag >? And one more thing, how do I actually put the input field inside the dashboard? what should I use
    – wootopia
    Aug 5 '16 at 11:57
  • And thank you for alerting me about the escaping value of the content!
    – wootopia
    Aug 5 '16 at 11:58
  • Omg! Silly me... THANK YOU, Birgire! Very, very much for the help!
    – wootopia
    Aug 5 '16 at 12:07
  • Here we use the custom field support of the post post type. So you can add a new custom field named wpse_desc (I added a screenshot). The custom field value will be inserted as the meta description value inside the <head> tag.. @wootopia
    – birgire
    Aug 5 '16 at 12:07
  • Thank you for detailed explanation! I also wasn't aware about "Custom Fields", so in case someone will have problem with it, here's the link: codex.wordpress.org/Using_Custom_Fields
    – wootopia
    Aug 5 '16 at 12:27

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.