newbie here.
I am struggling around for hours. I am developing my first WP Plugin where I can enter products (product title and description). It seems that wp saves my description in the db, because if I open the product in the backend I can see my saved description. But if I'm trying to echo the description I get an empty value.
Below are some code-snippets:
metabox:
function product_custom_meta(){
add_meta_box('product_meta', __('Product Title', 'product-textdomain' ), 'product_meta_callback', 'url' );
}
add_action('add_meta_boxes', 'product_custom_meta');
function product_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'product_nonce' );
$product_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-desc" class="product-row-title"><?php _e( 'Description', 'product-textdomain' )?></label>
<input type="text" name="meta_desc" id="meta_desc" value="<?php if ( isset ( $product_stored_meta['meta-desc'] ) ) echo $product_stored_meta['meta-desc'][0]; ?>" />
</p>
<?php
}
function product_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'product_nonce' ] ) && wp_verify_nonce( $_POST[ 'product_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta_desc' ] ) ) {
update_post_meta( $post_id, 'meta_desc', sanitize_text_field( $_POST[ 'meta_desc' ] ) );
}
}
add_action( 'save_post', 'product_meta_save');
And thats how I tried to get my value:
$desc = get_post_meta( $post->ID, 'meta_desc', true );
echo $desc;
But it didn't worked, I hope someone can help me.
Thanks in advance