{"id":135621,"date":"2021-04-21T15:39:48","date_gmt":"2021-04-21T15:39:48","guid":{"rendered":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-context\/"},"modified":"2026-06-23T03:16:07","modified_gmt":"2026-06-23T03:16:07","slug":"block-context","status":"publish","type":"blocks-handbook","link":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-context\/","title":{"rendered":"Context"},"content":{"rendered":"<p>Block context is a feature which enables ancestor blocks to provide values which can be consumed by descendent blocks within its own hierarchy. Those descendent blocks can inherit these values without resorting to hard-coded values and without an explicit awareness of the block which provides those values.<\/p>\n<p>This is especially useful in full-site editing where, for example, the contents of a block may depend on the context of the post in which it is displayed. A blogroll template may show excerpts of many different posts. Using block context, there can still be one single &#8220;Post Excerpt&#8221; block which displays the contents of the post based on an inherited post ID.<\/p>\n<p>If you are familiar with <a href=\"https:\/\/react.dev\/learn\/passing-data-deeply-with-context\">React Context<\/a>, block context adopts many of the same ideas. In fact, the client-side block editor implementation of block context is a very simple application of React Context. Block context is also supported in server-side <code>render_callback<\/code> implementations, demonstrated in the examples below.<\/p>\n<h2>Defining block context<\/h2>\n<p>Block context is defined in the registered settings of a block. A block can provide a context value, or consume a value it seeks to inherit.<\/p>\n<h3>Providing block context<\/h3>\n<p>A block can provide a context value by assigning a <code>providesContext<\/code> property in its registered settings. This is an object which maps a context name to one of the block&#8217;s own attributes. The value corresponding to that attribute value is made available to descendent blocks and can be referenced by the same context name. Currently, block context only supports values derived from the block&#8217;s own attributes. This could be enhanced in the future to support additional sources of context values.<\/p>\n<pre><code class=\"language-js\">    attributes: {\n        recordId: {\n            type: 'number',\n        },\n    },\n\n    providesContext: {\n        'my-plugin\/recordId': 'recordId',\n    },\n<\/code><\/pre>\n<p>For a complete example, refer to the section below.<\/p>\n<h4>Include a namespace<\/h4>\n<p>As seen in the above example, it is recommended that you include a namespace as part of the name of the context key so as to avoid potential conflicts with other plugins or default context values provided by WordPress. The context namespace should be specific to your plugin, and in most cases can be the same as used in the name of the block itself.<\/p>\n<h3>Consuming block context<\/h3>\n<p>A block can inherit a context value from an ancestor provider by assigning a <code>usesContext<\/code> property in its registered settings. This should be assigned as an array of the context names the block seeks to inherit.<\/p>\n<pre><code class=\"language-js\">registerBlockType('my-plugin\/record-title', {\n    title: 'Record Title',\n    category: 'widgets',\n\n    usesContext: ['my-plugin\/recordId'],\n\n<\/code><\/pre>\n<h2>Using block context<\/h2>\n<p>Once a block has defined the context it seeks to inherit, this can be accessed in the implementation of <code>edit<\/code> (JavaScript) and <code>render_callback<\/code> (PHP). It is provided as an object (JavaScript) or associative array (PHP) of the context values which have been defined for the block. Note that a context value will only be made available if the block explicitly defines a desire to inherit that value.<\/p>\n<p>Note: Block Context is not available to <code>save<\/code>.<\/p>\n<h3>JavaScript<\/h3>\n<pre><code class=\"language-js\">registerBlockType('my-plugin\/record-title', {\n\n    edit({ context }) {\n        return 'The record ID: ' + context['my-plugin\/recordId'];\n    },\n\n<\/code><\/pre>\n<h3>PHP<\/h3>\n<p>A block&#8217;s context values are available from the <code>context<\/code> property of the <code>$block<\/code> argument passed as the third argument to the <code>render_callback<\/code> function.<\/p>\n<pre><code class=\"language-php\">register_block_type( 'my-plugin\/record-title', array(\n    'render_callback' =&gt; function( $attributes, $content, $block ) {\n        return 'The current record ID is: ' . $block-&gt;context['my-plugin\/recordId'];\n    },\n) );\n<\/code><\/pre>\n<h2>Example<\/h2>\n<ol>\n<li>Create a <code>record<\/code> block.<\/li>\n<\/ol>\n<pre><code>npm init @wordpress\/block --namespace my-plugin record\ncd record\n<\/code><\/pre>\n<ol>\n<li>Edit <code>src\/index.js<\/code>. Insert the <code>recordId<\/code> attribute and <code>providesContext<\/code> property in the <code>registerBlockType<\/code> function and add the registration of the <code>record-title<\/code> block at the bottom:<\/li>\n<\/ol>\n<pre><code class=\"language-js\">registerBlockType( 'my-plugin\/record', {\n    \/\/ ... cut ...\n\n    attributes: {\n        recordId: {\n            type: 'number',\n        },\n    },\n\n    providesContext: {\n        'my-plugin\/recordId': 'recordId',\n    },\n\n    \/**\n     * @see .\/edit.js\n     *\/\n    edit: Edit,\n\n    \/**\n     * @see .\/save.js\n     *\/\n    save,\n} );\n\nregisterBlockType( 'my-plugin\/record-title', {\n    title: 'Record Title',\n    category: 'widgets',\n\n    usesContext: [ 'my-plugin\/recordId' ],\n\n    edit( { context } ) {\n        return 'The record ID: ' + context[ 'my-plugin\/recordId' ];\n    },\n\n    save() {\n        return null;\n    },\n} );\n<\/code><\/pre>\n<ol>\n<li>Edit <code>src\/edit.js<\/code> for the <code>record<\/code> block. Replace the <code>Edit<\/code> function with the following code:<\/li>\n<\/ol>\n<pre><code class=\"language-js\">import { TextControl } from '@wordpress\/components';\nimport { InnerBlocks } from '@wordpress\/block-editor';\nimport { __ } from '@wordpress\/i18n';\n\nexport default function Edit( props ) {\n    const MY_TEMPLATE = [ [ 'my-plugin\/record-title', {} ] ];\n    const {\n        attributes: { recordId },\n        setAttributes,\n    } = props;\n    return (\n        &lt;div&gt;\n            &lt;TextControl\n                label={ __( 'Record ID' ) }\n                value={ recordId }\n                onChange={ ( val ) =&gt;\n                    setAttributes( { recordId: Number( val ) } )\n                }\n            \/&gt;\n            &lt;InnerBlocks template={ MY_TEMPLATE } templateLock=\"all\" \/&gt;\n        &lt;\/div&gt;\n    );\n}\n<\/code><\/pre>\n<ol>\n<li>Edit <code>src\/save.js<\/code> for the <code>record<\/code> block. Replace the <code>save<\/code> function with the following code:<\/li>\n<\/ol>\n<pre><code class=\"language-js\">export default function save( props ) {\n    return &lt;p&gt;The record ID: { props.attributes.recordId }&lt;\/p&gt;;\n}\n<\/code><\/pre>\n<ol>\n<li>Create a new post and add the <code>record<\/code> block. If you type a number in the text box, you&#8217;ll see the same number is shown in the <code>record-title<\/code> block below it.<\/li>\n<\/ol>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/user-images.githubusercontent.com\/8876600\/93000215-c8570380-f561-11ea-9bd0-0b2bd0ca1752.png?ssl=1\" alt=\"Block Context Example\" \/><\/p>\n","protected":false},"author":0,"featured_media":0,"parent":134406,"menu_order":64,"template":"","meta":{"footnotes":""},"class_list":["post-135621","blocks-handbook","type-blocks-handbook","status-publish","hentry","type-handbook"],"revision_note":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/135621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook"}],"about":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/types\/blocks-handbook"}],"version-history":[{"count":12,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/135621\/revisions"}],"predecessor-version":[{"id":182241,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/135621\/revisions\/182241"}],"up":[{"embeddable":true,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/blocks-handbook\/134406"}],"wp:attachment":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/media?parent=135621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}