I've been "talking" with Phi Phan, the developer of the "https://wordpress.org/plugins/better-youtube-embed-block/" plugin which allows you to display vertical YouTube videos.
Since there was no support for the URL to be the value of a CPT, he suggested the following, I transcribe what we were talking about.
Me:
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.
Phi
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.
Me
I saw it in Attributes! What I don’t see are the Custom fields I created with Toolset types
Phi
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' => 'hidden link',
'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
);
Contract
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.
_______
I still haven't managed to get it to display the value even though I changed the value in the filter:
wpcf_testimonial_url
to
wpcf-url-youtube
any ideas?