• Is there a function that can do this? I have many galleries, and some were created as media, some as attachments, some as ‘none’ for when the page visitor clicks an image.

    I’d like all post galleries to act as though they were all set to open as ‘media’

    Is this possible? A kind of overwrite function?

Viewing 8 replies - 1 through 8 (of 8 total)
  • I’m not sure what you’re asking? Like a popup?

    Thread Starter ordresser

    (@ordresser)

    I have an existing WordPress site, and it has galleries on several posts. Some of these galleries are set to ‘media,’ others to ‘attachment’ etc, so when a visitor clicks an image (the gallery thumbnail), it opens as that sort of link (a media file or an attachment). I want every gallery on my website to open as ‘media’ and none to open as attachment, using a function.

    Moderator bcworkz

    (@bcworkz)

    You can override the gallery output and have the [gallery] shortcode output content in any form that you like. Use the ‘post_gallery’ filter for this. Your filter callback is fully responsible for creating all output. You could copy the significant portion of the gallery_shortcode() source and alter it to only output media file links regardless of the link attribute.

    I’d think it would be easier to search/replace all the shortcode link attributes, but that’s just me ??

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Moved to Fixing WordPress, this is not a Developing with WordPress topic.

    Thread Starter ordresser

    (@ordresser)

    Could you give an example of how that would work? I’ve done some searching and not found anything I can use or modify.

    Moderator bcworkz

    (@bcworkz)

    Well, the basic setup is all I can show you, but it may not be very helpful. It’s a start anyway.

    add_filter( 'post_gallery', 'my_get_gallery_shortcode', 10, 2 );
    function my_get_gallery_shortcode( $output, $attr ) {
      // insert modified source for gallery_shortcode() here
    }

    You must remove the “post_gallery” filter application from your version of source or you will invoke an infinite loop. Edit the applicable $atts['link'] portion to achieve your desired effect. The source is in wp-includes/media.php starting around line 1631. The link attribute portion is around line 1776. There may be other portions you don’t need, but regardless, you end up with a lot of code. Producing a somewhat flexible gallery output takes some doing.

    Thread Starter ordresser

    (@ordresser)

    Wow. OK, so basically I can’t just add a function that overrides whatever the posts’ image/gallery links are already and replace any that have “attachment” set so that they become “media”?

    If I’m editing many files in source, like /media, will that pose a great risk of WordPress erros (breaking things) when my hosting provider automatically force updates my WordPress?

    Moderator bcworkz

    (@bcworkz)

    That’s right. Unfortunately, the gallery filter is not like most where you could search/replace small elements of output. Output is all on you or all on WP, nothing in between.

    There is an alternative if your gallery shortcode occurs in post content. You could hook “the_content” filter late, so that shortcodes have already been expanded, and search/replace the content. Typically, the gallery shortcode output is the majority, if not all, of content, so filtering content is little different from filtering gallery output, if it were in fact even possible.

    There’s no risk of breaking things within WP itself after an update if you were to modify core files. This is because whatever you changed will be obliterated and reverted to the current version. However, any code in a plugin or theme that is dependent on your modification will be broken. The real risk in changing core files is people become reluctant to update because going back and reinserting their changes becomes a big PITA. Minor security updates can come rather frequently and they are the ones we should not delay in applying. Delaying updates introduces real security risks. What was once unknown becomes very well known, often with related exploits appearing in the wild.

    You may tell yourself you’re willing to keep up with updates and religiously reapply your changes, but that’s not going to last. Been there, done that. (altered plugin code, not core) Of course, if your host is pushing updates, you have no choice in the matter. Altering core code is still not worth doing. It’s better to replicate 100 lines from core in your plugin than change 1 line in core itself.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How can you make all galleries on entire website open as media?’ is closed to new replies.