• Resolved strongerphilip

    (@strongerphilip)


    How do you access $attributes from a dynamic custom block PHP file?

    Accessing attributes in a static custom block via JS is really simple. However I’m unable to access attributes in a dynamic custom block via PHP.

    I’m using the @wordpress/create-block (@wordpress/scripts v25.1.0) to get started with dynamic blocks:

    npx @wordpress/create-block --variant dynamic todo-list

    In this latest version, you are provided with a render.php file. Not sure how you are meant to return the attributes within this file. Can’t find any docs on this:

    <?php
    
    global $block;
    
    $attributes = $block['attrs'];
    // $attributes = $block->get_attributes();
    
    print_r($attributes);
    
    ?>
    <div <?php echo get_block_wrapper_attributes(); ?>>
    	<?php esc_html_e('My Dynamic Block – hello from a dynamic block!', 'my-dynamic-block'); ?>
    </div>

    Failing that, I’ve gone back into the main php where the block is registered and have tried hooking into the render_callback since this should be able to return the attributes , content and block:

    function register_dynamic_block()
    {
        register_block_type(
            __DIR__ . '/build',
            array('render_callback' => 'my_callback')
        );
    }
    add_action('init', 'register_dynamic_block');
    
    function my_callback($attributes, $content, $block)
    {
        print_r($attributes); 
       // Can't even access attributes in here! :S
    }

    But this also just prints an empty array Array()

    Anyone have any ideas as to how you are meant to retrieve attributes from the render.php file?

Viewing 6 replies - 1 through 6 (of 6 total)
  • You can access attributes directly with the variable $attributes which contains an array with all the attributes of the block.

    In your example you have overwritten this variable with $block['attrs'] which it doesn’t exists and also you don’t need to put global $block.

    Thread Starter strongerphilip

    (@strongerphilip)

    Okay! I had two issues in my implementation that was causing the $attributes to be empty.

    First: Thank you Bernardatis for clarifying how to retrieve the $attributes! Brilliant ??

    To reiterate:

    You can access the attributes you’ve created from block.json within the render.php directly with $attributes:

    // render.php
    
    <?php
    
    // Extract the 'title' attribute. Fallback to 'unknown'. 
    $title = $attributes['title'] ? $attributes['title'] : 'unknown';
    
    ?>
    
    <div>
        <!-- Echo the title value -->
        <h2><?= $title ?></h2>
    </div>

    Second: the underlying issue was that my block.json still had the source property when defining the attributes.

    The source property should NOT be defined when using a dynamic block as it’s only meant for use in a static block.

    Doing so will prevent your attributes from saving in your edit.js file, thus making your $attributes empty. So remove the source property altogether.

    (I was migrating a static block to a dynamic block and didn’t realize this issue).

    Attribute sources are used to define how the attribute values are extracted from saved post content. They provide a mechanism to map from the saved markup to a JavaScript representation of a block.

    https://schemas.wp.org/trunk/block.json

    So don’t do this:

    // block.json
    
    ...
    "attributes": {
      "title": {
        "type": "string",
        "source": "text" // <-- THIS IS INCORRECT!
      }
    },
    ...
    

    Just remove it all together:

    // block.json
    
    ...
    "attributes": {
      "title": {
        "type": "string"
      }
    },
    ...

    Now your attribute will save, and you should be able to render it in render.php.

    If I want to access these attributes from my viewScript file, is there a method I can use or do I essentially have to add them to my render file as data attributes or something like that?

    • This reply was modified 1 year, 9 months ago by Mike Badgley.

    Hi @badg0003, do you find a native solution for accessing attributes from your viewScript file?

    Unfortunately no. I ended up just adding them as data-* attributes, which works ok but kinda clunky feeling.

    This should be added to the docs. So hard to find!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Attributes in a Dynamic Custom Block’ is closed to new replies.