Wednesday, February 9, 2011

WordPress plugin - Domains Update

I dared a few days ago to share a plugin I made with my work mates. It took me about 10 days to decide to do it (the sharing part). Last night my boss called me to ask me how to use it and in the end his conclusion was that it was not doing what I said it would...but, as usual, I had the "revelation" in the morning - meaning like 10 minutes ago.

So, I decided to put it here and see what kind of problems other people might have with it(omg...I am so evil).

What I pretend this plugin to do: updates values in database.

When you should use it: when you move your Wordpress site from one domain to another and you want to keep the database consistent.

Let's imagine the scenario when you move your Wordpress site from location A to B.

Options description:
1. Old domain - in our case would be A
2. New domain - in our case would be B
3. URL format - uncheck it if any of the domains does not have an URL format (e.g. localhost)
4. Select custom table(s) - when you press this button a list of the Wordpress' database tables is shown. When you select a table, a list of its columns is shown. Use this to update the values of old to new domain in other tables than wp_posts and other columns than wp_posts.guid and wp_posts.post_content (these are the default location of the update).
5.Update only custom tables - if this option is selected the default locations are skipped.
6. Run changes - runs the database updates.

Note 1: you can run this plugin multiple times.
Note 2: if you need to make updates in custom tables make sure you selected at least one table and for each table at least one column (otherwise it will tell you that there was no data to update).
Note 3: you might wonder why the plugin's name is Domains Switcher and in administrator's dashboard the name is Update Domains. Because I like to screw users' mind.

One more thing. The steps I follow when I need to move a Wordpress site from domain A to domain B and on domain A there is already content that needs to pe kept on domain B:
  1. Copy all needed files from A to B
  2. Export-import database from A to B
  3. On B, go to database and in table wp_options where column option_name has value siteurl and home the column option_value should have value A. Change it to B.
  4. On B, open file wp_config.php and update the information for database connection on B
  5. Enter wordpress admin on B (now you should be able to do that) and install and activate the Domains Switcher aka Update Domains plugin
  6. If all you need to update are posts and pages, run it with simplest configuration (without custom tables). The files uploaded and inserted into posts now will be taken from B, instead of A.
  7. If you used plugins which create their own tables, run the plugin against tables and columns that you know might contain URLs (e.g. for a slideshow plugin, it might keep in a custom table the links to images it uses)
  8. Sometimes plugins which do not create their own tables might insert links in column wp_postmeta.meta_value

You can download the plugin from here.

Known issue: Don't use it to update the information in cformsII plugin. It will alter the serialized string containing the cforms settings (and the plugin will stop from working). If you need to move cformsII to another domain, my suggestion is to install the plugin to the new locationa and backup-restore the forms from the old domain to the new one. If you have more than one form created with cformsII, just add new forms and backup-restore them. The global forms settings need to be update "manually" though.

Tuesday, February 8, 2011

How to add different custom content on WordPress pages

In the last few months I had to work on different WordPress sites which required different content on different pages. That content was supposed to be manageable from WordPress administrator account. As I was new to WordPress I decided to create a plugin each time I needed manageable content to appear on different pages. This morning I just figured out how to take advantage of some already made WordPress plugins which would make this task much easier and much efficient, from my point of view.

Let's take the example of adding different Sidebars on each page of a site.
1. The plugin I am using is Custom Post Type UI. After installing and activating it I define my custom type Sidebars. From plugin's Advanced Options I choose it to support Title, Editor and Custom Fields. I also define a taxonomy for this custom type, called Page Sidebars and I attach it to Post Type pages.

2. Now in Dashboard I have a new option to administrate the custom type I just defined. So all I have to do is to go to Sidebars and start adding new content of this type.

3. When I insert new pages in the left of the editing window I can add the custom types by their title.

4. Now the code I use in sidebar.php from my theme to get for each page the content of each custom type sidebar related to each page.
/* get the global post variable to know on what page you are on */
global $post;
/* for the given page, get the taxonomies of type page_sidebars */
$data          = get_the_terms($post->ID, 'page_sidebars');
$my_sidebars   = array();
foreach($data as $sidebar):
/* get the post of type sidebars with the name of the page_sidebars taxonomy */
$my_sidebars[] = get_post_by_name($sidebar->slug, 'sidebars');

In functions.php of your theme define
function get_post_by_name($page_name, $post_type = 'post', $output = OBJECT) {
   global $wpdb;
   $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_name, $post_type ));
   if ( $post )
      return get_post($post, $output);

   return null;
Note: If you need new fields for your custom types, there is another plugin easy to use and integrate with Custom Types UI: Custom Field Template