Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle/manage custom images?

I am working on a special plugin for a customer.

The situation in short:
The plugin contains a automatic import for a .zip file. Inside this files are one .xml file and images. The plugin reads the .xml file and insert the information to the database.

My question:
How I can handle the images the best way. Should I import them into the wordpress gallery or should I manage them on my own. Is there a way to use the wordpress gallery, because it will automatic generate thumbnails, or is it not a good idea?

I need some suggestions. Thanks!

like image 248
yfain Avatar asked May 24 '18 07:05

yfain


2 Answers

You should add images in wordpress gallery. Then you have to get these uploaded images from the wordpress gallery:

Step 1: Prepare the query

global $post;

$args = array(
    'post_parent'    => $post->ID,           // For the current post
    'post_type'      => 'attachment',        // Get all post attachments
    'post_mime_type' => 'image',             // Only grab images
    'order'          => 'ASC',               // List in ascending order
    'orderby'        => 'menu_order',        // List them in their menu order
    'numberposts'    => -1,                  // Show all attachments
    'post_status'    => null,                // For any post status
);

First, we set up the global Post variable ($post) so we can have access to the relevant data about our post.

Second, we set up an array of arguments ($args) that define the kind of information we want to retrieve. Specifically, we need to get images that are attached to the current post. We're also going to get all of them, and return them in the same order they appear in the WordPress gallery.

Step 2: Retrieve the Images from Wordpress gallery

// Retrieve the items that match our query; in this case, images attached to the current post.
$attachments = get_posts($args);

// If any images are attached to the current post, do the following:
if ($attachments) { 

    // Initialize a counter so we can keep track of which image we are on.
    $count = 0;

    // Now we loop through all of the images that we found 
    foreach ($attachments as $attachment) {

Here we are using the WordPress get_posts function to retrieve the images that match our criteria as defined in $args. We are then storing the results in a variable called $attachments.

Next, we check to see if $attachments exists. If this variable is empty (as will be the case when your post or page has no images attached to it), then no further code will execute. If $attachments does have content, then we move on to the next step.

Set parameters for a WordPress function called wp_get_attachment_image for the images information.

Source: Read the link for complete tutorial or other steps > https://code.tutsplus.com/tutorials/how-to-create-an-instant-image-gallery-plugin-for-wordpress--wp-25321

like image 87
stefan Avatar answered Oct 04 '22 00:10

stefan


You should add images in wordpress gallery. Then you have to get these uploaded images from the wordpress gallery:

Step 1: Prepare the query

global $post;

$args = array(
    'post_parent'    => $post->ID,           // For the current post
    'post_type'      => 'attachment',        // Get all post attachments
    'post_mime_type' => 'image',             // Only grab images
    'order'          => 'ASC',               // List in ascending order
    'orderby'        => 'menu_order',        // List them in their menu order
    'numberposts'    => -1,                  // Show all attachments
    'post_status'    => null,                // For any post status
);

First, we set up the global Post variable ($post) so we can have access to the relevant data about our post.

Second, we set up an array of arguments ($args) that define the kind of information we want to retrieve. Specifically, we need to get images that are attached to the current post. We're also going to get all of them, and return them in the same order they appear in the WordPress gallery.

Step 2: Retrieve the Images from Wordpress gallery

// Retrieve the items that match our query; in this case, images attached to the current post.
$attachments = get_posts($args);

// If any images are attached to the current post, do the following:
if ($attachments) { 

    // Initialize a counter so we can keep track of which image we are on.
    $count = 0;

    // Now we loop through all of the images that we found 
    foreach ($attachments as $attachment) {

Here we are using the WordPress get_posts function to retrieve the images that match our criteria as defined in $args. We are then storing the results in a variable called $attachments.

Next, we check to see if $attachments exists. If this variable is empty (as will be the case when your post or page has no images attached to it), then no further code will execute. If $attachments does have content, then we move on to the next step.

Set parameters for a WordPress function called wp_get_attachment_image for the images information.

Source: Read the link for complete tutorial or other steps > https://code.tutsplus.com/tutorials/how-to-create-an-instant-image-gallery-plugin-for-wordpress--wp-25321

like image 4
youpilat13 Avatar answered Oct 12 '22 06:10

youpilat13