Flutter to Custom Post Types

So I’m working a project right now where I’m converting a WordPress installation from Flutter to the native WordPress custom post type functionality. The reasons for the switch are numerous. For one, it’s always good rule of thumb to use as few plugins as possible. The fewer plugins, the fewer php warning messages, the fewer javascript conflicts, the fewer hooks and filters getting processed on every load, and the fewer hits on the Database server. Secondly, flutter is particularly hard on the database, sometimes adding an extra 5 or more queries for every custom field.

The really great thing about flutter is that it still stores the field values in the  wp_postmeta table which means you don’t have to worry about any real data migration. So you have the luxury of taking it a step at a time.

The first step is to convert those key categories attached to “write panels” into post types. I did this by registering the post type as usual and simply running the following function on the init hook. This function performs a simple DB query to update posts with the specified cat slug into post_types.


function convert_cats_to_types($cat,$type) {
global $wpdb;
$query = $wpdb->prepare("
UPDATE wp_posts SET post_type = '$type' WHERE ID IN
(
SELECT DISTINCT object_id FROM wp_term_relationships rel
INNER JOIN wp_term_taxonomy AS tt ON tt.term_taxonomy_id = rel.term_taxonomy_id AND tt.taxonomy = 'category'
INNER JOIN wp_terms AS t ON t.term_id = tt.term_id AND t.slug = '$cat'
)
");
$results = $wpdb->query($query);
}

There’s no risk of losing data here, we are just changing where the posts in question are show up. The second thing to do is to replace the field-specific metaboxes. We’ll cover that in the next post.

Registration Forms: What’s New in 1.5

Last week I released version 1.5 of my Simplr Registration Forms plugin. The new version includes some big fixes and requested features. Particularly, this version now supports WP Multisite and has a few addition profile fields that can be added to the default form. It also includes better security, via WP nonces, and better field validation.

But the most important change is that it includes hooks and filters that allow it to be extended by you, the user.

For instance, let’s add a field to our form that requests the user’s zip code. First, in your functions.php file create a function for displaying the field:

function sample_zip_field($form) {
 $form .=  '<div>';
 $form .=  '<label for="zip">Zip Code:</label>';
 $form .=  '<input type="text" name="zip" value="'.$_POST['zip'] .'"/><br/>';
 $form .=  '</div>';
 return $form;
}

Note that this function receives the parameter $form and then returns $form. Failing to return the form will make the entire registration form disappear. To add this form to the registration use:

add_filter('simplr_add_form_fields', 'sample_zip_field');

But then we also need to make sure this data gets saved when the for gets saved. So you’ll need to create a function for that as well.

function sample_save_meta($user_id) {
if(isset($_POST['zip'])) {
 add_user_meta($user_id, 'user_zip', $_POST['zip']);
 }
return $user_id;
}

Note that in order for this function to work properly it has to receive the $user_id. It is also good practice to return the $user_id at the end of the function, though not necessary.

To make sure your save function is called use the hook:

add_action('simplr_profile_save_meta','sample_save_meta');

With these two “hooks”, you can customize the registration form however you want. You could even set up your field function to only display on certain pages, making it form-specific.

Finally, I’ve also added filters to the labels on the default form fields so you can change them at will. For instance, to change username to “screen name” use the following.

function sample_label_username($label) {
 $label = "Screen name: ";
 return $label;
}
add_filter('simplr_label_username','sample_label_username');

I hope you find the changes useful.

WordPress PodsCMS SEO Titles Plugin

I haven’t submit this plugin to the WordPress SVN yet, because I’m still playing with it. The remaining issue is that while it works, I’m not sure it works in the most efficient manner possible. I’m also going to post it over at @podscms and get some feedback.

At any rate, for anyone who’s running a pods installation and having trouble getting SEO friendly titles on your pod pages, this plugin creates a template tag <?php pods_seo_title(); ?> that you can use to override wordpress default behavior. The plugins check to see if the page is a pod page, and if so calls the title of the respective pod. If not, it checks to see if the page is singular and returns the TITLE | SITENAME format if it is. Otherwise it returns the SITENAME | SITE DESCRIPTION form.

Download: Pods SEO Titles