Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Sanitaizing Select Optin For Custom Post Type Metabox in WP

You should scape data when displaying it, not when saving. To sanitize, I think the best choice in this case is sanitize_text_field()

if( isset( $_POST['my_meta_box_select'] ) ) {
    update_post_meta( $post_id, 'my_meta_box_select', sanitize_text_field( $_POST['my_meta_box_select']   ) );
}

Then, only if you are going use the data as attribute you should use esc_attr():

$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : '';
<select name="my_meta_box_select" id="my_meta_box_select">
   <option value="asia" <?php $selected == 'asia') ? 'selected'; ?>>Asia</option>
   <option value="africa" <?php $selected == 'africa') ? 'selected'; ?>>Africa</option>
   <option value="america" <?php $selected == 'america') ? 'selected'; ?>>America</option>
</select>

If you want also validate the data, you should compare with a list of valid values (or other validation comparison), as Rarst suggested in his answer (in this case you could skip the sanitization because if the value has a not valid value you don’t trigger the update_post_meta() function):

    if( isset( $_POST['my_meta_box_select'] ) ) {
        $value = $_POST['my_meta_box_select'];
        if ( in_array( $value, array('asia', 'africa', 'america'), true ) ) {
            update_post_meta( $post_id, 'my_meta_box_select', $value );
        }
    }

Related Posts:

  1. How do I remove all the metaboxes for a custom post type?
  2. Get all posts from custom post type and insert in select input as metabox
  3. Detect meta value changes when post is updated (post_updated)
  4. Having trouble with custom post type / meta box
  5. How can I include meta box content when searching?
  6. update a post meta from a single table cell TablePress
  7. update custom post type meta from a shortcode
  8. Custom Meta Boxes – Nonce Issue – Move to trash issue
  9. Displaying Meta Box Image
  10. How to keep a check box in custom meta box for custom post type checked by default for add new post?
  11. How to add multiple featured image in meta box in post editor?
  12. Show metabox in custom-post-template depending on taxonomy term?
  13. Formatting custom meta box date from YYYY/MM/DD to a more readable alternative
  14. Displaying custom posts only if custom meta box’s date is not expired
  15. Displaying Metabox value (custom post type taxonomy)
  16. How can I display my custom metaboxes on a custom post template?
  17. Meta Box Only in CPT Edit Page, Not in Add New
  18. get_post_meta is returning image id
  19. Populate Custom Fields in a Custom Post Type?
  20. Custom meta box data array: foreach not working correctly?
  21. Undefined Variable – Custom Post Type Meta
  22. Populate dropdown from one custom post type inside another custom post type
  23. How to check if user meta field is empty in conditional else statement
  24. A better way to add a meta box to custom post types
  25. Metabox not show in categories custom post type cmb2
  26. Show Custom Post Type meta boxes only on Page Edit
  27. Can’t publish custom post type – “You are not allowed to edit this post.”
  28. Meta Data for Custom Post Type not saving
  29. create custom meta box with default value
  30. Displaying a div from an assigned meta_value when on a page
  31. How to Echo Metadata Value in Currency Format
  32. Issue on Working with Metabox – Checkbox to enable Sale Items
  33. Why do I have to press the “Update” button twice to save my meta box values?
  34. Values from meta-box not on $_POST
  35. Custom post type suddenly stoped working after WordPress 5 update
  36. How can I output WPAlchemy repeating fields meta values in my page template?
  37. Meta box data is saved but NOT displayed in the meta box text field. Why?
  38. Save Metabox Custom Field Value
  39. Meta box data not saving
  40. WP_Meta_Query object with conditionals
  41. Related posts meta_query CPT
  42. Meta box with front-end styling
  43. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  44. Cannot Save MetaBox Data in Custom Post Type
  45. Add a class to post_class if more than one post shares same meta_value_num
  46. Function to return values from metabox
  47. Add a meta field to the list of results for a custom post type
  48. Empty meta-box returns publishdate if no value is set?
  49. Custom Meta Box with variable number of fields
  50. Custom Post Type meta data getting deleted on bulk editing taxonomies
  51. Get meta values from parent post and save in child post
  52. Metaboxes in Loop
  53. Custom meta box data not saving
  54. Collect custom post in a calendar
  55. Delete custom post type metadata without deleting the post in admin area
  56. Problem with ‘save_post’ hook not running
  57. Edit post meta direct from post.php?
  58. Howto: use existing post_meta as options for a different metabox (checkboxes or list)
  59. Display a post count from a custom metabox selection
  60. Post AND page parameter for WP function
  61. Use a Variable in update_post_meta as the $meta_key
  62. Parent Page and Breadcrumb URL’s for Custom Post Types Not Working
  63. Create a Dropdown from Custom Post Type
  64. Translating custom post type metaboxes [closed]
  65. Adding a metabox on a CPT’s submenu page
  66. Custom filter function not working with Custom post type
  67. Fixed values for same post translations
  68. How to Add multiple instance of meta box to custom post type
  69. Using POST method in meta box, no results
  70. meta box on custom post type
  71. Checking if $_FILE isset for an array of file upload metaboxes
  72. Importing Data from a Non-WordPress database, into WP
  73. Ordering values entered in Custom Taxonomies
  74. Getting thumbnails of a custom posts featured images in a page-templates metabox
  75. Listing custom terms in custom post meta
  76. Permalinks: custom structure for taxonomy – tags?
  77. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  78. Custom Post Type & Meta Box – Displaying meta box information on front end?
  79. Remove Post Custom Meta Box
  80. How can I move (or create another) publish button?
  81. Wrap meta boxes & data handling for specific post types in classes?
  82. What functions does WordPress use for filtering / sanitizing comments?
  83. Can custom fields be added without a meta box?
  84. Meta Box on Custom Post Type not saving
  85. Custom fields for custom post type
  86. Show future events based on custom field AND order by date
  87. Custom “radio button meta box” not saving correctly
  88. Global custom meta box
  89. Sizing textarea field in custom metabox
  90. Moving meta boxes in admin
  91. Remove “show sharing buttons” metabox Jetpack from custom post type
  92. Using preg_replace to filter custom textarea
  93. Add additional field to custom post_type
  94. Meta-Boxes for CustomPostType cause PHP Errors and Notices in “Add New” view
  95. Custom Post Meta from custom metaboxes is randomly being deleted
  96. Check if post with same meta value exists
  97. Filter date from post meta when date is in string format
  98. data-type=”” … needed post tags stripped of characters
  99. Get only used meta_values
  100. If Post Meta Equals Current Title Conditional Advanced Custom Fields
Categories custom-post-types Tags custom-post-types, metabox, post-meta, sanitization
Getting parent category hierarchy as objects from category template
How do I remove ‘Home’ from homepage title?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress