0

Background of my Problem: I have several hundred images with urls stored in a Toolset Types custom wordpress embedded field, along with video urls. I can display my videos no problem from the embedded field but the images only show the URL not the image.

Solution approach: I'm trying to write a function to register an embed handler for self hosted images: where the URL refers to my own site, and the function will spit out the markup I need, in this case an image tag.

Here is a sample URL for one of my images: https://mysite.ca/mydirectorypath/sampleimage.jpg

I've looked here https://wordpress.stackexchange.com/search?q=wp_embed_register_handler

but I don't see an example for images plus I don't understand regex. I was hoping someone could help me modify a function like this below from https://codex.wordpress.org/Function_Reference/wp_embed_register_handler to fit my self hosted image scenario:

<?php

wp_embed_register_handler( 'forbes', '#http://(?:www|video)\.forbes\.com/(?:video/embed/embed\.html|embedvideo/)\?show=([\d]+)&format=frame&height=([\d]+)&width=([\d]+)&video=(.+?)($|&)#i', 'wp_embed_handler_forbes' );

function wp_embed_handler_forbes( $matches, $attr, $url, $rawattr ) {

    $embed = sprintf(
            '<iframe src="http://www.forbes.com/video/embed/embed.html?show=%1$s&format=frame&height=%2$s&width=%3$s&video=%4$s&mode=render" width="%3$spx" height="%2$spx" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>',
            esc_attr($matches[1]),
            esc_attr($matches[2]),
            esc_attr($matches[3]),
            esc_attr($matches[4])
            );

    return apply_filters( 'embed_forbes', $embed, $matches, $attr, $url, $rawattr );
}

?>

many thanks

3
  • 1
    Would this not require you to copy paste those values out of the post meta and into the post content so that OEmbed can be used? Would it not be easier to just retrieve the URLs via get_post_meta then put them into img and iframe tags? Note that regular expressions aren't a WP thing, you could take matching that URL and eliminate the WordPress component entirely and ask about that part on stack overflow, and get a lot more eyes on it, otherwise anybody answering has to know both WordPress and regular expressions
    – Tom J Nowell
    Jan 31, 2020 at 19:11
  • I'm using Toolset views shortcodes to retrieve the embedded field url data. But I see your suggestion to just check the URL's to see if they are an image and put the URL in an img tag and if it's an image put it into an iframe tag would work. I just have to write a function to check for a jpg in the url. I was hoping to have a simpler approach with this question but maybe it's harder than I thought. Thanks for your comment! Jan 31, 2020 at 20:07
  • Checking the file type of the URL is a considerably simpler solution than a custom embed hander. Feb 1, 2020 at 5:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.