5

I want to keep the style attribute. $str is just an example, here's my code:

$allowed_html = array(
     'div' => array(
         'title' => array(),
         'class' => array(),
         'style' => array()
    )
);

$str = '<div title='Click to continue' style='display:table'>This is a button</div>';
wp_kses($str, $allowed_html );

$str will actually receive a bunch of html tags and attributes from a post. Then from there i want to strip out all tags and attributes leaving out only divs tags and style and title attributes

Thank you, MMK.

  • Could you add a bit more on what exactly the problem is? What does $allow_html currently return that you want to change? – Howdy_McGee Dec 29 '14 at 22:04
  • wp_kses strips out any tags and attributes leaving out only those specified in the parameter $allowed_html. The $str string contains a style attribute. I want to keep it. But instead wp_kses removes it. I want to know how i can keep the style attribute there. – MMK Dec 29 '14 at 22:07
  • $str will actually receive a bunch of html tags and attributes from a post. Then from there i want to strip out all tags and attributes leaving out only divs tags and style and title attributes – MMK Dec 29 '14 at 22:10
  • It may be easier for future readers to view if you edited your original question and added this information, so it's all together. That way it will be easier to answer. There's an edit button just underneath the tags. – Howdy_McGee Dec 29 '14 at 22:17
12

This is an older question, but here's the answer for future generations:

WordPress will check the styles against a whitelist and it will still strip the style attribute if none of the styles are safe. The default whitelist is:

  • text-align
  • margin
  • color
  • float
  • border
  • background
  • background-color
  • border-bottom
  • border-bottom-color
  • border-bottom-style
  • border-bottom-width
  • border-collapse
  • border-color
  • border-left
  • border-left-color
  • border-left-style
  • border-left-width
  • border-right
  • border-right-color
  • border-right-style
  • border-right-width
  • border-spacing
  • border-style
  • border-top
  • border-top-color
  • border-top-style
  • border-top-width
  • border-width
  • caption-side
  • clear
  • cursor
  • direction
  • font
  • font-family
  • font-size
  • font-style
  • font-variant
  • font-weight
  • height
  • letter-spacing
  • line-height
  • margin-bottom
  • margin-left
  • margin-right
  • margin-top
  • overflow
  • padding
  • padding-bottom
  • padding-left
  • padding-right
  • padding-top
  • text-decoration
  • text-indent
  • vertical-align
  • width

This list is, as with most things in WordPress, filtered! You can add display to it as follows to let your code work as expected:

add_filter( 'safe_style_css', function( $styles ) {
    $styles[] = 'display';
    return $styles;
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.