Custom Fields
Re-Envisioned
Support MB Blocks Dynamic content in Custom HTML field

  • Creator
    Topic
  • #31499
    Resolved DANIEL FITIPALDO R.
    Participant

    Hello,
    I have a question regarding the use of Custom HTML field in a block.
    I am using a custom HTML in a block in edit mode and I want its content to be dynamic based on other fields / attributes that I have edited in the same block.
    I was thinking of using the php callback function to build the dynamic content but I don't know if it is the best option because I don't know if it passes any argument to that function, such as the post_id in which the block is being used.
    Could you guide me on how to generate dynamic content in a Gutenberg block field of type Custom HTML ??
    Thank you so much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #31511
    Long Nguyen
    Moderator

    Hi Daniel,

    The field custom_html does not save the value to the database, it is not supported to display/render on the frontend so it will not work with the block. You can create a custom function such as

    function show_my_template( $post_id ) {
        echo 'Post ID is: ' . $post_id;
    }

    Then add the custom function to the render callback function and pass the accepted arguments to the custom function
    https://docs.metabox.io/extensions/mb-blocks/#render_callback

    function my_hero_callback( $attributes, $is_preview = false, $post_id = null ) {
        // Fields data.
        if ( empty( $attributes['data'] ) ) {
            return;
        }
            show_my_template( $post_id );
    ...
    }
    
    #31522
    DANIEL FITIPALDO R.
    Participant

    Ok, I understood. Thank you Long, you can close this ticket as resolved.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.