Covert all tables from MyISAM to InnoDB

Here’s a quick one liner to convert all tables on in a specified database from MyISAM to InnoDB:

mysql -Bse "SELECT CONCAT('ALTER TABLE ',table_schema,'.',table_name,' engine=InnoDB;') FROM information_schema.tables WHERE engine = 'MyISAM' and table_schema = 'your_database_name';" | xargs -I {} mysql -e {}

Boom! You’re welcome.

Hide WordPress Post from All Queries

Problem: you want to create a variation of a page but you don’t want it to show up on the home page or in any archives or anything. You just need a direct link so you can share it with someone.

Solution:

add_action('pre_get_posts', 'hide_hidden_posts');
function hide_hidden_posts($query) {
  if ( is_admin() ) {
    return $query;
  }

  if ( is_single() AND $query->is_main_query() ) {
    return $query;
  }
  $ids = wp_cache_get('hidden_posts', 'posts');
  if ( !$ids ) {
    global $wpdb;
    $ids = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = 'hide_post'");
    wp_cache_set('hidden_posts', $ids, 'posts');
  }
  $query->set('post__not_in', $ids);
  return $query;
}

This function will modify all WordPress’ frontend queries to exclude any posts with a custom field “hide_post”, except in the case that the query is the main query on a single post page.

Caveat: This will only be functional for plugins and themes using the WP_Query api. Custom queries will not be modified.

Use ZenHub.io to organize your github issues into a scum board

Just a quick endorsement. I’ve been very much enjoying using zenhub.io for work lately. I’m managing the development of “Terminus” which is Pantheon’s command line utility. Zenhub.io is a chrome extension that allows you turn the normal issue queue via into a pseudo scrum board.

zenhub.io turns your normal issue queue into a scrumboard

zenhub.io turns your normal issue queue into a scrumboard

The Simplest WordPress User Access Log Ever

Those of use who develop using pods often find we use it for everything. So here’s a quick tip on using PodsCMS to create a custom user access log.

Step 1: Set up the Pod

I’m assuming you’ve already installed/activate both the PodsCMS and Pods UI plugins. If not, please do so before starting.

Create a new Pod called “logins”.

By default each pod is created with a name and slug field. We’re going to use the name field but you can delete the slug field.

Then you’ll need to create a field for “date”. Of course, Pods stores the date any entry is created in a field called “created” which you can access from within Pods Templates. But it still makes sense to have a date field in the Pod itself, if nothing else for the sake of a clear data model.

So once you have added the date field your Pod will look like this:

Step 2: Add function

Now just add the following code to your functions.php file.

add_action('set_logged_in_cookie','mpv_add_access_log_entry');
function mpv_add_access_log_entry($user) {
$user = explode('|',$user);
$log = new PodAPI(); 
$params = array('datatype'=>'logins');
$params['columns'] = array('name'=>$user[0],'date'=>date('Y-m-d H:i:s'));
$log->save_pod_item(pods_sanitize($params));
}

This function uses the Pod API to insert a row in the logins table. Alternatively you can use the $wpdb class and do something like this:

global $wpdb;
$wpdb->insert($wpdb->prefix.'pod_tbl_logins',array('name'=> $user[0],'date'=> date('Y-m-d H:i:s')));

The only trouble with going this route is that in order to use the pods admin interface to manage the data, you’ll also need to add a row to the wp_pod table. This will change with Pods 2.0 so there’s no need to demonstrate. But I strongly recommend using the PodsAPI class as it will make sure to implement best practices and in 2.0 it will use the $wpdb class anyway.

So that’s it. To create an exportable report of the logins just install the “Exports and Reports” plugin. Or you can use PodsUI to create a custom interface.

Have fun.

Jmapping : Jquery plugin for Google Maps

Need to quickly integrate Google Maps with a series of posts? Try using JMapping. The easy part of this Jquery plugin is that you really don’t have to know much Jquery. Just loop through your posts with the right html syntax and POW! it happens. There are other jquery approaches that are more flexible … but they require considerably more knowledge.

Theming Custom Post Types in WordPress 3.0

