Create a WordPress admin user with MySQL

If you’ve ever been locked out of a WordPress installation, but have access to the database, here’s a nifty snippet to grant you administrator-level access. There are a couple of things you need to do before using this MySQL code. First, set the variables to your own information. Next, if your WordPress installation is based on a non-standard wp_ table prefix, you must find/replace ‘wp_’ with your current table prefix.

SET @id = 99;
SET @user = 'username';
SET @pass = 'password';
SET @email = 'email@ddress';
SET @name = 'Your Name';

INSERT INTO  wp_users (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name) VALUES (@id, @user, MD5(@pass), @name, @email, '', '2013-06-20 00:00:00', '', '0', @name);
INSERT INTO  wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES (NULL, @id, 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
INSERT INTO  wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES (NULL, @id, 'wp_user_level', '10');
INSERT INTO  wp_usermeta (umeta_id, user_id, meta_key, meta_value) VALUES (NULL, @id, 'active', '1');

Generate a custom _s (underscores) WordPress theme from command line

I’m building out a new theme for my blog using the _s theme. It’s an exercise in building out a responsive grid. _s is a great theme starter. Underscores.me has a nifty little tool to generate the starter theme with namespaced functions, title, description, etc… for the theme. I wanted to generate the theme directly from command line. Here’s what I used to generate the theme directory with one line (your pwd should be wp-content/themes/):

curl --data "underscoresme_generate=1&underscoresme_name=Coderrr&underscoresme_slug=coderrr&underscoresme_author=Brian+Fegter&underscoresme_author_uri=http%3A%2F%2Fcoderrr.com&underscoresme_description=A+custom+theme+for+coderrr.com." http://underscores.me >> coderrr.zip; unzip coderrr.zip; rm coderrr.zip;

There are four things that happen here:

  • Curl http://underscores.me and send post data
  • Tell Linux to place all returned data into a zip file
  • Unzip that file and create the theme directory
  • Remove the zip file

If you don’t want to format your URL encoded data, visit underscores.me and fill out the advanced form. You can extract your post data by using the Google Chrome Dev Tools network tab.

_s

WordPress Rewrites Without Duplicate Content

There are times when you need to surface content in a different context. For instance, you might have an events section and you want to have your related live-blog posts reside within the context of that specific event URI structure. The problem lies in surfacing the same content in the original location and in a rewritten location. Search engines frown on this. You can easily sidestep this and stay search engine friendly by setting up simple redirects.

Continue…

Send a WordPress Auth Cookie with HTTP API Requests

This snippet I created lets you hit your site with a remote request that includes your current auth cookie. I use this to circumvent caching plugins that use output buffering when I need to store a page load for analysis.

Testing Plugins and Themes on Multiple WordPress Versions

If you’re a theme or plugin developer, it’s essential to support at least WordPress 3.3+. Smaller sites and blogs have an easier time upgrading to the latest WP version. However, upgrading large corporate sites becomes a major operation because the codebase is usually collaborative and has many dependencies built on earlier versions. With that said, it’s good to be inclusive of those users as well by adding backwards compatibility.

I typically test my plugins on the major releases. If users have issues on minor releases, I’ll generate an install for that version on my local environment and debug from there.

On my local, I surface all WP versions under one domain, whatever.com. Each WP version is a subdomain. So, WP 3.3 is found at 3.3.whatever.com and so on. It’s easy to copy a vhost and a /etc/hosts entry for a new WP instance or use a software like MAMP Pro to do it for you. Another approach is to place each version in a subfolder off the domain docroot. You can find all of the WordPress versions at the core SVN repo. If you want to use GIT, you can find a synced SVN > GIT repo here. Simply fetch whatever branch corresponds to your desired version number.

To install my plugin, I checkout the trunk of my plugin into the plugins folder of each WP version. I use Versions App and title the bookmark as the version number. If you’re on Windows, you can use TortoiseSVN as well. I find SVN to be annoying on the command line, so I use a GUI. To note, the reason I use trunk checkouts and not symlinks for my plugin folders is because symlinks don’t play nice with plugins_url() from my experience. It’s a good practice to stay away from using constants directly to circumvent the symlinks issue and use API functions instead.

I keep all of tagged versions of WP in one database with different table prefixes. This keeps everything nice and tidy and in one place. For example, WP 3.3 becomes wp_33_ and WP 3.4.2 becomes wp_342_. Another benefit to this approach is you don’t have to create a new database each time you want to spawn a new WP instance. I also title each WP instance site title with the version number as another indicator of which version I’m developing.

Finally, I keep a copy of the latest nightly build to test for future compatibility as well. To get the latest nightly, simply do a SVN checkout of WP trunk.

Debug This – WordPress Plugin

I’m excited to release my latest plugin. It’s a great tool for developers to debug their plugins or themes. Debug This comes with 49 different debug modes to help expose specific raw data when you need it. The benefit of Debug This is the time saved from hardcoding var_dump or print_r to see WP globals, queried objects, etc… Developers can easily add new debug modes from their theme or plugin.

Debug This utilizes the WordPress Admin bar for mode navigation. All mode navigation links are relative to whatever page/post you are viewing. Custom modes and be nested in grouped drop-downs.

I really like the Debug Bar plugin and I’ve created several extensions for my personal use. However, there are times when I want to see the raw data in a full-screen width. Once you click a Debug This mode, it takes you to an immersive full-width experience.

Finally, DT renders the page first before displaying the debug mode. Custom debug extensions can analyze the rendered page with the supplied buffer in their callback function. This allows for intricate debugging that no other plugin offers (that I know of).

Update: If Debug This doesn’t suit your needs, I came across another great debug plugin called Debug Objects.

Debug This

WordPress Plugin8KDownloadsDownload Now »

Audit Post Attachments

One of the more annoying things about WordPress is the inability to see how many files are attached to a post without invoking the media upload area. Here’s a plugin I whipped up to report posts and their attachments by post type. Enjoy!

WordPress Remove Attachment Rows Where File Doesn’t Exist

Here’s a nifty script I cooked up tonight. This basically looks through all of your attachments and see if the associated real files exist on the server. If they don’t, the attachment database row is deleted. I realize this isn’t the most useful tool for everyday use. It was built for a one-off use case and I didn’t want this script to get dusty in my local files. So, here it is for your consumption or truncation!

Sublime Text WordPress Snippets and Auto-completions

I have become a huge fan of Sublime Text 2 as of late. It’s a simple, yet behemoth-featured editor that allows you to craft your own unique editing experiences. This post is not touting all the wonderful features about Sublime, but if you’re not using it you should be. It’s available for Mac, Windows and Linux.

I primarily develop websites based on WordPress, and I find myself frequenting the Codex on a daily basis to look up parameters that I haven’t memorized. Over time this can amount to a lot of time wasted. Fortunately for me and you, there is a Sublime WordPress package ready for you to install in mere seconds. This gives us a lot of WordPress goodness at our fingertips right in the editor.

To install through Package Manager, type CMD-SHFT-P (Macs) and ‘Type Package Control: Install Package’. Wait for the package search dialogue to appear then type ‘WordPress’ and hit enter. Boom. Done.

To take advantage of all the new WordPress goodness, simply type a WP-specific function and hit tab to auto-complete. Repeatedly hitting tab will move through the function parameters. There is also snippet goodness as well. You can take a look at all of the snippets and auto-completions here.

For instance if you would like to insert a plugin header, make sure you are in PHP syntax mode and type ‘plugin_head’ and then hit tab. In the CSS syntax mode, you can type ‘theme_head’ and move through all of the definitions with the tab key.

This is not even the tip of the tip of the tip of the iceberg with Sublime. Here’s a great course on what Sublime can do for you and your workflow.

If you would like to install the WordPress Sublime package manually, go to: https://github.com/purplefish32/sublime-text-2-wordpress and clone the repo to your package library.

WordPress Hooks and jQuery Custom Events

WordPress Hooks

If you develop with WordPress, you’re very familiar with the Plugins API. This allows you to ‘hook’ into most any part of the application and either filter data or run bits of code at a desired sequence point.

A sample action hook might look like this:

add_action('init', 'foobar_register_post_type', 0);
function foobar_register_post_type(){
    //Do something
}

A sample filter hook might look like this:

add_filter('query_vars', 'foobar_add_public_query_var', 0, 1);
function foobar_add_public_query_var($vars){
    $vars[] = 'foobar_var';
    return $vars;
}

There are hundreds of way to extend WordPress which is why it’s an amazing application upon which to develop. You can even create your own custom hooks using something like the following:

do_action('foobar_action');
$data = apply_filters('foobar_filter', $data);

This gives others access to extend your code with a couple lines of code in the right spots.

jQuery Custom Events

Many WordPress developers are building nice front-end experiences these days on jQuery (JavaScript). More specifically, plugins and themes are using AJAX instead of using native $_POST or $_GET behavior. The problem I’ve run into with several plugins is their lack of front-end extensibility.

On the server-side, the proper WordPress hooks usually exist, but there’s typically no way to extend specific events on the client-side. jQuery gives us the ability to easily follow the pattern of WordPress hooks.

A simple custom event might like this:

//This is similar to the WP function add_action();
jQuery(document).bind('foobar_custom_event', function(event, param1, param2){
    //Do something
});

The way to trigger a custom event is as follows:

//This is similar to the WP function do_action();
jQuery(document).trigger('foobar_custom_event', {arg1 : ['foo', 'bar', 'hello', 'world'], arg2 : 'Hello World' });

As you can see we trigger ‘foobar_custom_event’ and pass in arguments to the callback. If you build plugins or themes for WordPress, it’s as simple as that to make your client-side scripts as extensible as your server-side scripts.

An example use-case would be to allow other plugins to hook into a form submission success state to pass the submitted data onto a third-party API or post it their own registered ‘wp_ajax’ hooks.

There’s no need for building out a complex hook system in JavaScript. It’s already available to you. It’s up to you to make your application as open as possible to cultivate creativity around your codebase.