Convert myGallery to NextGen Gallery

For a lot of web logs, photo galleries are used. The web logs in this server used to run myGallery. However, this WordPress plugin wasn’t updated for a long time. (Recently there was an update for WordPress 3. I haven’t tested this one yet). So I needed a new gallery plugin. The NextGen Gallery plugin has all the requirements that I need. And best of all, it’s reasonably compatible with myGallery. So I decided to write a script that can convert all myGallery galleries to NextGen galleries. It will also update all the pages that display the gallery. Last, it will convert the thumbnail directory, so NextGen Gallery can use them. Of course, you can also let NextGen Gallery generate new thumbnails. But since my server isn’t that fast, using existing thumbnails is a good idea.

If you are running myGallery, and want to upgrade to NextGen Gallery and perform the conversion, this are the steps to perform. First, download NextGen Gallery. Disable myGallery, and enable NextGen Gallery. Next, copy and paste the script below in a PHP file. Modify the configuration and run the script. This will give a series of SQL statements. You can run these on the MySQL database. For example, you can use phpMyAdmin to run the queries easily.


// the prefix for your WordPress tables (default: wp_)
$WP_PREFIX = 'wp_';

// the directory on the server where your photos are stored
$GALLERYBASEPATH = 'wp-content/myfotos/daniel';

// username for the database
$USERNAME = 'username';

// password for the database
$PASSWORD = 'password';

// name of the database containing WordPress
$DATABASE = 'wordpress';

// host for the database (99% chance it will be localhost)
$HOST = 'localhost';


if (substr($GALLERYBASEPATH,-1)!='/' && $GALLERYBASEPATH!='')

$link = mysql_connect($HOST,$USERNAME,$PASSWORD) or die('Cannot connect to database '.$HOST);
$x = mysql_select_db($DATABASE,$link) or die('Cannot select database '.$DATABASE);

// convert the gallery
$result = mysql_query('SELECT * FROM '.$WP_PREFIX.'mygallery') or die('Cannot execure queries. Is the prefix correct?');
while ($row = mysql_fetch_assoc($result))
$row['longname'] = addslashes($row['longname']);
$row['galdescrip'] = addslashes($row['galdescrip']);
echo 'INSERT INTO '.$WP_PREFIX.'ngg_gallery (gid,name,path,title,galdesc,pageid,previewpic,author) VALUES ('.$row['id'].',\''.$row['name'].'\',\''.$GALLERYBASEPATH.$row['name'].'\',\''.$row['longname'].'\',\''.$row['galdescrip'].'\',\''.$row['pageid'].'\',\''.$row['previewpic'].'\',0);' . '

// convert the images
$result = mysql_query('SELECT * FROM '.$WP_PREFIX.'mypictures INNER JOIN '.$WP_PREFIX.'mygprelation ON '.$WP_PREFIX.''.$WP_PREFIX.' INNER JOIN '.$WP_PREFIX.'mygallery ON '.$WP_PREFIX.''.$WP_PREFIX.'mygprelation.gid');

while ($row = mysql_fetch_assoc($result))
$row['description'] = addslashes($row['description']);
echo 'INSERT INTO '.$WP_PREFIX.'ngg_pictures (pid,galleryid,filename,description) VALUES ('.$row['pid'].','.$row['gid'].',\''.$row['picturepath'].'\',\''.$row['description'].'\');' . '

// convert pages
$result = mysql_query('SELECT id,pageid FROM '.$WP_PREFIX.'mygallery');
while ($row = mysql_fetch_assoc($result))
echo 'UPDATE '.$WP_PREFIX.'posts SET post_content=\'[nggallery id='.$row['id'].']\' WHERE ID='.$row['pageid'] . ';

// convert page with master gallery
$result = mysql_query('SELECT * FROM '.$WP_PREFIX.'posts WHERE post_content=\'[mygallistgal]\'');
while ($row = mysql_fetch_assoc($result))
echo 'UPDATE '.$WP_PREFIX.'posts SET post_content=\'[album template=extend]\' WHERE ID='.$row['ID'] . ';

There are a number of caveats you should be aware of:

  • Gallery pages are updated, so they contain the new gallery tag. If there is any other text on a page, this text is lost
  • The page looks for the master gallery page (the page with links to all galleries. It will only find it if this page does not contain any other text. If the last code block does not give any results, you have to change this page manually

If you visit your gallery pages now, the should work, only the thumbnails are not displayed. To fix this, I’ve made a second script.

$prefix = '/home/www/wordpress_mu/wp-content/myfotos/daniel';
$dirs = scandir($prefix);

foreach ($dirs as $dirname) {
if ($dirname=='.' || $dirname=='..') continue;
$dir = $prefix.'/'.$dirname;
if (is_dir($dir)) {

if ($dh = opendir($dir)) {
echo $dir . " open

if (is_dir($dir.'/tumbs')) {
if ($dh2 = opendir($dir.'/tumbs')) {

while (($file = readdir($dh2)) !== false) {
if (substr($file,0,3)=='tmb') {
$newname = str_replace('tmb','thumbs',$file);
$r = rename($dir.'/tumbs/'.$file,$dir.'/tumbs/'.$newname);
echo "filename: $file to $newname : rename: $r
// rename tumbs dir
$r = rename($dir.'/tumbs',$dir.'/thumbs');
echo $dir . " rename " .$r."


Just change the prefix variable, to point to the path where all galleries are located. This script will rename all thumbnail directories immediately.

9 thoughts on “Convert myGallery to NextGen Gallery

    1. Sure, you can quote this post. All posts are available under a creative commons licence (see bottom of each page for licence details).

  1. Hello!
    I found your script to update to the nextgen gallery! That´s great but my question is – you write “If there is any other text on a page, this text is lost” – does this mean all posts with text are deleted after the update?
    Thank´s for your help,
    kind regards,

    1. Hi Markus,

      No, not all post with text are lost, just pages with a gallery on it AND if that page is linked to the gallery in MyGallary. Say you have the following as a page content:

      Look at my gallery!
      [mygallery id=3]

      (or something like that, can’t remember the exact code to display gallery with myGallery).
      My scripts update that page, so the new content would be:

      [nggallery id=3]

      So, the text “Look at my gallery!” would be lost. If you don’t want that, remove the code after “// convert pages”

  2. Hey Daniel,

    unfortunatelly, I have currently a blog with myGallery-Gallerys. Could you please help me to adapt your scripts. I woud like convert the myGallerys to the current version of NextGen Gallery.

    Thank you

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.