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

Sanitizing `wp_editor();` Values for Database, Edit, and Display

wp_kses to the rescue!

My Editor Contains everything a Post Might

Pass the result through wp_kses_post on the way in and out, and all should be good.

Remember, this will strip out anything added by the_content filter, so to preserve oembeds and shortcodes, use this:

echo apply_filters( 'the_content', wp_kses_post( $content ) );

You might also want esc_textarea for outputting the form if you’re using <textarea> tags directly

My Editor contains text, but no markup

On output, esc_html is your friend. Use this for situations when you have a text area that will never contain markup. On input, try wp_strip_all_tags on input to sanitise. The other WP APIs will make sure no SQL injections occur

My Editor contains markup, but a limited subset

wp_kses to the rescue, pass it through wp_kses along with a second parameter, an array defining what tags and attributes are allowed. E.g.:

$allowed = [
    'a' => [
        'href' => [],
        'title' => []
    ],
    'br' => [],
    'em' => [],
    'strong' => [],
];

echo wp_kses( $content, $allowed );

Be wary though of overextending. If you add script and iframe tags, is there really any point in using wp_kses?

wp_editor

wp_editor outputs internally, it can’t be escaped, therefore it is responsible for escaping itself. Do not try to be unhelpful by passing it pre-escaped content, this can lead ot mangled output and double escaping ( a great way for malformed content to bypass escaping ).

The function that outputs has the responsibility for escaping. This enables late escaping. Escaping earlier or multiple times is dangerous and introduces new complex problems, as you no longer know with certainty what is or isn’t escaped.

Related Posts:

  1. Saving Post Data in Another Database
  2. Update all posts automatically when using post_meta
  3. Better post meta efficiency?
  4. Change post format using custom field
  5. Exporting Data from WordPress into a flat table
  6. What Is meta_id In wp_postmeta?
  7. Add custom field automatically (add_post_meta) with value based on number of words of article
  8. How can I sort posts by the date and a custom meta field?
  9. Cannot retrieve a custom RSS field from posts
  10. Customize rel=canonical tag for single blog post
  11. How to I retrieve the ID from the Posts page?
  12. Add field to user meta table in database when link is clicked
  13. if in category but only with post meta
  14. Display post number by category
  15. Add multiple meta keys to a post at once
  16. saving/reading custom field value does not work – no value gets POSTed
  17. Post meta not working
  18. Is there a way to save different data when USER interacts with the same POST?
  19. How do you update post date (year only) in a separate custom field?
  20. WP_Query sort by meta_value_num or date
  21. Creating Ordered Query using Meta_key
  22. How enter custom post meta or custom field on box post list archive page?
  23. What effect can a large wp_post table have on overall site performance?
  24. Importing Data from a Non-WordPress database, into WP
  25. Copy post to separate database with “add_action(….)”
  26. When open add new post or page WordPress add post with ID=0 continuously [closed]
  27. Saving custom fields to a custom taxonomy
  28. Hooking into the post editing screen for an existing page only
  29. SQL trigger failes with post_content
  30. alt of attached image in post / pages
  31. Auto populate custom fields by post date
  32. Best Way to detect unique posts in wp rest api
  33. Get the correct meta_value with get_post_meta
  34. Surrogate ID for posts, is there an alternative field in the posts table?
  35. ACF Relationship – Get Parent’s Post Object
  36. Extracting a TLD from the content and assign to custom field
  37. post_name is not stored until post is published… where should I store my slug until it goes live then?
  38. Filter question list on substring of metavalue
  39. Add a meta to a post submited from a frontend form
  40. get backup file from wordpress database in x days
  41. How can I set and update the the_date according to a custom field of the post
  42. How to retrieve a post by inputing the url in a custom field and displaying it on an options page
  43. Expired Post with More Recent Time Stamp?
  44. Custom fields in normal posts
  45. Converting Posts to Pages
  46. Show posts between two Custom Fields date
  47. Add custom class to existing menu items from custom meta
  48. WP Query between posts custom fields [duplicate]
  49. get_the_ID() fails the first time, returns a value the second time it’s called
  50. I moved my site to another server, wp admin works so does the front page, but posts don’t work
  51. How to get only current images of a post
  52. Ordering posts alphabetically by meta keys and title
  53. How to change the publishing date of each posts?
  54. Run function after post is updated
  55. Can’t seem to replicate permalink structure for localhost development site
  56. Extra field in wp_post instead of postmeta
  57. Voting System, database connections?
  58. Where should get_post_meta() go to get $post->ID get_post_meta() is empty and $post->ID & get_the_id() are working
  59. When I click edit on a post, all the content disappear. Does anyone know how to fix this?
  60. Using Post ID and Page ID in same function
  61. Apply comment to different post (not the current post) [duplicate]
  62. Adding custom fields to the Quick Edit screen – puzzled about the column concept
  63. Order posts by separate menu order for different sub-categories
  64. Given two custom post types: Automatically add meta fields from one custom post type to another
  65. How to only allow post to be deleted if custom field doesn’t exist
  66. How to custom sort-out editing-panel fields?
  67. Delete old post with new post
  68. How can I create a menu items from meta box based on users input
  69. get_post_meta property of non object
  70. Home page: using custom fields for posts and pages
  71. How would I attach media/images to a post based on a ID stored in a each post’s custom field
  72. How do I insert a custom field in a user submitted post?
  73. Show related (archive) month and year to post
  74. Attaching global meta to custom fields
  75. Load posts from external source if not found in database
  76. First Custom Post Custom Fields Empty After New Custom Post
  77. How to Do Custom Fields to Output a Definition List
  78. Display a custom field rating system in the front end
  79. Is there a maximum amount of pages for a multi-page post?
  80. WP_query sort by custom meta_key “price”
  81. How to make a local “scroll to ID” on post?
  82. If meta_key exists then don’t select this post?
  83. How to make category for word post_content
  84. How to detect if an ACF custom-field is really in use?
  85. NewsPaper WP Theme – Additional Related Posts Filter by Time (divTag composer)
  86. Read more opens attachment
  87. Site ‘Categories’: save an admin global setting with post metadata [closed]
  88. How can I merge a several related posts into one post?
  89. Restore posts, but only posts that exist in my database backup
  90. How to make internal links creating plugin to respect ACF?
  91. Change permalinks in posts via SQL
  92. Translation related post text to Arabic
  93. Image inside the content is replaced with featured image from my older post
  94. Is it safe to totally unregister the default “post” Post Type
  95. WordPress post meta key and value lookup
  96. How to get a list of all posts and their categories?
  97. How can I create Woodmart theme “linked variations” through REST API?
  98. How to manually delete post from database without causing conflicts?
  99. Get featured image thumbnail and inserting into custom field on wordpress posts
  100. Can’t save/get Patterns’ `post_content` attribute
Categories posts Tags custom-field, database, post-meta, posts, sanitization
How to check if an action is already fired?
Get posts id in array by meta value and key

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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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