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 ...


18

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 (...


16

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

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

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

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

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 ...


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

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 );...


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".


6

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'] ) &&...


6

Are you writing a template? A filter in functions.php or a plugin? A straightforward method could be using get_post_gallery with the second argument set to false, so that it return the object rather than the html. if ( get_post_gallery() ) : //Get the gallery object $gallery = get_post_gallery( get_the_ID(), false ); //Form an array with the ...


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

This is probably a good case for a global variable. Just make sure it is set to an array before you add to it and also when to attempt to output the content. Convert the array to a string before adding to content. Also, make sure the meta data exists before adding to the array -- that way you're not adding empty copyright values. GLOBAL VERSION function ...


5

In order for your images to be responsive they require CSS. The CSS will inform the image to stretch 100% of the available space and to automatically adjust the height. img.responsive { width: 100%; height: auto; } Because you don't want to apply this rule to all images, you'll need to add a class on the target image which tells it to use these ...


5

Using birgire's suggestions, I came up with the following solution that is working well with both the old pre-WP 4.x data and the newer posts that have been added under 5.0. // if there is a gallery block do this if (has_block('gallery', $post->post_content)) { $post_blocks = parse_blocks($post->post_content); $ids = $post_blocks[0][attrs][...


4

Core shortcode Regular Expressions Basically we can do that with a Regex - actually even with the Regex provided by core by get_shortcode_regex(). First we need to grab the shortcode tags and build a regex. The core function get_shortcode_regex() sadly offers no chance to throw in an argument so we'd be left with a regex that matches each and every ...


4

I got curious about the SQL way of finding all posts without any attachments. Method #1 - Sub query with NOT IN Here's my first attempt to construct such a query: global $wpdb; $sql = " SELECT p1.ID, p1.post_title FROM {$wpdb->posts} p1 WHERE p1.post_type = 'post' AND p1.post_status = 'publish' ...


4

To add to the answer from @cristian.raiber, there is no function in Wordpress or php to detect screen sizes, and there will in all probabilty never be such functionality. Php runs server side, which runs before clients side which is browser side. As for php, and for that matter Wordpress, I cannot see any future implementation of any kind of function to ...


4

It appears that the templates live in script form <script type="text/html" id="tmpl-my-custom-gallery-setting"> To render the above template would require wp.media.template('my-custom-gallery-setting')(view) Since we're replacing the template: logic then all we need to do is store a list of template IDs. if (!wp.media.gallery.templates) wp.media....


4

Remove: $content = get_the_content(); and replace: <p><?php echo $content; ?></p> with: <?php the_content(); ?> See this note on the Codex page for get_the_content(): An important difference from the_content() is that get_the_content() does not pass the content through the 'the_content' filters. This means that ...


4

You need to get the metadata of each image, add this to your functions.php file: function get_post_gallery_images_with_info($postvar = NULL) { if(!isset($postvar)){ global $post; $postvar = $post;//if the param wasnt sent } $post_content = $postvar->post_content; preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids)...


4

This solution will add the data-fancybox="group" to gallery links produced by the default [gallery] shortcode. This has been tested and works regardless of whether themes have HTML5 theme support enabled for galleries or not. The solution works by using the post_gallery filter to gain access to the gallery shortcode's output. From there, the HTML is parsed ...


4

You adjust that when you insert the image into the post, not when you upload it. When you upload the image, you will be looking at it on the Media Library page. On the right hand side is a column that says "ATTACHMENT DETAILS". Scroll down on that, and look for "ATTACHMENT DISPLAY SETTINGS". The field you're looking for is called "Link To" and you want to ...


4

What you want to do is already baked into WordPress but just not frequently used. In either the classic or the block editor, click to add an image. Once you have the block upon, instead of uploading or pulling from existing images in your Media Library, click on Insert from URL. Now just paste the URL from wherever the image is being served. Just double ...


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