WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I added the code below to the plugin that I'm working on to display the values of the custom fields but it shows up but blank.

function mySEO() {

// Add hook for admin <head>
add_action('admin_head', 'mySEO');
// Add hook for front-end <head>
add_action('wp_head', 'mySEO');

add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );

When I add the content of the called file directly to the folder, it shows perfectly. However, I want the user of the plugin to only put value and not touch the code. Below is the code in the called file. Any ideas of what the problem could be?

if ( is_single() || is_page() ) {
    $title = get_post_meta( $post->ID, 'title', true ); 
    if ( $title )
        echo $title . ' | ' . get_bloginfo('name');
        wp_title('') . ' | ' . get_bloginfo('name');

if ( is_single() || is_page() ) {

    if ( have_posts() ) : while ( have_posts() ) :  the_post();

    $description = get_post_meta( $post->ID, 'description', true ); 
    printf( '<meta name="description" content="%s" />',
        ( $description ? $description : get_the_excerpt_rss() )

    endwhile; endif;

} elseif ( is_home() ) {
    printf( '<meta name="description" content="%s" />',

if ( is_single() || is_page() ) {

    if ( have_posts() ) :   while ( have_posts() ) : the_post();

    $keywords = get_post_meta($post->ID, 'keywords', true);
    printf ( '<meta name="keywords" content="%s" />',
          ( $keywords ? $keywords : the_excerpt_rss(); ?

    endwhile; endif;

} elseif ( is_home() ) {
    printf( '<meta name="keywords" content="%s" />',

share|improve this question
This code is quite difficult to read, would be nice if you added line breaks and removed unnecessary opening and closing PHP tags... Also, use bold to highlight key words and not all the text, as this is not smooth to read as well. – brasofilo Dec 23 '12 at 3:15
@brasofilo I've submitted an edit that should clean up his code somewhat – shea Dec 23 '12 at 4:17
@bungeshea, nice touch :), but still needs another reviewer to aprove the change. Dz slick, please check: Why do people not split long lines when posting code here? and also a good IDE... – brasofilo Dec 23 '12 at 4:33
Thanks for the edit guys. – Dz.slick Dec 23 '12 at 5:12
up vote 0 down vote accepted

aside from a few syntax errors in there with your printfs, the crux of the problem is things like this:

$title = get_post_meta( $post->ID, 'title', true );

won't work in your included file because there is no $post in this scope, You have to globalize $post before you can use it:

global $post;
$title = get_post_meta( $post->ID, 'title', true );
share|improve this answer
Thank a lot Milo. I'll try it out. – Dz.slick Dec 23 '12 at 17:04
Thank you so much Milo. That did it. – Dz.slick Dec 23 '12 at 19:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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