0

I have a custom meta box attached to a post type that displays ways for customers to submit new ideas. Customers are supposed to summarize their idea in 30 characters or less, which seems like a good use for post_excerpt. I'm not great with php (still learning) so any help with getting the echo statement right would be appreciated.

Below is my custom meta box as it stands. For simplicity I have stripped away everything else.

The post excerpt will display in the meta box but any edits will not save there. What am I doing wrong?

function idea_information() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="ideameta_noncename" id="ideameta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the location data if its already been entered
    $quicknote = get_post_meta($post->ID, '_ideas_quicknote', true);


    // Echo out the fields
    echo '<h3>Basics</h3>
          <input type="text" name="excerpt" id="excerpt" value="' . $post->post_excerpt  . '"/>';

}

// Save the Metabox Data

function whiteout_save_idea_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['ideameta_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.
    $station_meta['_ideas_quicknote'] = $_POST['_ideas_quicknote'];

    // Add values of $station_meta as custom fields

    foreach ($station_meta as $key => $value) { // Cycle through the $station_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
    }

}

add_action('save_post', 'whiteout_save_idea_meta', 1, 2); // save the custom fields

Related posts: Replace the Post Excerpt Meta Box with a Field in My Custom Meta Box

6
  • did you remove the build-in excerpt textarea?
    – Bainternet
    Commented Feb 26, 2011 at 14:04
  • I have tried it with it in and with using the remove_meta_box function. function remove_page_excerpt_field() { remove_meta_box( 'postexcerpt' , 'idea' , 'normal' ); } add_action( 'admin_menu' , 'remove_page_excerpt_field' );
    – MTT
    Commented Feb 26, 2011 at 14:32
  • other then trying do you see it on the post edit page?
    – Bainternet
    Commented Feb 26, 2011 at 15:02
  • Your question is do I see the excerpt box on the post edit page correct? Yes. With the default excerpt box unset in this post type the "meta field" excerpt box is saving changes so my question becomes at this point how do get the_excerpt to display the current value in the input field? If you add it as an answer I can give you credit for the solution. Thanks @Bainternet!
    – MTT
    Commented Feb 26, 2011 at 15:36
  • 30 characters or less? Imagine if your question were limited to 30 characters or less: "I have a custom meta box attached to a". ;)
    – Das
    Commented Feb 28, 2011 at 2:08

1 Answer 1

2

The only custom form field that you seem to be printing out is a text input named "excerpt". In your processor script you are checking for a $_POST var named "_ideas_quicknote". Since it is not actually sent in the POST request, your processor will never see it. Please try the following:

<?php
print "\n" . '<h3>Basics</h3>';
print "\n" . '<input type="text" name="_ideas_quicknote" id="myplugin_ideas_quicknote" value="' . esc_attr( $quicknote )  . '"/>';
3
  • I changed the name to excerpt and replaced _ideas_quicknote elsewhere to excerpt. Everything with your script works great now. Thanks @mfields!
    – MTT
    Commented Feb 28, 2011 at 4:14
  • No worries. Glad it worked :) I would suggest that you swap names and go with "_ideas_quicknote elsewhere" instead of "excerpt". The reason is that "excerpt" looks like something that WordPress already uses while "_ideas_quicknote elsewhere" looks entirely custom. IMHO it's best to use custom names + prefixes wherever possible. Swapping them might save you a headache in a year when you revisit the code or another developer is modifying it.
    – mfields
    Commented Feb 28, 2011 at 8:14
  • That makes a lot of sense, I'll do that. :)
    – MTT
    Commented Mar 2, 2011 at 13:21

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.