Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Created February 19, 2013 05:52
Show Gist options
  • Save michaeluno/4983428 to your computer and use it in GitHub Desktop.
Save michaeluno/4983428 to your computer and use it in GitHub Desktop.
Applies allowing HTML tags with the KSES filter.
<?php
/*
Plugin Name: Test - Kses Allow Tags
Plugin URI:
Description:
Version: 0.0.0.1
Author: Michael Uno
Author URI: http://michaeluno.jp
*/
if ( !class_exists( 'Admin_Page_Framework' ) )
include_once( dirname( __FILE__ ) . '/classes/admin-page-framework.php' );
class KsesAllowTags extends Admin_Page_Framework {
function SetUp() {
$this->SetRootMenu( 'Settings' );
$this->AddSubMenu( 'Test Kses Allow Tags',
'test_kses_allow_tags' );
}
function do_test_kses_allow_tags() { // do_ + pageslug
$string = <<<STRING
<noscript>foo</noscript><style><strong>bar</strong></style>
STRING;
// allows noscript and style tags
$string = $this->custom_wp_filter_post_kses( $string, array( 'noscript' => array(), 'style' => array() ) );
echo '<pre>' . htmlspecialchars( $string ) . '</pre>';
}
function custom_wp_filter_post_kses( $string, $arrAllowedTags, $allowed_protocols = array() ) {
global $allowedposttags;
$allowed_html = array_replace_recursive( $allowedposttags, $arrAllowedTags );
if ( empty( $allowed_protocols ) )
$allowed_protocols = wp_allowed_protocols();
$string = addslashes( $string ); // the original function call was doing this - could be redundant but haven't fully tested it
$string = stripslashes( $string ); // wp_filter_post_kses()
$string = wp_kses_no_null( $string ); // wp_kses()
$string = wp_kses_js_entities( $string ); // wp_kses()
$string = wp_kses_normalize_entities( $string ); // wp_kses()
$string = wp_kses_hook( $string, $allowed_html, $allowed_protocols ); // WP changed the order of these funcs and added args to wp_kses_hook
$string = wp_kses_split( $string, $allowed_html, $allowed_protocols );
$string = addslashes( $string ); // wp_filter_post_kses()
$string = stripslashes( $string ); // the original function call was doing this - could be redundant but haven't fully tested it
return $string;
}
}
new KsesAllowTags();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment