1

I want to use a shortcode to replace it with an image form a url.

My idea:

[shortcode url="http://google.de/aImage.jpg"]

I post this shortcode usinbg IFTTT. After publishing the article the shortcode should be replaced by the image. But the image should come from my "local" library. So the shortcode should download the image, replace it self with a html tag for the image in the library ... that's it :-D

Is it possible to use a shortcode that way? How?

  • Hi Patrick, could you provide more details on what you've tried already (and why it didn't work)? – kraftner Dec 15 '15 at 12:48
1

You don't need a shortcode in a traditional sense. You could use something like {imageurl=""http://google.de/aImage.jpg}

You would use the filter content_save_pre to run before the content is saved to the database a search and replace with the img markup which you get from media_sideload_image(). media_sideload_image() downloads the image from a URL adds it to the media manager and then return the img markup.

Have a look how wp_make_content_images_responsive() works. It something similar as it replaces the old img forma with the responsive image format.

  • Thank you- good idea. But I have some trouble with the add_filter('content_save_pre', 'getImageUrlOutOfContent', 10, 1); - I think. function getImageUrlOutOfContent($content) { preg_match_all('/\\{IFTTTImageFetch="([^"]+)"\\}/i', $content, $matches); $url = $matches[1][0]; $content = str_replace($url, "", $content); return $content; } The content will not be replaced or it will be 0. – Patrick Dec 15 '15 at 23:43
  • I would first debug the matches result. So edd error_log(print_r(,true)); but don't forget to activate define('WP_DEBUG_LOG', true);. Then you can check the results in wp-content/debug.log – grappler Dec 17 '15 at 10:04
1

The sample post should be the content.

Samplepost {IFTTTImageFetch="https://drscdn.500px.org/photo/132307433/m%3D900/4c2b5e1f7f8c4d4a11a56f8f3cfdc565"} some stuff ..asd asd asd

  function getImageUrlOutOfContent($content)
    {
        preg_match_all('/\\{IFTTTImageFetch="([^"]+)"\\}/i', $content, $matches);
        $url = $matches[1][0];
        importRemoteMedia($url, $post->ID);     
        $content = str_replace($url, "", $content);
        return $content;
    }
    add_filter('content_save_pre', 'getImageUrlOutOfContent', 10, 1);

It should parse the prefix and the url - do the download stuff ... and remove it form the content ... but it does't even show a match...

  • fixed it with a workaround ... explode - and splitted the string by my own. – Patrick Dec 18 '15 at 10:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.