Adding a custom meta box in WordPress

Written by Keith on the 4th May, 2011. Filed under: Tips & Tricks, WordPress

In this little tutorial, we will be creating a simple meta box in WordPress that will be displayed when we are writing a new post. The meta box we will create is very basic and will only print some text but this will get you comfortable with using them.

First off, open up your functions.php file and add the following code.

add_action('add_meta_boxes', 'eirestudio_create_meta_box');
function eirestudio_create_meta_box()
{
   add_meta_box('eirestudio_meta_wrapper', 'My Super Meta Box', 'eirestudio_display_meta_box', 'post', 'side' ,'default', '');
}

function eirestudio_display_meta_box()
{
   echo 'Eire Studio welcomes you!';
}

Heres how it works

The add_action function attaches a custom function (which we have to create) to an action; the action we are using in this tutorial is the add_meta_boxes action. The add_action function accepts 4 parameters:

  • tag (required) - The name of the action you wish to hook onto. A list of available actions are here.
  • function_to_add (required) - This is name of your function you want to be used.
  • priority - This determines when your function is called in relation to other functions. The default setting is 10.
  • accepted_args - How many arguments your function takes.

The eirestudio_create_meta_box function is a function we will use to create the meta box by using the add_meta_box action. The add_meta_box action defines the meta box such as the heading, where it will be shown on the page etc. The add_meta_box action accepts 7 parameters:

  • id (required) = The CSS id that your meta box will be wrapped in.
  • title (required) = The heading of your meta box.
  • callback (required) = The function name to be called to actually show the meta box.
  • page (required) = What screen will this meta box be shown on. Post, page, custom post type or link.
  • context = Which part of the screen will the meta box be shown.
    • normal
    • advanced
    • side
  • priority = The priority in which your meta box should be shown. high, core, default or low.
  • callback_args = Arguments to pass into your callback function.

Then we simply create the eirestudio_display_meta_box function and pop in some content :)

WordPress to WPMU Conversion

Written by Keith on the 14th February, 2010. Filed under: Tips & Tricks

So I just finished installing and converting this site to WPMU, mainly to test it out and delve deeper into wordpress.

The migration from the old site to the new, went remarkably smooth and the site was only down for about 5 minutes which I class as a big success (Also, I’m sure my method is not the most elegant). Because of the fact that WordPress and WMPU share 99% of the same codebase, the conversion was painless.

I am going to briefly explain how I did it.

Step 1 – Backup

Backup, Backup, Backup!

Using ftp, everything was backed up, as in the complete installation and all sub directories of the existing wordpress installation. Then I dumped the entire mysql database for the site. Now if we bodge it up, it doesn’t really matter, we have the most important thing, our data.

Step 2 – Download

Download WPMU and overwrite all existing wordpress files and folders. Now delete the .htaccess and wp-config.php files. Pop over into the browser and refresh your site. You should now see an install screen. (Note: my site is for personal use and gets only a few visitors, so If your site is big and gets alot of traffic, put up a blank index.php file to protect yourself against malicious users.) Fill in your database details as per your original wordrpess installation and log in. You should now have a working install of WMPU.

Step 3 – Migrate

So, we now have a spanky new WPMU install, pretty cool huh? Our next step is to migrate all of our old content into WMPU. While having a peek through PHPmyadmin, I see some new tables created by WMPU: wp_1_ etc. Keep a note of the these.

Now I select the main tables which are below and export only these tables. Also, select the ‘Add DROP TABLE’ checkbox.

  • wp_comments
  • wp_commentmeta
  • wp_links
  • wp_postmeta
  • wp_posts
  • wp_terms
  • wp_term_relationships
  • wp_term_taxonomy

With the sql file, I open this in a text editor and begin to replace the old table names with the new ones.

For example. When fixing the wp_posts table for use with WPMU, I simply do a find and replace like so:

Find wp_posts Replace wp_1_posts and do this for all of the 8 tables above.

Step 4 – Patch

WordPress MU adds a ‘/blog’ to your posts and removes the www from the url by default. If you have some content indexed in Google and don’t want to lose any traffic, there are 2 optional steps left.

Download and activate this plugin.

And add this to your .htaccess file to convert www traffic to the new non www url.

RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule (.*) http://example.com/$1 [R=301,L]

Now, you are done and have a WordPress Mu installation, congrats!

Conclusion

As mentioned at the start of this post, this is most likely not the most elegant solution but It worked absolutely fine for me and should work for you too. Also, in regards to the media files, you can replace these permalinks in mysql and point them to the new files/ folder, but as this is a small site, I’m fine with my images pointing to my old wp_content folder.