Title: get_block_wrapper_attributes
Published: December 9, 2020
Last modified: April 28, 2025

---

# get_block_wrapper_attributes( string[] $extra_attributes = array() ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#wp--skip-link--target)

Generates a string of attributes by applying to the current block being rendered
all of the features that the block supports.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#parameters)󠁿

 `$extra_attributes`string[]optional

Array of extra attributes to render on the block wrapper.

Default:`array()`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#return)󠁿

 string String of HTML attributes.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#source)󠁿

    ```php
    function get_block_wrapper_attributes( $extra_attributes = array() ) {
    	$new_attributes = WP_Block_Supports::get_instance()->apply_block_supports();

    	if ( empty( $new_attributes ) && empty( $extra_attributes ) ) {
    		return '';
    	}

    	// This is hardcoded on purpose.
    	// We only support a fixed list of attributes.
    	$attributes_to_merge = array( 'style', 'class', 'id', 'aria-label' );
    	$attributes          = array();
    	foreach ( $attributes_to_merge as $attribute_name ) {
    		if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) {
    			continue;
    		}

    		if ( empty( $new_attributes[ $attribute_name ] ) ) {
    			$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ];
    			continue;
    		}

    		if ( empty( $extra_attributes[ $attribute_name ] ) ) {
    			$attributes[ $attribute_name ] = $new_attributes[ $attribute_name ];
    			continue;
    		}

    		$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ];
    	}

    	foreach ( $extra_attributes as $attribute_name => $value ) {
    		if ( ! in_array( $attribute_name, $attributes_to_merge, true ) ) {
    			$attributes[ $attribute_name ] = $value;
    		}
    	}

    	if ( empty( $attributes ) ) {
    		return '';
    	}

    	$normalized_attributes = array();
    	foreach ( $attributes as $key => $value ) {
    		$normalized_attributes[] = $key . '="' . esc_attr( $value ) . '"';
    	}

    	return implode( ' ', $normalized_attributes );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-block-supports.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-block-supports.php#L175)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-block-supports.php#L175-L220)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Block_Supports::get_instance()](https://developer.wordpress.org/reference/classes/wp_block_supports/get_instance/)`wp-includes/class-wp-block-supports.php` |

Utility method to retrieve the main instance of the class.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.6.0](https://developer.wordpress.org/reference/since/5.6.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#comment-content-6283)
 2.    [Lovro Hrust](https://profiles.wordpress.org/lovor/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/#comment-6283)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_block_wrapper_attributes%2F%23comment-6283)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_block_wrapper_attributes%2F%23comment-6283)
 4.  Regarding Fabian Kägy’s comment, in WordPress 6.1.1 other attributes work too,
     even custom attribute like `data-settings`.
 5.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_block_wrapper_attributes%2F%3Freplytocom%3D6283%23feedback-editor-6283)
 6.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/?output_format=md#comment-content-5874)
 7.    [Fabian Kaegy](https://profiles.wordpress.org/fabiankaegy/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/get_block_wrapper_attributes/#comment-5874)
 8.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_block_wrapper_attributes%2F%23comment-5874)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_block_wrapper_attributes%2F%23comment-5874)
 9.  At the time of WordPress 6.0 the `get_block_wrapper_attributes` function accepts
     two keys via the `$extra_attributes`. `class` and `style`.
 10.     ```php
         $wrapper_attributes = get_block_wrapper_attributes(
         	[
         		'class' => 'custom-class',
         		'style' => 'color: #333',
         	]
         );
         ```
     
 11. In order to output the wrapper attributes, you need to add them inside your outermost
     HTML element in your block markup.
 12.     ```php
         $wrapper_attributes = get_block_wrapper_attributes(
         	[
         		'class' => 'custom-class',
         		'style' => 'color: #333',
         	]
         );
     
         return sprintf( '<section %s>...</section>', $wrapper_attributes );
         ```
     
 13.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_block_wrapper_attributes%2F%3Freplytocom%3D5874%23feedback-editor-5874)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_block_wrapper_attributes%2F)
before being able to contribute a note or feedback.