0

I've been drilling with codes for so long.

I was trying to create a metabox with a simple textare on it.

Been stuck for almost 10 hours tyring to solve this.

Still up until right now my meta box information won't save.

Here's my codes:

add_action("admin_init", "custom_product_metabox");

function custom_product_metabox(){
  add_meta_box("custom_product_metabox_01", "Product Description", "custom_product_metabox_field", "portfolio_page", "normal", "low");
}

function custom_product_metabox_field(){
    global $post;

    $data = get_post_custom($post->ID);
        $val = isset($data['custom_product_input']) ? esc_attr($data['custom_product_input'][0]) : 'no value';

        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

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

        echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
    }

add_action("save_post", "save_detail");

function save_detail( $post_id, $post, $update ){
    // global $post;

    if(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE){
             return $post_id;
        }

        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    if( !current_user_can( 'edit_post' ) ) return;

    update_post_meta($post_id, "custom_product_input", $_POST["custom_product_input"]);
}

I've been playing around with this and still have no luck on making my one and only textarea to save so I can pull up the data and use it on my frontend. I am using bridge wordpress theme.

Anyone who can help me please? I really want to solve this.

  • Try debugging this step by step. Is save_detail() called? Is the first if false? Is the second if false? Is the third if false? Do the variables used in update_post_meta() contain what you believe they do? – kero Jul 6 '18 at 8:27
0

Maybe the problem could be that you dont show properly the value of the custom field, try this:

echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input">' . $text . '</textarea>';
  • Some escaping in there would be nice, I guess ;) – Krzysiek Dróżdż Jul 6 '18 at 9:57
  • I think escape html tags should be in the save_custom_field function. – SlavisWolf Jul 6 '18 at 11:30
  • You should sanitize in save function. You always have to escape when you print user input... What if I put </textarea> as that value? It will break HTML of that site. And of course I can put it in there... – Krzysiek Dróżdż Jul 6 '18 at 12:19
  • mmm but sanitize dont remove all html tags? – SlavisWolf Jul 6 '18 at 13:04
  • No. Why should it? – Krzysiek Dróżdż Jul 6 '18 at 13:34

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.