12

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.

4
  • 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
    Commented Dec 29, 2014 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
    Commented Dec 29, 2014 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
    Commented Dec 29, 2014 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
    Commented Dec 29, 2014 at 22:17

1 Answer 1

32

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

WordPress will check the styles against a list of allowed properties and it will still strip the style attribute if none of the styles are safe. The default allow list (as of WordPress 6.2) is:

array(
    'background',
    'background-color',
    'background-image',
    'background-position',
    'background-size',
    'background-attachment',
    'background-blend-mode',

    'border',
    'border-radius',
    'border-width',
    'border-color',
    'border-style',
    'border-right',
    'border-right-color',
    'border-right-style',
    'border-right-width',
    'border-bottom',
    'border-bottom-color',
    'border-bottom-left-radius',
    'border-bottom-right-radius',
    'border-bottom-style',
    'border-bottom-width',
    'border-bottom-right-radius',
    'border-bottom-left-radius',
    'border-left',
    'border-left-color',
    'border-left-style',
    'border-left-width',
    'border-top',
    'border-top-color',
    'border-top-left-radius',
    'border-top-right-radius',
    'border-top-style',
    'border-top-width',
    'border-top-left-radius',
    'border-top-right-radius',

    'border-spacing',
    'border-collapse',
    'caption-side',

    'columns',
    'column-count',
    'column-fill',
    'column-gap',
    'column-rule',
    'column-span',
    'column-width',

    'color',
    'filter',
    'font',
    'font-family',
    'font-size',
    'font-style',
    'font-variant',
    'font-weight',
    'letter-spacing',
    'line-height',
    'text-align',
    'text-decoration',
    'text-indent',
    'text-transform',

    'height',
    'min-height',
    'max-height',

    'width',
    'min-width',
    'max-width',

    'margin',
    'margin-right',
    'margin-bottom',
    'margin-left',
    'margin-top',
    'margin-block-start',
    'margin-block-end',
    'margin-inline-start',
    'margin-inline-end',

    'padding',
    'padding-right',
    'padding-bottom',
    'padding-left',
    'padding-top',
    'padding-block-start',
    'padding-block-end',
    'padding-inline-start',
    'padding-inline-end',

    'flex',
    'flex-basis',
    'flex-direction',
    'flex-flow',
    'flex-grow',
    'flex-shrink',
    'flex-wrap',

    'gap',
    'column-gap',
    'row-gap',

    'grid-template-columns',
    'grid-auto-columns',
    'grid-column-start',
    'grid-column-end',
    'grid-column-gap',
    'grid-template-rows',
    'grid-auto-rows',
    'grid-row-start',
    'grid-row-end',
    'grid-row-gap',
    'grid-gap',

    'justify-content',
    'justify-items',
    'justify-self',
    'align-content',
    'align-items',
    'align-self',

    'clear',
    'cursor',
    'direction',
    'float',
    'list-style-type',
    'object-fit',
    'object-position',
    'overflow',
    'vertical-align',

    'position',
    'top',
    'right',
    'bottom',
    'left',
    'z-index',
    'aspect-ratio',

    // Custom CSS properties.
    '--*',
)

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;
} );
3
  • For more info, see wp_kses_attr_check() and safecss_filter_attr().
    – rinogo
    Commented Aug 15, 2018 at 19:34
  • 1
    @brooke thanks for the edit, you're the real mvp! Commented Dec 1, 2020 at 19:14
  • 1
    Dang, I wasted so much time on this. Thanks for your clear answer and solution. Still helping people 8 years later.
    – Aristocles
    Commented Feb 24, 2023 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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