Breon
Breon

Reputation: 95

Undefined variable: post in

I'm debugging my wordpress theme and I keep getting this notification in my functions.php file:

Undefined variable: post in (myfolders)/functions.php on line 961

This is line 961

echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));

The code is for a custom tinymce for my custom post type(Portfolio) project description.

What is causing variable to be undefined? Here is the code in it's entirety:

add_action( 'add_meta_boxes', 'mpc_add_description_meta_box' );
// Add the projects Meta Box
function mpc_add_description_meta_box() {
    add_meta_box('mpc_add_description_meta_box', 'Project Description', 'mpc_add_description_meta_box_callback', 'portfolio', 'normal', 'high');
}

// the description output
function mpc_add_description_meta_box_callback() {
    global $post;
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="mpc_projects_description_noncename" id="mpc_projects_description_noncename" value="'.wp_create_nonce(plugin_basename(__FILE__)).'" />';
    // Get the location data if its already been entered
    $input = get_post_meta($post->ID, 'mpc_projects_description', true);
   // echo '<input type="text" name="mpc_projects_description" value="' . $input  . '" "  />';
    wp_editor($input, 'mpc_projects_description', array('textarea_name' => 'mpc_projects_description', 'editor_css' => '<style>#wp-mpc_projects_description-editor-container{background-color:white;style="width:100%;}</style>'));

    echo '<table id="post-status-info" cellspacing="0"><tbody><tr><td id="wp-word-count">Word count: <span class="word-count_2">';
    $words = strip_tags($input);
    $count = str_word_count($words, 0);
    echo $count;
    echo '</span></td>
    <td class="autosave-info">
    <span class="autosave-message">&nbsp;</span>
    </td>
    </tr></tbody></table>';
}

//save the data
add_action('save_post', 'mpc_save_description_meta', 1, 2); // save the custom fields
function mpc_save_description_meta($post_id, $post) {
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['mpc_projects_description_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.
    $mpc_description_meta['mpc_projects_description'] = $_POST['mpc_projects_description'];
    // Add values of $mpc_description_meta as custom fields
    foreach ($mpc_description_meta as $key => $value) { // Cycle through the $mpc_description_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }
}



echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));

Upvotes: 0

Views: 6758

Answers (1)

Nathan Dawson
Nathan Dawson

Reputation: 19308

The post global is available inside the functions but you haven't set it outside.

Change:

echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));

To:

global $post;
echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));

This could also depend on where you use this code. If you fire the code before the post object is set then you'll get this error.

If it's just in functions.php then change it to:

function wpse_post_test() {
    global $post;
    echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));
}
add_action( 'wp', 'wpse_post_test' );

After reviewing your question again it appears you are attempting to output this inside functions.php. What are you trying to achieve?

Upvotes: 1

Related Questions