• Resolved Miguel

    (@mparraud)


    I’m using Toolset Types to create CPTs, and I want to display vertical YouTube videos with testimonials.

    The CPT has a field called “Testimonial URL”, where the user pastes the YouTube URL of the testimonial.

    I created a query for this CPT and wanted to use this plugin (which, by the way, is excellent!) to display these testimonials, but the problem is that I can’t set the URL as a dynamic field.

    I can’t figure out how to solve this.

    What I noticed is that with a Paragraph block, it allows me to insert an inline field. It would be great to be able to do the same with the YouTube block, inserting it and having the video URL come from an inline field.

    • This topic was modified 5 months ago by Miguel.
Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hi Miguel @mparraud,

    Thanks for your excellent suggestion. WordPress 6.9 now supports binding custom attributes for any block, so I’ll add this feature in the next version after the holidays.

    In the meantime, you can use the code below to bind a custom field to the url attribute of this block:

    add_filter(
    'block_bindings_supported_attributes_boldblocks/youtube-block',
    function ( $attributes ) {
    $attributes = [ 'url', 'caption' ];
    return $attributes;
    }
    );

    Best, and happy holidays,
    Phi.

    Thread Starter Miguel

    (@mparraud)

    I added the code you sent to functions.php, but nothing changed. I’m doing something wrong.

    Ah! Happy New Year!

    Plugin Author Phi Phan

    (@mr2p)

    @mparraud Could you provide a bit more detail about your site setup? This feature requires WordPress 6.9 or later. It works well on my local site running WordPress 6.9 with the Twenty Twenty-Four theme.

    Happy New Year to you too!

    Thread Starter Miguel

    (@mparraud)

    Sorry, I saw it in Attributes! What I don’t see are the Custom fields I created with Toolset types

    Plugin Author Phi Phan

    (@mr2p)

    This feature only adds support for binding a custom field from a dynamic binding source to the url attribute. To bind a custom field to the url attribute, the field must be supported as a dynamic binding source.

    I don’t have experience with Toolset, but if you don’t see your field listed as a binding source, it likely doesn’t support dynamic binding for its custom fields.

    The simplest way to create a custom binding source is shown below.

    add_action(
    'init',
    function () {
    register_meta(
    'post',
    'youtube_video_url',
    array(
    'show_in_rest' => true,
    'type' => 'string',
    'default' => 'https://youtu.be/hhH3bHrh_4U',
    'single' => true,
    )
    );
    }
    );

    add_filter(
    'get_post_metadata',
    function ( $value, $post_id, $meta_key ) {

    if ( $meta_key === 'youtube_video_url' ) {
    // Return the real value. Replace the field name with your real field name.
    return get_post_meta( $post_id, 'wpcf_testimonial_url', true );
    }

    return $value;
    },
    10,
    3
    );

    This approach registers a fake post meta field as a binding source, then hooks into the get_post_metadata filter to return the real value from your custom field.

    Replace wpcf_testimonial_url with your own field slug. It looks like Toolset stores custom fields with the wpcf_ prefix, but I’m not completely sure. If your fields don’t use that prefix, just use the field slug as-is.

    Once this code is in place, you can bind the youtube_video_url field to the url attribute of the YouTube block.

    You can also ask Toolset for further support on this binding stuff.

    Phi.

    Plugin Author Phi Phan

    (@mr2p)

    @mparraud I’ve just released a new version that adds block binding support for the URL and caption attributes. You can now bind your custom fields to those attributes.

    If you’d like to use your Toolset field, you can do so with the code I shared in my previous post.

    Please let me know if it works for you.

    Thread Starter Miguel

    (@mparraud)

    I showed our back-and-forth to the Toolset support team, and they replied as follows:

    Honestly, I do not have any experince with the plugin “better-youtube-embed-block” and the code you shared is from Dev and they know the best and what could be the possible required fixes or code enhancements should be added in order to work Toolset custom field and “better-youtube-embed-block” with dynamic sources.

    I can check and try once if you share problem URL and admin access details and see if I can able to offer you any sort of solution. However – It’s better if you can check with them and if they have any hook/filters to attach Toolset custom field to the youtube block they offer. Because for instance even if I fix it for now and leter with the code changes the plugin author will make in order to make this work as require and our fix will be broken.

    I would say its better choice if you could wait a little and check with plugin author and they should provice official way to use Toolset custom field(s) with their youtube block it will be safe solution to use.

    I hope this makes sence.

    Plugin Author Phi Phan

    (@mr2p)

    @mparraud Have you tried the code I shared in my previous post?

    add_action(
    'init',
    function () {
    register_meta(
    'post',
    'youtube_video_url',
    array(
    'show_in_rest' => true,
    'type' => 'string',
    'default' => 'https://youtu.be/hhH3bHrh_4U',
    'single' => true,
    )
    );
    }
    );

    add_filter(
    'get_post_metadata',
    function ( $value, $post_id, $meta_key ) {

    if ( $meta_key === 'youtube_video_url' ) {
    // Return the real value. Replace the field name with your real field name.
    return get_post_meta( $post_id, 'wpcf_testimonial_url', true );
    }

    return $value;
    },
    10,
    3
    );

    You can use it as is. Just replace wpcf_testimonial_url with your actual field slug, and it should work. This approach works with any meta field framework, as long as the correct field name is provided.

    I’ve already added the binding logic to the plugin. All you need to do on your side is provide the source binding, and everything else should work as expected.

    This block does not provide any hooks or filters for content binding. It looks like Toolset uses its own custom binding approach rather than the official WordPress core method. Because of that, you can use my code snippet with the correct field name/slug, and it should work.

    Let me know how it goes.

    Phi.

    Thread Starter Miguel

    (@mparraud)

    Phi, I installed your plugin last version and added the last code with the slug of my field, but it doesn’t work yet.

    I’m trying to figure out what’s the problem.

    Plugin Author Phi Phan

    (@mr2p)

    @mparraud Could you explain your issue in a bit more detail? Does the post meta binding source appear in the Attributes selection settings, or can you select the source but the video does not show up?

    Thread Starter Miguel

    (@mparraud)

    Hi Phi,

    After adding your code, the youtube_video_url attribute appears and I can select it.

    When I select it, the backend shows the “Map block to embed Google” video thumbnail. However, even though I updated the field name in functions.php to match the one I created (changing the slug “url-youtube” to “wpcf-url-youtube”), the videos are still not displaying.

    I am attaching screenshots of both the block settings and the functions.php file for your review.

    URL as a Toolset types inline field request

    thank you

    Plugin Author Phi Phan

    (@mr2p)

    Hi Miguel @mparraud,

    You’re getting closer. The next step is to debug and make sure this line returns the correct video URL:

    get_post_meta( $post_id, 'wpcf-url-youtube', true );

    The key idea is that you need to return the correct video URL inside the get_post_metadata filter:

    add_filter(
    'get_post_metadata',
    function ( $value, $post_id, $meta_key ) {

    if ( $meta_key === 'youtube_video_url' ) {
    // Return the real value. Replace the field name with your real field name.
    return get_post_meta( $post_id, 'wpcf-url-youtube', true );
    }

    return $value;
    },
    10,
    3
    );

    Please let me know how that goes.

Viewing 12 replies - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.