shortcode_atts

Home » Functions » shortcode_atts

The shortcode_atts function is a built-in WordPress function that is used to merge the given attributes with the default attributes. It takes two arguments: the default attributes and the user defined attributes.

The function is mostly used in WordPress shortcode development to set default values for shortcode attributes and to merge them with user defined attributes.

Here’s an example usage code:

function custom_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'attribute_1' => 'default_value_1',
        'attribute_2' => 'default_value_2',
    ), $atts );

    return "attribute_1 = {$atts['attribute_1']}, attribute_2 = {$atts['attribute_2']}";
}
add_shortcode( 'custom_shortcode', 'custom_shortcode' );

In the above example, the shortcode_atts function is used to merge the default values with the user defined values. If a user does not define a particular attribute, it will fallback to its default value.

Overall, the shortcode_atts function is an essential function for anyone who wants to develop custom shortcodes in WordPress.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.