Skip to content

Instantly share code, notes, and snippets.

@deltafactory
Created October 2, 2013 04:57
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save deltafactory/6789297 to your computer and use it in GitHub Desktop.
Save deltafactory/6789297 to your computer and use it in GitHub Desktop.
Fix stored URLs for Lightbox assets in NextGEN Gallery 2.x. If you find that your lightbox images are missing or are pointing to the old location for the site, then this script will help.
<?php
/*
INSTRUCTIONS:
Use at your own risk. Backup your database before continuing.
1. Copy code to a file in the root of your website.
2. Change YOUR_SITE_ROOT to a value that makes sense.
3. Execute by visiting the page. Verify before/after results.
4. Change MAKE_CHANGES_TO_SITE to true and execute it again.
*/
/*** Change this to the URL of your Wordpress installation, without the trailing slash. ***/
define( 'YOUR_SITE_ROOT', 'http://yoursiteurl.com' );
/*** When the results have been confirmed, change this to true to modify data. ***/
define( 'MAKE_CHANGES_TO_SITE', false );
/*** No need to edit below here. ***/
require( 'wp-load.php' ); // Load the WordPress environment
header( 'Content-type: text/plain' ); // Set text output for easier debugging/readability.
fix_ngg_lightbox(); // Run the fix
function fix_ngg_lightbox() {
global $wpdb;
// Find the row in question, if it exists.
$post = $wpdb->get_row(
"SELECT ID, post_title, post_content
FROM $wpdb->posts
WHERE post_type='lightbox_library' AND post_title='lightbox'"
);
if ( !$post )
exit( 'Lightbox library post not found.' );
// Decode blob to proper object. Done twice to avoid shared reference.
$data_before = json_decode( base64_decode( $post->post_content ) );
$data = json_decode( base64_decode( $post->post_content ) );
$base_url = untrailingslashit( YOUR_SITE_ROOT );
$data->values->nextgen_lightbox_loading_img_url = $base_url . '/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-ico-loading.gif';
$data->values->nextgen_lightbox_close_btn_url = $base_url . '/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-btn-close.gif';
$data->values->nextgen_lightbox_btn_prev_url = $base_url . '/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-btn-prev.gif';
$data->values->nextgen_lightbox_btn_next_url = $base_url . '/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-btn-next.gif';
$data->values->nextgen_lightbox_blank_img_url = $base_url . '/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-blank.gif';
$post_content = $post_content_filtered = base64_encode( json_encode( $data ) );
echo "\n\n";
if ( MAKE_CHANGES_TO_SITE ) {
$wpdb->update( $wpdb->posts, compact( 'post_content', 'post_content_filtered' ), array( 'ID' => $post->ID ) );
echo '*** Done.';
} else {
echo '*** No data is being changed. Verify the results and change the constant "MAKE_CHANGES_TO_SITE" to "true" and re-run the script to commit.';
}
echo "\n\n";
echo "Before:\n";
print_r( $data_before );
echo "\nAfter:\n";
print_r( $data );
}
@sturman2
Copy link

Hi, I tried this, and I have 4 comments:

  1. I believe this line should also be included (right before $post_content = [....] ):
    $data->css_stylesheets = $base_url . '/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/jquery.lightbox-0.5.css';
  2. Although it works, it does not work when you set your lightbox effect to any of the other kinds other than 'lightbox':
    none, shutter, shutter2, thickbox, fancybox, highslide
  3. Undoubtably like many others, this issue has wasted much time for me. In the end I solved it by a combination of the above script and these instructions on http://wordpress.org/support/topic/moving-site-to-a-different-domain-but-next-gen-keeps-old-site-links ---
    Anyway, I just went into other options and in the Lightbox Tab I switched to every option (Fancybox, Thickbox, etc.) and clicked teh advanced settings, then it showed a textarea with the old url in it, just removed the http://domain.com part and voilá.
  4. I am not a great programmer, but at least I understand the need for simplicity. It is beyond me how people can make a great and popular plugin like this, and not simply read the base url from the wp options.

@Shumaher
Copy link

Shumaher commented Jan 5, 2014

thanks for this fix.
$base_url . can be removed (if WP installed NOT into subdir) to be domain independent

@gomarion
Copy link

Hello!

Your code works! But I found one more issue with NextGen Gallery—and that's when you use "Highslide" for the Lightbox effect. Apparently, it still contains the old URL. To fix this, there's just a little modification made to this code:

Line 33: WHERE post_type='lightbox_library' AND post_title='lightbox'"
Change to: WHERE post_type='lightbox_library' AND post_title='highslide'"

Remove Lines 44 to 48 and replace with:
$data->values->nextgen_highslide_graphics_dir = $base_url . '/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/highslide/graphics';

Just dropping this here in case somebody encounters the same problem.

@deltafactory
Copy link
Author

Thanks for the continued feedback guys. I'm sure it's helpful for others who discover this. It's too bad that Photocrati hasn't really felt it necessary to fix the problem within the plugin itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment