Ask your WordPress questions! Pay money and get answers fast! (more info)

Put images to site's gallery automatically by custom field (php + Meta Slider and Carousel with Lightbox Pro) WordPress

  • SOLVED

Hi everybody,

I'm working on a theme which needs a little extras now but I don't know if this function is even possible:
We are working with lots of pictures located in specific folders like in this hierarchy example:

01-Water
01-1-Fish
01-2-Ducks
01-3-Ponds
01-3-1-smallPonds
01-3-2-largePonds

02-Air
02-1-Birds
02-2-Clouds
02-3-Planes

etc.

For every of these folders will be a WP-Site with description and a slideshow made with Meta Slider and Carousel with Lightbox Pro.
Now I'd like to create a custom field where I enter the path to the specific folder to show the right pictures automatically on each site like:

Custom field: /01-Water/01-3-Ponds/01-3-1-smallPonds/

All pics in this folder will be put automatically into the gallery then.

Is there any solution or can you recommend any other advice?

Many thanks in advance!

Answers (1)

2017-09-02

Reigel Gallarde answers:

I don't have Meta Slider and Carousel with Lightbox Pro, so what I can give is only an insight.

I'm guessing, that the Meta Slider and Carousel with Lightbox Pro is saving gallery images in a custom field by ids.
It should be an array of IDs of the images.

if that's the case, I can think of two functions here to be created.
One is to get all the images' url based on the folder given.
Another function for getting the id of the images based on the url given.

Something like these two functions:
https://pastebin.com/ABUyJLHk (I'm using pastebin because code formatting of wpquestions.com is broken)

here's how you would typically use it.


$folder = '/2013/06/'; // would much something like websites.com/wp-content/uploads/2013/06/T_7_front-1-300x300.jpg
$media_arr = wp2432_get_image_ids_by_folder( $folder );
print_r( $media_arr );


$media_arr will now hold the array of ids of the images. you can then save it as the custom field of Meta Slider and Carousel with Lightbox Pro that is responsible for the galleries.

A more related example of usage.

// $id = id of the post or the post type gallery that the plugin is using.
$folder = get_post_meta( $id, 'gallery-path', true );
$media_arr = wp2432_get_image_ids_by_folder( $folder );
update_post_meta( $id, '<the custom field>', $media_arr );


Reigel Gallarde comments:

I have checked the free version and my theory works... here's the code.

add_action( 'save_post', 'wp2432_igsp_save_metabox_value', 20 );

function wp2432_igsp_save_metabox_value( $post_id ){
global $post_type;

$registered_posts = wp_igsp_get_post_types(); // Getting registered post types

if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // Check Autosave
|| ( ! isset( $_POST['post_ID'] ) || $post_id != $_POST['post_ID'] ) // Check Revision
|| ( !current_user_can('edit_post', $post_id) ) // Check if user can edit the post.
|| ( !in_array($post_type, $registered_posts) ) ) // Check if user can edit the post.
{

$folder = '/2013/06/';
$gallery_imgs = wp2432_get_image_ids_by_folder( $folder );
update_post_meta($post_id, '_vdw_gallery_id', $gallery_imgs);

}


you should replace $folder with your post meta or $folder = $_POST['gallery-path'] perhaps?


Reigel Gallarde comments:

above code has error.. please check pastebin https://pastebin.com/UKZ9L6LE


Reigel Gallarde comments:

by the way, this assumes that you have the image already uploaded as media..
because Meta Slider and Carousel with Lightbox will only accept IDs of the media and not the url.