fbpx

Using NextGen Gallery with Bedrock

NextGen Gallery Other Options screen

Bedrock from Roots is a modern take on structuring a WordPress website. It leans on Composer to manage all third-party dependencies (WordPress core, plugins, and themes where possible), flexes PHP environment variables for configuring your site, and so ultimately results in a more robust setup that I’m more comfortable managing. Almost all WordPress projects at Sterner Stuff are built on it.

Of course, moving WordPress into its own directory isn’t expected by most plugins or themes. While Bedrock works seamlessly with most WordPress best practices, plugins that stray from those practices can end up with some hiccups.

The NextGen Gallery Problem

NextGen Gallery saves gallery file paths with wp-content appended, which is a problem, since Bedrock renames this to app. So we have to replace all those. The table isn’t registered with the $wpdb object, so it won’t get updated when you update post content, but there’s a better way anyway.

Next up, NextGen uses the WP_HOME constant when building the URLs for the gallery images. With Bedrock, storing WordPress in its own directory, this translates to https://example.org/wp. Nope. Really, the plugin should be using WP_SITEURL, but for legacy reasons, that isn’t the case.

You can find more details about these issues on the Roots forum.

Getting NextGen to Create Proper URLs for Galleries

To solve this, you’ll first want to set a new constant. Since you’re using Bedrock, open config/application.php and add the following line:

Config::define('NGG_GALLERY_ROOT_TYPE', 'content');

This tells NextGen to generate paths and URLs using WordPress’s content-directory-related constants. For example, the URL will now start with https://example.org/app. Good start.

Next up, if you go to Gallery -> Other Options in the admin, you’ll find the setting for “Where would you like galleries stored?” It’s probably set to something like wp-content/gallery/. You’ll want to drop this down to just gallery/.

Finally, we have to retroactively update that path for all the existing galleries. Like I said, a normal wp-cli¬†search-replace won’t catch it because it isn’t registered on the $wpdb object. You’ll need to tweak it a bit. Here’s a very specific version of the command that should get you going:

wp search-replace wp-content/gallery/ gallery/ wp_ngg_gallery --all-tables-with-prefix --include-columns=path

And that should do it! Your gallery images should all now show up just as they did before.


Are You a Do-It-Yourself WordPress-er?

If you aren't ready to invest in Sterner Stuff, I've got something for you! Check out my new (and free) WordPress for Small Business Owners email course!

Leave a reply

Your email address will not be published. Required fields are marked *