Quick and Dirty: Clone Custom Field, Template Linked Files on Movable Type

Movable Type is a pretty frustrating platform to work with because every so often (or, “way too often,” depending on who you ask) a function of the system simply doesn’t do what you’d expect it to do. Such is the case with the “Clone Blog” functionality. Although it dutifully copies most of a website from one “blog” object to another, a few things are missing.

Most notably, custom fields and templates’ linked files are not copied. This is a deal-breaker for any large installation that uses the built-in MT “Clone” feature.

To get around this limitation, I wrote a stupid, quick ‘n’ dirty PHP script to finish the cloning process, called finishclone.php. It takes only 1 argument: the “new ID” you are cloning to. If all goes well, you’ll see output like this:

[root@dev www]$ php finishclone.php 28
Cloning process complete.
[root@dev www]$ 

In this example, 28 is the newly created blog’s ID. The blog you want to clone from is set as a constant within the script. I’ll leave modifying the script to support more flexible command line arguments as an exercise for the reader.

 * Ease the final steps in cloning a Movable Type blog.
 * Description:   This script should be run after Movable Type's "Clone Blog"
 *                function has completed and before the cloned blog is used.
 * Author:        "Meitar Moscovitz" <meitar@maymay.net>

// Set constants.
define('MT_ORIG_BLOG', 0); // the ID of the blog you are cloning from
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'movabletype');
define('MYSQL_DB', 'movabletype');

// Get command line arguments.
if (2 > $_SERVER['argc']) { die('Tell me the ID of the blog to clone into.'); }

$blog_id = (int) $argv[1];

// Connect to db
if ( !mysql_pconnect( MYSQL_HOST, MYSQL_USER, MYSQL_PASS ) ) {
	die( 'Connection to the database has failed: ' . mysql_error( ) );
mysql_select_db( MYSQL_DB );

// Clone custom fields.
$result = mysql_query('SELECT * FROM mt_field WHERE field_blog_id='.MT_ORIG_BLOG.';');
while ($row = mysql_fetch_object($result)) {
		sprintf("INSERT INTO mt_field ("
			."field_type) "
	) OR print mysql_error() . "\n";

// Link template files to filesystem.
$arr = array();
$result = mysql_query('SELECT template_name,template_linked_file FROM mt_template WHERE template_blog_id='.MT_ORIG_BLOG.';');
while ($row = mysql_fetch_object($result)) {
	$arr[$row->template_name] = $row->template_linked_file;
foreach ($arr as $k => $v) {
	mysql_query("UPDATE mt_template SET template_linked_file='$v' WHERE template_blog_id=$blog_id AND template_name='$k';");

print "Cloning process complete.\n";

One reply on “Quick and Dirty: Clone Custom Field, Template Linked Files on Movable Type”

  1. Thanks for the tips. I’m seriously thinking about changing to WordPress though with most of my sites. I have a few websites still using Movable Type but I’m not sure if it is the best blog platform out there. A little bit difficult for me at least.

Comments are closed.