• Resolved gnr5

    (@gnr5)


    Hi, I know you don’t provide support to the import, but would it be possible to modify the import to also include an url to an image?

    Appreciate your effort!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Harmonic Design

    (@harmonic_design)

    Hi gnr5 (dope name and profile pic!),
    this is something that is technically possible but will probably require more work than it’s worth. Up to you to decide!

    HD Quiz stores images by ID – not direct URL paths. This is so that your site can use srcset, lazy loading, etc. What this means is that you can’t really select an image by URL path (more on this below), BUT, if you upload your images beforehand and get the IDs of each image, then we can use that instead.

    PRO TIP: Ids are sequential. So if you upload 100 images and the first image has an id of 4400, then the last image will have an id of 4500. This way you don’t have to check each and every image. Just note that I have a feeling that the order they are “added” is irrelevant and ids are assigned based on “upload completed” – so something to pay attention to.

    So if you want to go this route, what can you do? Based on your past post history, I’m going to assume that you are fairly comfortable with making small coding edits, so let’s dive in!

    On ./includes/tools/csv_import.php on line 249 you will see $fields["featured_image"]["value"] = 0;

    This is where we essentially set the featured image ID to 0 – so no image. If you were to replace 0 with a value from your CSV however…

    $fields["featured_image"]["value"] = intval($csvAsArray[$start][14]);

    you’ve now set the featured image ID to the 15th column from your CSV!

    But what if this isn’t good enough. What if you really, really want to use URLs instead of IDs. Well doing the following should be an absolute last case scenario, and I will REFUSE to offer any further support going down this path – you can take a look at ./includes/functions.php. There is a function hdq_get_attachment_id which attempts to get the id of an image from a URL.

    So example code would look something like this.

    $image_url = sanitize_text_field($csvAsArray[$start][14]);
    $image_id = hdq_get_attachment_id($image_url);
    $fields["featured_image"]["value"] = $image_id;
    Thread Starter gnr5

    (@gnr5)

    Thanks! for your response, about the username, yes, big big bigggg fan of GNR.

    You provided great insights, option A is possible, already implemented it and it works however going back and forth getting the IDs to match the correct question is a little annoying. I agree with you, might be more work than what its worth.

    Option B, will need to work on it a little bit more. Ideally with a URL it would need to added it to the server, then process it into the media library, then get the id.

    Option C: Maybe find a plugin that pulls images from Creative Common libraries and attach as feature image. There is one but is quite buggy

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Import images’ is closed to new replies.