Accessing attributes in a dynamic block
-
I am trying to create a dynamic block and I am unable to determine the way to grab this information on the PHP side.
Currently my index.js has:
import edit from "./edit"; import { registerBlockType } from "@wordpress/blocks"; import { __ } from "@wordpress/i18n"; const attributes = { max_links: { type: "number", default: 7 }, links: { type: "array", default: [], source: "query", selector: "a", query: { text: { source: "html" }, link: { source: "attribute", attribute: "href" } } } }; registerBlockType("myblocks/quicklinks", { title: __("Quick Links", "myblocks"), description: __("Quick Links Block", "myblocks"), icon: "list-view", category: "my-category", keywords: [__("links", "myblocks")], attributes, edit });
and my plugin.php:
function my_register() { register_block_type('quicklinks', array( 'render_callback' => 'my_blocks_render_quicklinks_block', 'attributes' => array( 'max_links' => array( 'type' => 'number', 'default' => 7 ), 'links' => array( 'type' => 'array', 'default' => [], 'source' => 'query', 'selector' => 'a', 'query' => array( 'text' => array( 'source' => 'html' ), 'link' => array( 'source' => 'attribute', 'attribute' => 'href' ) ) ) ) )); } add_action('init', 'my_register'); function my_blocks_render_quicklinks_block( $attributes, $content ) { var_dump($attributes); var_dump($content); }
When I run the code the value of
$attributes['max_links']
is properly set to 7 but the value of$attributes['links']
is an empty array.My best guess is that my variable declaration in PHP is not matching the Javascript side but I cannot find any examples that define the source of the attribute as a query. Is there a way to get the query from the PHP side?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Accessing attributes in a dynamic block’ is closed to new replies.