Hot answers tagged

24

Open to anybody who can simplify this but here's what I came up with that worked for me. First thing's first - get the gallery, using get_post_gallery(), as soon as the loop starts: <?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); $gallery = get_post_gallery(); $content = ...


19

Ok, I've been playing with this for a little bit and have managed to change the output of the Gallery block, with the following caveats: The preview does not match the output. I think this is possible but appears to be a bit more involved. Certain classes and markup are required in the output for the block to be able to parse the content and keep it ...


17

There is an easier solution now: function my_gallery_default_type_set_link( $settings ) { $settings['galleryDefaults']['link'] = 'file'; return $settings; } add_filter( 'media_view_settings', 'my_gallery_default_type_set_link'); They added a filter to customize this default value (and other values related to the new media upload popup) in WP 4.0 (...


15

All those complex functions can be reduced to one simple function: attachment_url_to_postid() You only need to parse the image URL to retrieve the attachment ID: $attachment_id = attachment_url_to_postid( $image_url ); echo $attachment_id; That's all you need.


14

First you have to get the images. How to get all images of a gallery is described here. WordPress uses two classes for unzipping files. The PHP bilt in ZipArchive() (usage see David Walsh). And PclZip, you can found this class in wp-admin/includes/class-pclzip.php. If you got problems with ZipArchive() try the PclZip class. Now you just to have to glue ...


12

Answering my own question: As suggested by WebElaine I created a completely new block to fix the problem. To make this task easier, I used create-guten-block by Ahmad Awais, which is pretty easy to set up. In init.php I made sure to enqueue my JavaScript and extra CSS for the frontend. This should be straight forward. I recommend reading the Learning ...


11

When you create a gallery via the 3.5 media manager, menu_order is no longer used to save the order of the images, the order only exists in the shortcode you insert when you click the Insert Gallery button, via the ids= attribute. This is to accommodate the fact that you can now add multiple galleries to a single post- ordering via menu_order wouldn't work ...


11

From the article How to use WordPress 3.5 Media Uploader in Theme Options (by codestag.com), as seen in Using the WordPress 3.5 Media Uploader within plugins (by mikejolley.com). Mike Jolley's article has quite some nice tricks. Important note: if the page where the uploader is going to be used doesn't already have all media JS APIs, then wp_enqueue_media() ...


11

The source for the gallery shortcode is in wp-includes/media.php. There are a couple of hooks in there that might work for you. Without knowing exactly what you want to do, it is impossible to be more specific. If that doesn't work then you can remove the shortcode: remove_shortcode('gallery'); And add another gallery shortcode with the alterations you ...


11

Here are three methods Approach #1 It's common to wrap the output with custom HTML. We can do that by using the post_gallery filter and the gallery_shortcode() callback: /** * HTML Wrapper - Support for a custom class attribute in the native gallery shortcode */ add_filter( 'post_gallery', function( $html, $attr, $instance ) { if( isset( $attr['...


8

You can make it invisible with a simple trick: add_shortcode( 'gallery', '__return_false' ); This will remove the code from output without touching the post content.


8

Put this into your page template: <?php global $nggdb; $galleries = array(); $album = $nggdb->find_album(1); foreach( $album->gallery_ids as $galleryid ){ $gallery = $nggdb->find_gallery($galleryid); $galleries[$galleryid]['title'] = $gallery->title; $galleries[$galleryid]['url'] = get_bloginfo('url') ...


8

There is a simple solution for this. Let the gallery use the 5 column grid system and using the @media-queries we can alter it for tablet and mobile. I just tested it on your given URL and it works perfect. But try to remove any custom codes you have used before since I had to remove it in order to use the following code. /* For displaying 3 columns on ...


7

There is no need to copy the gallery shortcode. Just set your filter function between the native handler’s output and the final output. To do that, hijack the shortcode handler, then run a preg_replace_callback() over the output. add_action( 'after_setup_theme', 'wpse_74515_replace_gallery_shortcode' ); /** * Replace the default shortcode handlers. * * ...


6

You can do this pretty easily using the do_shortcode function. Check if an instance of [gallery] exists in your post content. Here's a simple function to drop in functions.php that checks the current post's content for the gallery shortcode: function gallery_shortcode_exists(){ global $post; # Check the content for an instance of [gallery] with ...


6

I've written answer to How to paginate attachments in a secondary query as gallery?, which can be used for solving this problem. Like @wpmudev-ari said the [gallery] shortcode can be rewritten, or a new shortcode could be created. I explain below - B. - how this can be done. Additionally there is a very simple solution - A. - for the pagination of the ...


6

There is a new shortcode_atts_{$shortcode} filter in WordPress 3.6 according to Mark Jaquith. You could use the shortcode_atts_gallery filter to force the link='file' attribute: add_filter('shortcode_atts_gallery','overwrite_gallery_atts_wpse_95965',10,3); function overwrite_gallery_atts_wpse_95965($out, $pairs, $atts){ // force the link='file' gallery ...


6

@tfer77 has given the right answer. I am here explaining it in a more better way. You need to add size="full" attribute to the gallery shortcode in the post content like [gallery ids="836,830,829" size="full"] Now when you use $gallery = get_post_gallery_images( $post->ID ); You will get full size images. You can check the same by using the filter ...


6

Deleting images using plugin: You can use this plugin, it will search your database and look if image is inserted into any post (in content, as featured image, in any custom field, anywhere...) or as background... If image is not used anywhere it will give you option to delete it. You will get list of all images on your site not used anymore, so you can ...


6

You can override the gallery shortcode link attribute with: add_filter( 'shortcode_atts_gallery', function( $out ){ $out['link'] = 'file'; return $out; } ); This means that even if your shortcodes are: [gallery ids="1,2,3"] [gallery ids="1,2,3" link="none"] the gallery output will always be generated as if you used link="file".


5

jQuery(".gallery-icon a").fancybox().attr('data-fancybox', 'wp-gallery-fancybox'); Gives all links the same rel attribute, this way the user will be able to slide between the images.


5

if you set image size in function.php as a "gallery" add_image_size( 'gallery', 200, 120, true ); you can remove width and height of specific image size such as "gallery": add_filter('wp_get_attachment_link', 'remove_img_width_height', 10, 4); function remove_img_width_height( $html, $post_id, $post_image_id,$post_thumbnail) { if ($post_thumbnail=='...


5

Thanks to the hint with the multiple galleries plugin from Niall Campbell and thanks to this question How to Add a Custom Colum on Thickbox Media Gallery Tab? (where I got the hook admin_head-media-upload-popup from), I was able to complete the task. I've added an option to add a style attribute to the gallery shortcode. Here is the complete code: ...


5

You can hijack the shortcode handler and set the attribute to a value of your choice. Then call the native callback for this shortcode. add_shortcode( 'gallery', 'file_gallery_shortcode' ); function file_gallery_shortcode( $atts ) { $atts['link'] = 'file'; return gallery_shortcode( $atts ); }


5

Old question but still getting search traffic so for everyone else: You can accomplish what you describe by filtering the gallery shortcode attributes. Also useful if you want to change the default image size, columns, etc. function gallery_should_link_to_files($out, $pairs, $atts) { $atts = shortcode_atts( array( 'link' => 'file' ), $atts ...


5

If you want get_post_gallery_images to give you full size images, you can use the following: // Use full size gallery images for the next gallery shortcode: add_filter( 'shortcode_atts_gallery', 'wpse_141896_shortcode_atts_gallery' ); // Your code: $gallery = get_post_gallery_images( $post ); foreach ($gallery as $img) { ?> <li><img src="&...


5

There is now (from version 4.0) much better solution which actually sets the default option: function my_gallery_default_type_set_link( $settings ) { $settings['galleryDefaults']['link'] = 'file'; return $settings; } add_filter( 'media_view_settings', 'my_gallery_default_type_set_link'); view: Overide Gallery Default Link to Settings


5

If you're comfortable with the complete opposite of the linked answer, you simply could use this query to fetch all posts that have an attachment and then use their IDs as the post__not_in parameter of \WP_Query: $attachment_args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', '...


5

WordPress doesn't make it very easy to modify aspects of the gallery shortcode. Some attributions: Thanks to user peterbra for really putting the whole thing together a year ago. Thanks to user birgire for coming up with a solution as far as adding the attribute in a meaningful way. The other option besides birgires ( as far as I know ) is to pretty much ...


5

You can try the following to clear the image attachment's title when it's inserted but not updated: /** * Empty the image attachment's title only when inserted not updated */ add_filter( 'wp_insert_attachment_data', function( $data, $postarr ) { if( empty( $postarr['ID'] ) && isset( $postarr['post_mime_type'] ) &&...


Only top voted, non community-wiki answers of a minimum length are eligible