Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

wp_pre_kses_block_attributes

Function Description:

Removes non-allowable HTML from parsed block attribute values when filtering in the post context.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Basic usage of wp_pre_kses_block_attributes
add_filter( 'wp_pre_kses_block_attributes', 'my_custom_block_attributes', 10, 2 );

function my_custom_block_attributes( $allowed_attributes, $context ) {
    // Add 'data-custom' attribute to allowed attributes
    $allowed_attributes['data-custom'] = true;

    return $allowed_attributes;
}
// Example 2: Restricting certain attributes in wp_pre_kses_block_attributes
add_filter( 'wp_pre_kses_block_attributes', 'restrict_block_attributes', 10, 2 );

function restrict_block_attributes( $allowed_attributes, $context ) {
    // Remove 'style' attribute from allowed attributes
    unset( $allowed_attributes['style'] );

    return $allowed_attributes;
}
// Example 3: Customizing allowed attributes based on context in wp_pre_kses_block_attributes
add_filter( 'wp_pre_kses_block_attributes', 'customize_block_attributes', 10, 2 );

function customize_block_attributes( $allowed_attributes, $context ) {
    // Check if the context is 'shortcode'
    if ( 'shortcode' === $context ) {
        // Add 'data-shortcode' attribute for shortcode context
        $allowed_attributes['data-shortcode'] = true;
    }

    return $allowed_attributes;
}