• v4ds

    (@v4ds)


    Hello,

    I want to add a new attribute to the core/embed block for a preview image (privacy matters).

    That’s working fine, a little excerpt from my code:

    if (name == "core/embed") {
                settings.attributes = Object.assign(settings.attributes, {
                    previewImage: {
                        type: "string"
                    }
                });
                }
            }

    In the oembed filter I replace the embed with custom HTML. There I want to include the previewImage from the embed block. Can I access a block attribute from the oembed filter?

    function edit_oembed_html($html, $url, $attr, $post_ID)
    {	
    	// Get thumbnail from block attribute 'previewImage'
    	// $thumbnail = this->embed->previewImage; PSEUDO CODE
    
    	// Build HTML template
    	$html = '
    		<div class="v4core_embed_blocker">
    			<img src="'. $thumbnail .'" alt="'. $name .'" />
    		</div>';
    
    	// Return it
    	return $html;
    }
    add_filter( 'embed_oembed_html', 'edit_oembed_html', 10, 4);
    • This topic was modified 4 years ago by v4ds.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Block attributes (non-default) are within each block’s comment delimiter in the original HTML. For example, a paragraph block delimiter might look like <!-- wp:paragraph {"fontSize":"medium"} -->. You could use preg_match() to extract desired data from the HTML.

    Joy

    (@joyously)

    Is the third parameter the attributes?

    Thread Starter v4ds

    (@v4ds)

    Hello,

    thanks for your answers, I am happy about any hint…

    @bcworkz Ok, that sounds interesting. I think I need those in the frontend, but there I can’t see the data comments. I was hoping for a solution without parsing all blocks, but maybe there is no other way currently without modifying the whole core/embed block…

    @joyously Yes, the third parameter has some attributes, but there seems to be saved the width and height of the iframe only:

    var_dump($attr); gets me:

    array(2) {
      ["width"]=>
      int(500)
      ["height"]=>
      float(750)
    }

    If it is possible to somewhat “inject” the previewImage data into the $attr array it would be great.

    • This reply was modified 4 years ago by v4ds.
    Moderator bcworkz

    (@bcworkz)

    You have $post_ID passed. Could you get the information you need from the related WP_Post object or its meta data? Related attachment posts? If it’s not in post meta or related attachments, the block delimiter attributes in content or content HTML itself may be the only source of the information you seek.

    Thread Starter v4ds

    (@v4ds)

    Unfortunately I couldn’t find the attribute neither in the output of var_dump( get_post($post_ID) ); nor in var_dump( get_post_meta($post_ID) );.

    I also tried a custom meta field that is linked with the block attribute (https://wholesomecode.ltd/guides/post-meta-fields-store-attributes-wordpress-gutenberg/), but then it is the same for every embed block.

    For the moment I will handle it via ACF and a global placeholder. Thank you for your ideas nonetheless.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Is there a way to access data of core/embed in oembed filters?’ is closed to new replies.