Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function strip_tags_on_post_save($post_id)
- {
- // If this is a revision, get real post ID
- if ($parent_id = wp_is_post_revision($post_id)) {
- $post_id = $parent_id;
- }
- // Get post content
- $post_object = get_post($post_id);
- // Check if this post has content
- if (!empty($post_object)) {
- // unhook this function so it doesn't loop infinitely
- remove_action('save_post', __NAMESPACE__ . '\\strip_tags_on_post_save');
- // Get just the post content
- $content = $post_object->post_content;
- // Keep just these tags
- $allowed_html = array(
- 'a' => array(
- 'href' => array(),
- 'title' => array()
- ),
- 'br' => array(),
- 'em' => array(),
- 'i' => array(),
- 'strong' => array(),
- 'b' => array(),
- 'img' => array(),
- 'ul' => array(),
- 'ol' => array(),
- 'li' => array(),
- 'blockquote' => array(),
- 'hr' => array(),
- );
- // Do it.
- $content_stripped = wp_kses($content, $allowed_html);
- // Update the post, which calls save_post again
- wp_update_post(array( 'ID' => $post_id, 'post_content' => $content_stripped ));
- }
- }
- add_action('save_post', __NAMESPACE__ . '\\strip_tags_on_post_save');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement