• 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)
  • you can access all the posted variables by jQuery using:

    print_r($_POST);
    print_r($_GET);

    To find all the posted variables and values in your PHP file.

    @echodrome

    Thread Starter echodrome

    (@echodrome)

    @aamirkhatri thank you but that does not seem to provide me with the information I need on save. When I press the publish button the only thing I get adding both your lines is:

    Array
    (
        [rest_route] => /wp/v2/posts/372
        [_locale] => user
    )

    the other thing is that this was not posted using jQuery but it seems to be the React library of Guttenberg. Other blocks seem to work by just defining ‘attributes’ in the callback.

    @echodrome try this and paste the output

    
    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' => 'TEsting',
                        
                    )
                )
            ));
        }
    • This reply was modified 4 years, 6 months ago by Yui.
    • This reply was modified 4 years, 6 months ago by Yui. Reason: please use CODE button for proper formatting
    Thread Starter echodrome

    (@echodrome)

    This is the output:

    array(1) { ["max_links"]=> int(7) }

    Which makes me think that PHP has no way of knowing about the values entered on the JS side unless you have a custom table/API that stores this information. There is no way to recreate the JS attributes from the HTML as can be done in the JS side.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Accessing attributes in a dynamic block’ is closed to new replies.