Dynamic content in Custom HTML field

Support MB Blocks Dynamic content in Custom HTML fieldResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #31499
    DANIEL FITIPALDO R.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.

    #31511
    Long NguyenLong 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.DANIEL FITIPALDO R.
    Participant

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

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