echo selected value from dropdown
This is for a custom type post, I have a custom field (a drop-down list) when I select an option in the dropdown let's say DesignApplication and save the post.
Then when I try to update the post later, I find GameApplication selected by default which is the first option in the list.
It doesn't retrieve the selected saved option, I searched on Google and tried to fix myself but to no avail.
esc_attr( $j_applicationCategory ) doesn't work it returns GameApplication.
Here is the code I'm using:
add_action( 'add_meta_boxes', 'progs_add_meta_box' );
function progs_meta_box_callback( $post ) {
wp_nonce_field( 'progs_meta_box', 'progs_meta_box_nonce' );
$j_applicationCategory = get_post_meta( $post-ID, '_j_applicationCategory', true );
echo 'plabel for=j_applicationCategory' . __('Application Category', 'the220px') . '/label';
echo 'select id=j_applicationCategory name=j_applicationCategory value=' . esc_attr( $j_applicationCategory ) . ' style=width: 100% /';
echo 'option value=GameApplicationGameApplication/option';
echo 'option value=UtilitiesApplicationUtilitiesApplication/option';
echo 'option value=DesignApplicationDesignApplication/option';
echo 'option value=ReferenceApplicationReferenceApplication/option';
echo '/select';
echo '/p';
}
function progs_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['progs_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['progs_meta_box_nonce'], 'progs_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['post_type'] ) 'progs' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if ( ! isset( $_POST['j_applicationCategory'] ) ) {
return;
}
$j_applicationCategory = sanitize_text_field( $_POST['j_applicationCategory'] );
update_post_meta( $post_id, '_j_applicationCategory', $j_applicationCategory );
}
Topic dropdown select custom-field custom-post-types Wordpress
Category Web