2

I have existing wordpress gallery. When I add new images to it, they are added at the bottom of gallery automatically but I would like to have them at the top as first images in gallery. Of course I can drag and drop them to the top manually but if you have a large gallery and add about 30 new images it is not easy.

3
  • I tried solution posted on this question( wordpress.stackexchange.com/questions/157118/…) but it did'n work for me. Jan 8 '15 at 17:55
  • I mean what work have you done yourself? What code have you written and tested to try and fix this issue? Its a good question but you must work on it yourself and then ask for help when your solution doesn't work as per the guidelines wordpress.stackexchange.com/help Jan 9 '15 at 5:12
  • The thing is that I have made my own website and came across this problem stated in my question above. I have not written any code to fix this issue because I am not so advanced and don't know where should start or what code should I write. So if you asking what I have done myself, its 2 days research online with no luck, tried solution writen here: wordpress.stackexchange.com/questions/157118/… with no luck and I am stuck right no. And that is why I asked for help here. Jan 9 '15 at 10:25
0

Here's a version of the linked solution that overrides galleryAddToolbar to have an extra button (insertTop) to do what you want (in your "functions.php"):

add_filter( 'media_view_strings', function ( $strings, $post ) {
    $strings['addToGallery'] = __( 'Add to end of gallery' );
    $strings['addToGalleryTop'] = __( 'Add to start of gallery' );
    return $strings;
}, 10, 2 );

add_action( 'admin_footer' , function () {
    ?>
    <script>
    jQuery(document).ready(function() {
        (function ($) {
            if (typeof wp.media !== 'undefined') {
                wp.media.view.MediaFrame.Post.prototype.galleryAddToolbar = function () {
                    var media = wp.media, l10n = media.view.l10n;
                    this.toolbar.set( new media.view.Toolbar({
                        controller: this,
                        items: {
                            insert: {
                                style:    'secondary',
                                text:     l10n.addToGallery,
                                priority: 70,
                                requires: { selection: true },

                                /**
                                 * @fires wp.media.controller.State#reset
                                 */
                                click: function() {
                                    var controller = this.controller,
                                        state = controller.state(),
                                        edit = controller.state('gallery-edit');

                                    edit.get('library').add( state.get('selection').models );
                                    state.trigger('reset');
                                    controller.setState('gallery-edit');
                                }
                            },
                            insertTop: {
                                style:    'primary',
                                text:     l10n.addToGalleryTop,
                                priority: 80,
                                requires: { selection: true },

                                /**
                                 * @fires wp.media.controller.State#reset
                                 */
                                click: function() {
                                    var controller = this.controller,
                                        state = controller.state(),
                                        edit = controller.state('gallery-edit');

                                    edit.get('library').unshift( state.get('selection').models );
                                    state.trigger('reset');
                                    controller.setState('gallery-edit');
                                }
                            }
                        }
                    }) );
                };
            }
        })(jQuery);
    });
    </script>
    <?php
} );
1
  • Thanks a lot for solution! You saved my day!!! Works perfectly!!! Jan 14 '15 at 14:25
0

This helped me on a client project SO MUCH! But with the Wordpress 4.4 update, the "Add to Start of Gallery" button disappeared.

I found that the div the buttons go into was changed to 33% width after the update. You can change the width to 100% and the button will reappear. The class is ".media-modal-content .media-toolbar-primary.search-form" in the media-views-rtl.min.css which can be found in wp-includes>css

I hope this helps others who found the fix to be valuable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.