Image Import addon: IDs?

Hi, this question is about Autoblog’s Image Import addon. (cacheimages.php)

This addon works perfectly. It “Imports any images in a post to the media library and attaches them to the imported post.”

So, for example, if my rss feed item’s description contains 10 different images, those 10 images are all imported/downloaded into my blog’s media library, and attached to the imported post, displayed (full-size) in the post. Perfect!

Now, I would like to use WordPress’ [gallery] shortcode. That way I can display those 10 images as a gallery, using WordPress auto-generated thumbnails. That shortcode looks like this:

[gallery columns="3" link="file" ids="147,148,149,150,151,152,153,154,155,156"]

Here’s my request: can you please add the possibility to generate that list of IDs? (like in my example above: “147,148,149,150,151,152,153,154,155,156”:wink:

I hope this request was clear, if not, don’t hesitate to ask, and I will try to explain it better.

Thank you very much!

Randy.

  • Jack Kitterhing
    • Code Norris

    Hi there Randy,

    Hope you’re well today and thanks for your question! :slight_smile:

    Autoblog should import and assign ID’s to the media when it’s imported into the media library, you can then use those id’s for the gallery, would that work for you?

    Any issues we’re here to help.

    Thank you!

    Kind Regards

    Jack.

  • randyfranklin
    • Design Lord, Child of Thor

    Hi Jack, thank you for your quick reply, I certainly appreciate it.

    Yes, you are correct, the ID’s are assigned to the media (images) when they are imported. Yes, I want to use those ID’s, I just don’t know how :slight_smile:

    Right now, this addon automatically generates the new url of each imported image in my blog post. Instead, I would like it to generate just the ID’s.

    Not sure if this clears up my request.

    Thanks!

  • Vaughan
    • Support/SLS MockingJay

    Hi @randyfranklin,

    From my understanding, instead of it posting the urls in the post as images,

    You want it to instead create the gallery shortcode and add those to the post instead.

    So in the post, instead of multiple img urls,

    You want it to post them as;

    [gallery columns="3" link="file" ids="147,148,149,150,151,152,153,154,155,156"]

    i’m not sure how easy that would be, but we can certainly add this to the feature requests for the developer to look at, however it’s not something that users would likely use a lot as this is the 1st request i’ve seen for this, so it might be quicker to advertise for a developer to custom create an addon for it. https://premium.wpmudev.org/wordpress-development/

    Hope this helps

  • randyfranklin
    • Design Lord, Child of Thor

    Hi Vaughan, thank you for your reply.

    I confirm, yes that is what I ultimately want to achieve. But… I can do part of the shortcode myself, and I can hide the full-size images in the post myself. The only thing I need generated is the list of ID’s, like for example:

    147,148,149,150,151,152,153,154,155,156

    That’s all I need.

    I’m pretty sure this must be very easy to do. In a previous version of Autoblog, this was the last comment in the cacheimages.php file:

    Returning the $post_ID even though it’s an action and we really don’t need to

    So my guess is that the ID is generated, but not used or displayed anywhere. You just need to add it to an array (on each imported image) and and give me the option to display it in my post.

    this is the 1st request i’ve seen for this

    Just like being able to post feature images or youtube clips, I am convinced this would be a very interesting and very valuable addition this the already awesome Autoblog. Thank you very much!

  • randyfranklin
    • Design Lord, Child of Thor

    Thanks Jack.

    So, what does future development mean? Timing? I will not get a solution right now?

    Please allow me to explain one more time: I’m pretty sure my request can be taken care of with two lines of code. It just needs to store the ID for every image that was imported into a variable, and I should be able to insert that variable (or the list of ID’s) into my post. That’s it.

    This must be a piece of cake for your team :slight_smile:

    This little piece of code will really help me move forward in my project. Thank you for your help and understanding.

    Best regards,

    Randy.

  • Vaughan
    • Support/SLS MockingJay

    Hi @randyfranklin,

    Feature requests can take a while, and also depends on the popularity of the request.

    I have just flagged one of our developers to see if he can take a look at this for you, if it’s not too complex, then he may be able to provide a solution for you quickly. Hopefully he should respond shortly.

    Hope this helps

  • Hoang Ngo
    • Code Slayer

    Hi @randyfranklin,

    I hope you are well today

    Please add this code to your theme functions.php or use mu-plugins

    function gallery_id_meta_box() {

    $screens = array( 'post' );

    foreach ( $screens as $screen ) {

    add_meta_box(
    'gallery_id_meta_box',
    __( 'Gallery ID', 'domain' ),
    'gallery_id_meta_box_callback',
    $screen
    );
    }
    }
    add_action( 'add_meta_boxes', 'gallery_id_meta_box' );
    function gallery_id_meta_box_callback(){
    echo implode(',',array_keys((array)get_attached_media( 'image',get_the_ID() )));
    }

    This will add a small box in your post edit page, if the post have images, it will show the ID list :slight_smile:

    If you have any issues please don’t hesitate to let us know so we can assist

    Best regards,

    Hoang

  • randyfranklin
    • Design Lord, Child of Thor

    Hi Hoang,

    Thank you very much.

    Yes, this function adds a box to my post edit page, and it shows the ID’s of the images in the post. Thank you.

    I really hope you won’t mind, I certainly don’t want to sound ungrateful, and I appreciate your assistance, but this is not entirely what I’m looking for. But it might be a part of the solution, so please allow me to explain:

    1. I use Autoblog to automatically add posts to my blogs, from rss feeds.

    2. I use Autoblog’s “Import Images” add-on to automatically import any images in a post to my WP’s media library and attach those images to the imported post.

    3. My request: I want the list of ID’s (of those imported and attached images) in my post. Your solution gives me the ID’s in a separate box, so I have to open the post, and copy the ID’s manually and paste them manually in my post.And I want it done automatically :slight_smile:

    Solution: I really think the solution is in the “Import Images” add-on, the file named cacheimages.php.

    That file replaces the original url’s of images with the new url’s of images (the images are now located in my blog’s media directory) and inserts those new url’s in my post. The only thing I want changed is that it would display the ID of the image :slight_smile: And if more than one image: a text list of the ID’s.

    I really hope I have explained this correctly. (English is not my main language.) Again, THANK YOU for your assistance and your patience with me.

    Randy.

  • Hoang Ngo
    • Code Slayer

    Hi there @randy,

    You welcome :slight_smile:. I’m glad to assist you.

    Just for clarify, so instead of display the image url, you want to display the image id by the gallery shortcode?

    If multiple images next to each other, unless they are in a wrapper we can identify, there will be hard to recognize that. If possible, I think better we can group all the images of a post, and then post the gallery at the end of the post. How about your idea?

    Please advise,

    Thanks.

    Best regards,

    Hoang

  • randyfranklin
    • Design Lord, Child of Thor

    Thank you Hoang,

    Just for clarify, so instead of display the image url, you want to display the image id by the gallery shortcode?

    Yes, I want to display the image ID.

    Yes, I want to use those image ID’s in the WordPress gallery shortcode.

    I think better we can group all the images of a post, and then post the gallery at the end of the post.

    Yes, that would be perfect!

    Thank you!!

  • randyfranklin
    • Design Lord, Child of Thor

    Thank you Hoang,

    Just for clarify, so instead of display the image url, you want to display the image id by the gallery shortcode?

    Yes, I want to display the image ID.

    Yes, I want to use those image ID’s in the WordPress gallery shortcode.

    I think better we can group all the images of a post, and then post the gallery at the end of the post.

    Yes, that would be perfect!

    Thank you!!

  • Hoang Ngo
    • Code Slayer

    Hi @randyfranklin,

    I hope you are well today

    Can you please replace the code I sent before with this code? :slight_smile:

    add_filter( 'the_content', 'replace_images_with_gallery' );
    function replace_images_with_gallery( $content ) {
    if ( ( $feed_id = get_post_meta( get_the_ID(), 'original_feed_id', true ) ) > 0 ) {
    //remove all the images
    $doc = new DOMDocument();
    $doc->loadHTML( $content );
    $images = $doc->getElementsByTagName( 'img' );
    foreach ( $images as $key => $image ) {
    $image->parentNode->removeChild( $image );
    }
    $new_content = $doc->saveHTML();

    $images = get_attached_media( 'image', get_the_ID() );
    if ( ! empty( $images ) ) {
    $new_content = $new_content . '[gallery columns="3" link="file" ids="'.implode(',',array_keys($images)).'"]';
    }

    return $new_content;
    }
    return $content;
    }

    Please give it a try and let see if it work

    Best regards,

    Hoang

  • randyfranklin
    • Design Lord, Child of Thor

    Hi Hoang, I hope you’re fine.

    Thank you for this new code.

    I have tested it, as you requested, and this is what I see:

    1. Yes, this works! It replaces all full-size images in my post with the gallery (handled by the WordPress gallery shortcode). Very nice! Thank you.

    2. If I’m not mistaking, this is a “general” function, this is not related to Autoblog at all. And as a result, it replaces all the full-size images in all my posts with a gallery. All my posts = new posts and old posts.

    (Example: I have old posts with just one full-size image in them, and now it shows the gallery thumbnail, because that full-size image has been replaced with a gallery.)

    Conclusion: This function works perfectly, but can you change it so that it is only activated when adding new posts via Autoblog?

    Also, if possible, can you hardcode the gallery shortcode in the post text? (That’s important when I export/import posts or move blogs or upgrade WordPress.)

    Thank you!!

    Randy.

  • Hoang Ngo
    • Code Slayer

    Hi @randyfranklin,

    I hope you are well today and thanks for your comment.

    Currently now, the code don’t modify the post content, and only posts import fro AutoBlog take affect.

    Also, I found that your idea is a great thing for new AutoBlog add-on, I will include that to future release :slight_smile:

    Thank you again!!!

    Best regards,

    Hoang

  • randyfranklin
    • Design Lord, Child of Thor

    Ho Hoang, thank you for your reply. Please forgive me to insist.

    In a previous message you asked me:

    Just for clarify, so instead of display the image url, you want to display the image id by the gallery shortcode?

    and I replied:

    Yes, I want to display the image ID.

    And now you confirm that your new code:

    the code don’t modify the post content

    so the ID’s are still not displayed.

    Can you please adapt the cacheimages.php file (this is the addon file) so that it displays the list of IDs in my post? And only when I import new posts.

    Thank you.

    I appreciate your help.

    Randy.

  • randyfranklin
    • Design Lord, Child of Thor

    Hi Hoang, how are you?

    Thank you for your reply, I certainly appreciate it.

    I replaced the cacheimages.php with this new version as you requested, and I tested it.

    Unfortunately I couldn’t make it work.

    What happens is: the full-size images get attached to the post, with their original url, so I have a feeling something else is not working now in this addon. The images were not imported in the media directory. The addon doesn’t seem to be working.

    More details: I use Autoblog 4.0.9.3

    The Image Import addon is the only addon activated

    I have no other plugins active, except Akismet.

    Thank you.

    Best regards,

    Randy.

  • randyfranklin
    • Design Lord, Child of Thor

    Hi Hoang, how are you today?

    I have upgraded to 4.0.9.4 as suggested, and I have tested your new cacheimages file again. And now it works! :slight_smile:

    THANK YOU! This is exactly the solution I was looking for. This works perfectly. I’m very happy.

    For your information: in the sourcecode, it shows :

    <img src=""><img src=""><img src=""><img src="">

    where the pictures used to be.

    That is no problem for me, because that code is not visible in the browser window.

    But I’m just mentioning it because if the pictures were not in the same paragraph, WordPress creates a new paragraph like this:

    <p><img src=""><img src=""><img src=""><img src=""></p>

    and depending on the css, this might create an additional space.

    Again, thank you for this great solution.

    I have one last request: can you please make a version of this new cacheimages.php for cacheandfeatureimages.php as well?

    Thank you.

    Best regards,

    Randy.

  • Hoang Ngo
    • Code Slayer

    Hi @randyfranklin,

    I hope you are well today

    About the normal images, we can import them to the post content using direct link or gallery shortcode. Feature images is different, as it handle by themes (“post-thumbnails”:wink:. So in this case, I don’t think the gallery shortcode can work in this case.

    Also many thanks for your patience here :slight_smile:

    If you have any issues please don’t hesitate to let us know so we can assist

    Best regards,

    Hoang

  • randyfranklin
    • Design Lord, Child of Thor

    Hi Hoang, I hope you’re fine.

    I wanted to come back regarding one particular issue with the solution you provided. As I mentioned before, this solution works great, except for the fact that it leaves this code in the html source of the blog post:

    <img src=""><img src=""><img src=""><img src="">

    (One <img src=””> for every large-size picture that was imported and replaced with the ID.)

    Now, in Chrome, the regular surfer does not see this.

    But in IE and in Firefox, these browsers shows the broken picture icon.

    So I was hoping you have a solution to remove all the empty <img src=””> code snippets from the blog post.

    Thank you!

    Randy.

  • Jose
    • Bruno Diaz

    Hey there @randyfranklin,

    To avoid that glitch with the empty <img> tags left behind when importing to a gallery shortcode directly, you should change Hoang’s code in cacheimages.php line 38 to look like this:

    public function import_post_images( $post_id, $details, $item ) {
    $post = get_post( $post_id );
    $new_images = $this->_import_post_images( $post->post_content, $details, $post_id );

    if ( count( $new_images ) ) {

    foreach ( $new_images as $key => $val ) {
    $post->post_content = str_replace( $key, '', $post->post_content );
    }
    $post->post_content .= '[gallery columns="3" link="file" ids="' . implode( ',', $new_images ) . '"]';
    $post->post_content = str_replace( '<img src="">', '', $post->post_content );//CHANGED
    wp_update_post( $post->to_array() );
    } else {
    //something happen, we will need to use the raw content, please note that simplepie will
    //sanitize content, this is the most case cause google news image don't display
    $new_images = $this->_import_post_images( $this->_get_simplepie_item_raw( $item ), $details, $post_id );
    if ( count( $new_images ) ) {
    $replaced_content = $this->_replace_content_with_new_images( $new_images, $post->post_content );
    $replaced_content .= '[gallery columns="3" link="file" ids="' . implode( ',', $new_images ) . '"]';
    $post->post_content = $str_replace( '<img src="">', '', $replaced_content );//CHANGED
    wp_update_post( $post->to_array() );
    }
    }
    }

    Note that there are only two new/altered lines marked with the comment ‘//CHANGED’.

    (of course, this will solve the problem for future imports, it wont fix already imported posts)

    Please let me know if this solves your issue.

    Cheers!

    Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.