Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is my custom meta box, here text area showing html tags.i used tinymc textaea editor. and i try to use wp_editor also. but data not saving.if it saved, i don't know how to call it. please help me find solution --THANK YOU--



<?php
add_action("admin_init", "admin_init");
    add_action('save_post', 'save_points');
    function admin_init(){
    add_meta_box("productInfo-meta", "Product Details", "product_meta_options", "product", "normal", "low");}

    function product_meta_options(){
        global $post;
        $custom = get_post_custom($post->ID);
        $category = $custom["category"][0];
        $brand = $custom["brand"][0];
        $features = $custom["features"][0];
        $holds = $custom["holds"][0];
?>
<table>
    <tr>
        <td>Category
        <td> <input type="text" size="100" name="category" value="<?php echo $category; ?>" /> </td>
    </tr>
    <tr>
        <td>Brand
        <td> <input type="text" size="100" name="brand" value="<?php echo $brand; ?>" /> 
    </tr>
    <tr>
        <td>Features</td>
        <td><textarea rows="20" cols="100" name="features"><?php echo $features; ?>
    </tr>
    <tr>
        <td></td>
        <td>
        <?php wp_editor( $content, 'test_content', $settings = array('textarea_name'=>'test_content','textarea_rows'=>20) );?>
        </td>
    </tr>
    <tr>
        <td>Holds
        <td><textarea rows="20" cols="100" name="holds"><?php echo $holds; ?></td>
    </tr>
</table>

<?php
}

function save_points(){
    global $post;
    update_post_meta($post->ID, "category", $_POST["category"]);
    update_post_meta($post->ID, "brand", $_POST["brand"]);
    update_post_meta($post->ID, "features", $_POST["features"]);
    update_post_meta($post->ID, "holds", $_POST["holds"]);
}?>


share|improve this question

1 Answer 1

There are a number of things going wrong in your code. I had to install it to see them all.

First, You don't want global $post. save_post will pass the post ID. Add a parameter to your callback.

function save_points($post_id){
    update_post_meta($post_id, "category", $_POST["category"]);
    update_post_meta($post_id, "brand", $_POST["brand"]);
    update_post_meta($post_id, "features", $_POST["features"]);
    update_post_meta($post_id, "holds", $_POST["holds"]);
}

Note: that only passes the post ID, not the whole post object. If you needed the whole object (which you don't) you could request a second parameter.

Second, lines like this one-- $brand = $custom["brand"][0];-- are triggering notices. You should be checking for the key before trying to use it.

$brand = (!empty($custom["brand"][0]) ? $custom["brand"][0] : '';

Third, that solves potential problems with lines like this one-- <td><textarea rows="20" cols="100" name="features"><?php echo $features; ?>-- by setting the value that are echoing to something.

Fourth, you have a number of unclosed <td> tags causing very broken markup.

Fifth, wp_editor, possibly the default instance, does not seem to like manually created textareas. I don't know of a workaround but maybe there is one. I would love to know if someone out there has a solution. In the meantime, using wp_editor for all of the textareas seems to work.

Sixth, see this answer for issues with doing what you are doing if using less than (it looks to me) WordPress 3.5.

Seventh, saving the keys as you are you going to have your meta fields show up in the default meta fields box as well. Save your fields with a leading underscore.

Eight, you weren't saving your content from the wp_editor in your original code at all.

Nine, you weren't retrieving you post content either.

Ten, you are running this code for all post saves, not just for your custom post type.

Eleven, you probably want to check that you are not dealing with a post revision, as in this example from the Codex.

Twelve, you would really do well to sanitize your POST data before shoving it into the database. POST data is not safe. Though you can't see it in the URL is trivial to alter.

Put that all together, except for the data validation which I will leave to you since this is a marathon answer already :

add_action("admin_init", "admin_init");
add_action('save_post', 'save_points', 1, 2);
function admin_init(){
  add_meta_box("productInfo-meta", "Product Details", "product_meta_options", 'product', "normal", "low");
}

function product_meta_options(){
  global $post;
  $custom = get_post_custom($post->ID);
  $category = (!empty($custom["_category"][0])) ? $custom["_category"][0] : '';
  $brand = (!empty($custom["_brand"][0])) ? $custom["_brand"][0]  : '';
  $features = (!empty($custom["_features"][0])) ? $custom["_features"][0]  : '';
  $holds = (!empty($custom["_holds"][0])) ? $custom["_holds"][0]  : '';
  $content = (!empty($custom["_custom_content"][0])) ? $custom["_custom_content"][0]  : '';
?>
<table>
    <tr>
        <td>Category
        <td> <input type="text" size="100" name="category" value="<?php echo $category; ?>" /> </td>
    </tr>
    <tr>
        <td>Brand
        <td> <input type="text" size="100" name="brand" value="<?php echo $brand; ?>" /> </td>
    </tr>
    <tr>
        <td>Features</td>
        <td><?php wp_editor( $features, 'features', $settings = array('textarea_rows'=>20) ); ?></td>
    </tr>
    <tr>
        <td>Content</td>
        <td>
        <?php wp_editor( $content, 'custom_content', $settings = array('textarea_rows'=>20) );?>
        </td>
    </tr>
    <tr>
        <td>Holds</td>
        <td><?php wp_editor( $holds, 'holds', $settings = array('textarea_rows'=>20) ); ?></td>
    </tr>
</table>

<?php
}

function save_points($postid,$post){
  global $_POST;
  // set the ID to the parent post, not the revision
  $postid = (wp_is_post_revision( $postid )) ? wp_is_post_revision( $post ) : $postid;
  $post_type = get_post_type( $postid );
  if ('product' == $post_type) {
    update_post_meta($postid, "_category", $_POST["category"]);
    update_post_meta($postid, "_brand", $_POST["brand"]);
    update_post_meta($postid, "_features", $_POST["features"]);
    update_post_meta($postid, "_holds", $_POST["holds"]);
    update_post_meta($postid, "_custom_content", $_POST["custom_content"]); // save the data
  }
}
share|improve this answer
    
@s_ha_dum...thank you for your answer.I just need to know why this editor field not saving data and how can i call back this content.... ?php wp_editor( $content, 'test_content', $settings = array('textarea_name'=>'test_content','textarea_rows'=>20) );?> –  Shihab Malayil Jun 30 '13 at 14:58
    
thank you s_ha_dum. –  Shihab Malayil Jul 3 '13 at 8:27
    
thank you s_ha_dum. pastebin.com/DpJAUufP this is my code before you fix the errors. now i just want to know how print the code in my page. –  Shihab Malayil Jul 3 '13 at 8:52
    
Sorry? How to print what code? –  s_ha_dum Jul 3 '13 at 13:21
    
how can i show the field content in my page?. i mean how can i call the field content. <?php echo esc_html( get_post_meta( get_the_ID(), 'brand', true ) ); ?> is it correct ?. –  Shihab Malayil Jul 5 '13 at 20:34

Your Answer

 
discard

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.