admin管理员组文章数量:1122826
I want to create a WordPress shortcode that will add an automated gallery of the contents in a folder. I am currently trying to implement this in a site that publishes 12 annual magazine issues, so as soon as a new PDF or JPG is dropped into a folder, it should be displayed on that specific gallery.
Meaning:
Archive
2013
Jan - Dec months need to be displayed, in a 4x3 or 6x2 grid gallery. If we are in June, and the latest issue is June, then only 6 JPGs would be displayed.
This way the client doesn't need to make any modifications in the WordPress site. Instead the client can just drop the files to a specific folder via FTP, and the WordPress PHP function would auto populate the gallery according to what it's able to fetch from that folder's contents.
EDIT
Unfortunately, the "Folder Gallery" plugin is close but not good enough. It does not read the contents of folders that sit outside the WP uploads folder, and we don't want to migrate the existing folder structure inside the WordPress install folder.
I also need it to open the links in a new window to the PDF of the magazine, and not a larger version of the picture in Fancybox.
I want to create a WordPress shortcode that will add an automated gallery of the contents in a folder. I am currently trying to implement this in a site that publishes 12 annual magazine issues, so as soon as a new PDF or JPG is dropped into a folder, it should be displayed on that specific gallery.
Meaning:
Archive
2013
Jan - Dec months need to be displayed, in a 4x3 or 6x2 grid gallery. If we are in June, and the latest issue is June, then only 6 JPGs would be displayed.
This way the client doesn't need to make any modifications in the WordPress site. Instead the client can just drop the files to a specific folder via FTP, and the WordPress PHP function would auto populate the gallery according to what it's able to fetch from that folder's contents.
EDIT
Unfortunately, the "Folder Gallery" plugin is close but not good enough. It does not read the contents of folders that sit outside the WP uploads folder, and we don't want to migrate the existing folder structure inside the WordPress install folder.
I also need it to open the links in a new window to the PDF of the magazine, and not a larger version of the picture in Fancybox.
Share Improve this question edited Oct 27, 2014 at 16:04 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Jun 10, 2013 at 22:00 FredFred 211 silver badge2 bronze badges 3- You may be able to integrate the ImgBrowz0r class which does read the contents of a directory and creates a gallery of preview images in a grid layout. – iyrin Commented Jun 11, 2013 at 1:20
- Is there a ImgBrowz0r plugin outhere i can just grab? i am pretty new when it come to php – Fred Commented Jul 3, 2013 at 18:53
- Did you ever find a solution? I have a similar situation. – Ricardo Andres Commented Nov 19, 2015 at 15:16
1 Answer
Reset to default 1It sounds like you just need shortcode to gather the contents of a folder and display a grid of those items. The shortcode is pretty easy to setup.
$gallery = do_shortcode('[folder_gallery title="XYZ" folder="wp-content/uploads/2015/11"]' );
echo $gallery;
Then you just need to glob a directory and render your gallery from the files. I didn't include a gallery but this will work outside of uploads. Just give it a path relative to the root folder.
// [folder_gallery folder="wp-content/uploads/2015/11" ]
add_shortcode( 'folder_gallery', 'folder_gallery__shortcode' );
function folder_gallery__shortcode( $atts ) {
$a = shortcode_atts(
array (
'folder' => '',
'title' => '',
), $atts );
$folder = $a [ 'folder' ];
// bad folder
if ( empty( $folder ) || ! is_readable(ABSPATH . $folder) ) {
return 'No Valid Folder Selected';
}
// allow filtering of the filetypes
$filetypes = apply_filters( 'folder_gallery_shortcode__filetypes', array ( 'png', 'jpg', 'jpeg', 'gif' ) );
// glob
$filetypes = empty( $filetypes ) ? 'png,jpg,jpeg,gif' : implode( ',', $filetypes );
$files = glob( untrailingslashit( ABSPATH . $folder ) . "/*.{" . $filetypes . "}", GLOB_BRACE );
$gallery_images = array ();
foreach ( $files as $file ) {
if ( $file === __FILE__ ) {
continue;
}
/*
$filetype = wp_check_filetype( $file );
$ext = $filetype[ 'ext' ];
$type = $filetype[ 'type' ];
$proper_filename = $filetype[ 'proper_filename' ];
*/
// replace the filepath with url path for front-end gallery
$gallery_images[] = str_replace( trailingslashit( ABSPATH ), trailingslashit( WP_SITEURL ), $file );
}
// TODO: IMPLEMENT YOUR GALLERY HERE
// construct the gallery
$output = empty( $a[ 'title' ] ) ? '' : '<h2>' . $a[ 'title' ] . '</h2>';
$output .= '<ul>';
// Loop through each image in each gallery
foreach ( $gallery_images as $image_url ) {
$output .= '<li>' . '<img src="' . $image_url . '">' . '</li>';
}
$output .= '</ul>';
// allow filtering of the output
$gallery = apply_filters( 'folder_gallery_shortcode__gallery', $output, $gallery_images );
return $gallery;
}
本文标签: plugin developmentAuto gallery from folder contents
版权声明:本文标题:plugin development - Auto gallery from folder contents 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736292383a1928919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论