Skip to content

Instantly share code, notes, and snippets.

@widoz widoz/KsesImage.php
Last active Nov 11, 2017

Embed
What would you like to do?
Wp Kses functions
<?php
/**
* Kses Image
*
* This is a wrapper function for wp_kses that allow only specific html attributes for images.
*
* @uses wp_kses()
*
* @since 1.0.0
*
* @param string $img The image string to process.
*
* @return string The processed string containing only the allowed attributes
*/
function ksesImage($img)
{
/**
* Filter Kses Image
*
* @param array $list The list of the allowed attributes
*/
$attrs = apply_filters('a_fitler_name', array(
'img' => array(
'src' => true,
'srcset' => true,
'sizes' => true,
'class' => true,
'id' => true,
'width' => true,
'height' => true,
'alt' => true,
'longdesc' => true,
'usemap' => true,
'align' => true,
'border' => true,
'hspace' => true,
'vspace' => true,
),
));
return wp_kses($img, $attrs);
}
<?php
/**
* Sanitize content for allowed HTML tags for post content.
*
* @todo Remove if the issue will be fixed. See below.
*
* @see https://core.trac.wordpress.org/ticket/37085
*
* @since 1.0.0
*
* @param string $data Post content to filter
*
* @return string Filtered post content with allowed HTML tags and attributes.
*/
function ksesPost($data, array $extraAttrs = array())
{
global $allowedposttags;
$tagsInputIncluded = array_merge($allowedposttags, array(
'input' => array(
'accept' => true,
'autocomplete' => true,
'autofocus' => true,
'checked' => true,
'class' => true,
'disabled' => true,
'id' => true,
'height' => true,
'min' => true,
'max' => true,
'minlenght' => true,
'maxlength' => true,
'name' => true,
'pattern' => true,
'placeholder' => true,
'readony' => true,
'required' => true,
'size' => true,
'src' => true,
'step' => true,
'type' => true,
'value' => true,
'width' => true,
),
'select' => array(
'autofocus' => true,
'class' => true,
'id' => true,
'disabled' => true,
'form' => true,
'multiple' => true,
'name' => true,
'required' => true,
'size' => true,
),
'option' => array(
'disabled' => true,
'label' => true,
'selected' => true,
'value' => true,
),
'optgroup' => array(
'disabled' => true,
'label' => true,
),
'textarea' => array(
'placeholder' => true,
'cols' => true,
'rows' => true,
'disabled' => true,
'name' => true,
'id' => true,
'readonly' => true,
'required' => true,
'autofocus' => true,
'form' => true,
'wrap' => true,
),
'picture' => true,
'source' => array(
'sizes' => true,
'src' => true,
'srcset' => true,
'type' => true,
'media' => true,
),
));
if ($extraAttrs) {
// Extract the key for comparison.
$extraAttrsKeys = array_keys($extraAttrs);
foreach ($tagsInputIncluded as $tag => $attrs) {
// It is a tag where we want to insert additional attributes?
if (in_array($tag, $extraAttrsKeys, true)) {
// If so, include the extra attributes list within the main tags input list.
$tagsInputIncluded[$tag] = array_merge($tagsInputIncluded[$tag], $extraAttrs[$tag]);
}
}
}
// Form attributes.
$tagsInputIncluded['form'] = array_merge($tagsInputIncluded['form'], array('novalidate' => true));
// Fieldset attributes.
// WordPress have an empty array.
$tagsInputIncluded['fieldset'] = array_merge($tagsInputIncluded['fieldset'], array(
'id' => true,
'class' => true,
'form' => true,
'name' => true,
));
return wp_kses($data, $tagsInputIncluded);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.