Please note this post is way way out of date. WordPress has since integrated CPTs fully into the template engine so that you can simple create a single-posttype.php file. [See Template Heirarchy

So today I developed a simple strategy for dealing with some of the theming issues that arise out of WordPress 3.0 and the new Custom Post Type functionality.

The Issue: You’re working on a site designed long before Custom Post Types were an itch in Matt’s cerebellum. But now that the functionality is there and you want to incorporate it into your theme. Previously you used conditional code to theme your single.php file.

<?php if(in_category('foo')) { ?>
   Do something.
<?php } elseif(in_category('bar')) { ?>
   Do something else.
<?php } else { ?>
   Do yet another thing.
<?php } ?>

The thought of adding yet another layer of conditionals makes you sick to your stomach. Custom Post Types need to be treated completely different. They have different categories, taxonomies, and even different sidbars. Arg!

Solution: One simple function. Call it whatever you want, but here’s what it looks like. When I say simple … I mean simple.

function get_post_in_context() {
global $post;
$type = $post->post_type;
include(TEMPLATEPATH .'/layouts/single-'.$type .'-content.php');
}

Now just create a directory in your theme called “layouts”. Then copy and paste all the markup in your single.php file between the get_header() tag and the get_sidebar() tag to a file named single-post-content.php and save the file to your layouts folder.

Now place a the get_post_in_context() tag into your single.php  where you want the markup from single-post-content.php to show up.

<?php get_header(); ?>
<?php get_post_in_context(); ?>
 <?php get_sidebar(); ?>
<?php get_footer(); ?>

The beauty of this is that you can now create a new single-POSTTYPE-content.php (example:single-events-content.php) file for any of your custom post types and it will automagically get called instead of the single-post-content.php without any more changes to the single.php file.

I can’t take total credit for the idea. Some theme frameworks already employ a version of this strategy. But I need to adapt an existing theme rather than start from a framework. This function above will allow you to scale your existing theme to accomodate new post types without having to redesign your whole site.

How to title a blog post

Problogger had great article last week on how to title a blog post. Sure it’s a boring, mundane part of your site, not nearly as sexy as those moving images and flashy drop-down menus. But the title of a post could be the most important factor your are ignoring. Not only do post titles effect whether or not someone stays long enough to read the whole article. Post titles are key elements for Search Engines.

Problogger gives us the following eight tips:

  1. Communicate a  Benefit
  2. Create Controversy or Debate
  3. Ask a Question
  4. Personalize Titles
  5. Use Keywords
  6. Use Power Words
  7. Big Claims and Promises
  8. Humor Titles

Read his explanation for each.

Problogger: 10 tips for a great blog post

Problogger offers these ten things you should consider before writing a blog post:

  1. Choosing a Topic – take a little extra time defining your topic and the post will flow better and you’ll develop something that matters to readers.
  2. Your Post’s Title – perhaps the most crucial part of actually getting readers to start reading your post when they see it in an RSS reader or search engine results page.
  3. The Opening Line – first impressions matter. Once you’ve got someone past your post’s title your opening line draws them deeper into your post.
  4. Your ‘point/s’ – a post needs to have a point. If it’s just an intriguing title and opening you’ll get people to read – but if the post doesn’t ‘matter’ to them it’ll never get traction.
  5. Call to Action – driving readers to do something cements a post in their mind and helps them to apply it and helps you to make a deeper connection with them.
  6. Adding Depth – before publishing your post – ask yourself how you could add depth to it and make it even more useful and memorable to readers?
  7. Quality Control and Polishing – small mistakes can be barriers to engagement for some readers. Spending time fixing errors and making a post ‘look’ good can take it to the next level.
  8. Timing of Publishing Your Post – timing can be everything – strategic timing of posts can ensure the right people see it at the right time.
  9. Promotion – having hit publish – don’t just leave it to chance that your post will be read by people. Giving it a few strategic ‘nudges’ can increase the exposure it gets exponentially.
  10. Conversation – often the real action happens once your post is published and being interacted with by readers and other bloggers. Taking time to dialogue can be very fruitful.