After writing this ticket I think I have two separate issues:
1. I can't get the same block in the block editor to render an embedded media field with either an image or a video - ie. I can get it to work only with one or the other
2. my shortcode throws an error in the back-end when I try to update the block editor - but the front-end looks fine no error.
If it is true that I need this shortcode because of a 'bug' with embedded field display please help!
Background:
The reason I developed this shortcode in the first place to test on my Types embedded media field is that although a youtube VIDEO presents just fine with (in a shortcode block)- this code only renders a linked url to an IMAGE:
[types field='common_featured_media'][/types]
I cannot get my IMAGES in the embedded media field to display unless using an html block: (I'm using the block editor)- but of course this code will not render a VIDEO:
<img src="[types field='common_featured_media' output='raw'][/types]">
So I therefore went down the road of designing a shortcode to test for a '.jpg' in the embedded media field to allow me to present different code depending on the field content. (since my embedded media custom field contains url's to both video and images - in around 1000 posts!)
However even though my shortcode appears to work, this outputs the URL only, not the image:
[wpv-conditional if="( '[testforimage]' eq '1' )" debug="true"]<img src="[wpv-post-field name="wpcf-common_featured_media"]" > [/wpv-conditional]
This also just outputs the URL:
[wpv-conditional if="( '[testforimage]' eq '1' )" debug="true"]<img src="[types field='common_featured_media' output='raw'][/types]">[/wpv-conditional]
Without the conditional the code does accurately render the image...very inconsistent.
I receive this error if the shortcode [testforimage] is included in any block (even though the front-end appears fine with no errors):
"Updating failed. Error message: The response is not a valid JSON response."
when I use the following custom shortcode I wrote:
/**
* Tests if url is an image
*
* @var string $text The value of my custom field to be tested (in this case =common_featured_media).
* @return True=url is image, or False=url is not image (does not contain jpg, jpeg, png, gif)
*
* @author M.Elliott
*/
function test_url_for_image( ) {
$text = types_render_field( "common_featured_media", array("output" => "raw") );
if ( strpos( $text , 'jpg') == false ) {
$answer = '0';
return $answer;
}
$answer = '1';
return $answer;
}
add_shortcode('testforimage', 'test_url_for_image');
The shortcode works when I echo messages to at different logic points and it works when I use it in a wpv-conditional.
However, I cannot get my embedded image field (the one I am testing here, ie. common_featured_media) to render. Only the linked url is output?
I'm puzzled as to why it throws this error in the block editor but appears to work on the front end except the embedded image is not being displayed.