WordPress as a Service

At two recent WordPress conferences, WPCampus 2016 and WordCamp Boston 2016, I gave a talk along with Andrew Bauer on using WordPress as a Service (WPaaS) in a higher education setting.

In this talk, we covered the organizational and technical benefits of running a multi-network, multi-site WordPress as the primary CMS at Boston University. We highlighted specific strategies, including organizational policies, development workflows and team structure that allow us to serve over a million users per month. We’re here to help dispel the notion that a centralized approach to WordPress can’t successfully be implemented in higher ed.

Slides:

Talk:

WordPress failing to insert post into the database

I was recently trying to insert imported data into WordPress. When trying to insert it into WordPress using wp_insert_post, I was receiving the following error:

Could not insert post into the database

It turns out that I was trying to insert data in a non-utf8 encoding, whereas WordPress uses UTF8 internally. All I had to do was run the following PHP function to convert it over to UTF8:

$post_content = iconv('ISO-8859-1','UTF-8', $post_content)

PHP misreporting existence of directory or files

Recently I was working with a WordPress site where I was deleting a network. When the network was deleted, the root site was also deleted (or vice versa). And when the root site deleted, it deleted its own media directory and the parent network’s media directory as well. This worked out OK in most cases (because the root site and root network are one-in-the-same in a multisite network). However in some of my workplace’s custom code, we hooked into both network and site deletion. We tried to delete some extra folders attached with the network and site. Some folders were shared, so once they were deleted, they should’ve shown up as missing. However the folders were showing up as existing on second try and when deleting were giving out errors. So…

PHP has something called stat cache to make file operations faster. It caches information anytime you make a call to functions like “stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), and fileperms().” Coincidentally, PHP was misreporting the existence of files and folders when they were clearly deleted and glob() would be confirming that fact. In the end, we had to add calls to clearstatcache() to avoid this trap.

I wish there was a way to disable the stat caching for a session, because calling this function every time gets to be a pain.

Things learned from Wordcamp 2012

I attended most of the developer track presentations at Boston’s Wordcamp 2012. I learned about some interesting tools and techniques that I’d like to document for myself and others.

Talk Optimizing for Speed by Ben Metcalfe

  1. YouGetSignal’s Reverse IP Lookup – Shows you how many other hosts live on the same IP Address. This can be helpful for anyone using shared hosting or maybe VPS.
  2. Debug Bar – Get debugging information from each page WordPress page load.
  3. Google XML Sitemaps – Delete this plugin if you have it. (I had it.)
  4. YSlow – Get load times and optimization tips of a page request.

WordPress as a Web Framework by Sam Hotchkiss

  1. MVC frameworks for WordPress
    1. WP MVC – Provides a singleton object and eliminates the metadata bottleneck by providing tables indexed by post IDs.
    2. Tina MVC
  2. _s Theme – Use Automattic’s Blank Theme as a good starting point.

Automating frontend workflow by Aaron Jorbin (blog post, slides)

  1. Autojump – Jump to frequently used directories
  2. Commander.js (nodejs) – Script working with CLI
  3. watch (nodejs) – Watch files/dirs
  4. mockjax – Fake your ajax calls (good for protyping or testing un-related, yet dependent functionality)
  5. Travis CI – continuous integration service (wordpress plugin tests)
  6. Glue – generate css sprites

Microdata for SEO by Dave Ross

  1. Add itemprop, itemscope, etc to any identifiable schema
  2. Google Rich Snippets Tool – Test your microdata
  3. Examples: SiteNavigation element for navigation, Blog element for blog posts, etc
  4. Some quick tidbits: Bing rates sites with microdata higher than sites without, and Google uses the microdata in search results

Enterprise WordPress by Jake Goldman (1up)

  1. Sites to show clients: showcase, WordPress VIP
  2. Maintaining a beautiful WordPress admin

Shortcodes by Jon Bishop

  1. Use oembed rather than plugins to support embedding media from sites like youtube, facebook, etc

Codex by Erick Hitler

  1. Use santize_* functions to save to db: santize_text_field(), sanitize_title()
  2. Use esc_* functions to show data to user (esc_url_raw() is the exception, it is the opposite of esc_url())

Javascript hooks by Luke Gedeon (1up)

  1. Javascript custom events are coming, they will provide functionality similar to WordPress’ action and filter hooks (list of hooks)

WordPress: How to programmatically remove categories returned by get_the_category_list function?

First of all, I would recommend using the wp_list_categories function (which supports ‘exclude’ and ‘exclude_tree’ arguments) instead of get_the_category_list.

Sometimes we do not have the option of choosing which function to use. Also, it is unfortunate that the get_the_category_list function does not provide a hook to target each category in the list. So we have to use a hacky method to remove items. The following code uses regex to parse the string (list of HTML elements separated by a separator) being returned by get_the_category_list function (using ‘the_category’ filter) and removes the appropriate categories.

I would also like to note and the code below removes the “Uncategorized” category (which is the default category and the most common offender users want to remove) and also the “Feature” category (something I was using in my own code).

/**
 * Identifies the "Uncategorized", "Feature" link
 * preg_replace_callback for bu_library_hide_uncategorized function
 * 
 * @param array $regex_parts
 * @return string 
 */
function bu_library_hide_uncategorized_callback($regex_parts) {
	if( !$regex_parts or count((array)$regex_parts) != 2)
		return $regex_parts;
	
	if ( in_array($regex_parts[1], array('Uncategorized', 'Feature')) )
		return '';
	
	return $regex_parts[0];
}

/**
 * Removes the uncategorized category from $thelist string parameter
 * 
 * Ignore wp-admin requests. Unfortunately, 'the_category' filter is used in other places with 1 argument), so we must
 * make the last 2 arguments optional (or we get PHP Warnings) and quit when the 2nd argument is not supplied
 */
function bu_library_hide_uncategorized($thelist, $separator = '', $parents = '') {
	
	// short circuit for lists that do not have uncategorized category,
	// or when this function is called from wp-admin (i.e. missing separator)
	if(is_admin() or !$separator or stripos($thelist, 'Uncategorized') === false) return $thelist;
	
	$listitems = explode($separator, $thelist);
	
	$new_listitems = array();
	foreach($listitems as $item) {
		if ($new_item = preg_replace_callback('!<\s*a[^>]*>(.*?)<\s*/a[^>]*>!im', 'bu_library_hide_uncategorized_callback', $item)) {
			$new_listitems[] = $new_item;
		}
	}
	
	$thelist = implode($separator, $new_listitems);
	return $thelist;
}
add_filter('the_category', 'bu_library_hide_uncategorized', 10, 3);

Include admin scripts/styles in WordPress plugin

There are two ways of including custom scripts/styles in an admin page for a WordPress 3 plugin.

Solution 1 (Deprecated):


$slug = add_submenu_page('myplugin.php', 'My Plugin', 'My Plugin', 'upload_files', __FILE__, 'myplugin_options');

add_action('admin_print_scripts-'. $slug, 'myplugin_enqueue_admin_scripts');
add_action('admin_print_styles-'. $slug, 'myplugin_enqueue_admin_styles');

function myplugin_enqueue_admin_scripts($suffix) {
	$plugin_path = plugin_dir_url(__FILE__) . 'resources/';
	wp_enqueue_script('jquery.js', $plugin_path . 'jquery.pack.js');
}

function myplugin_enqueue_admin_styles($suffix) {
	$plugin_path = plugin_dir_url(__FILE__) . 'resources/';
	wp_enqueue_style('jquery.css', $plugin_path . 'jquery.css');
}

Solution 2 (recommended):

$slug = add_submenu_page('myplugin.php', 'My Plugin', 'My Plugin', 'upload_files', __FILE__, 'myplugin_options');

add_action('admin_enqueue_scripts', 'myplugin_enqueue_admin_scripts');


function myplugin_enqueue_admin_scripts($hook_suffix) {
	if ($hook_suffix == 'myplugin/slug') {
		$plugin_path = plugin_dir_url(__FILE__) . 'resources/';
		wp_enqueue_script('jquery.js', $plugin_path . 'jquery.pack.js');
		wp_enqueue_style('jquery.css', $plugin_path . 'jquery.css');
	}
}

Upgrading WPMU 2.x to WordPress 3.0: Multisite Problems

When upgrading WordPress MU installation to a WordPress 3 multisite installation, I faced a lot of “Internal Server Error” problems, such as:

Premature end of script headers: *.php
an error occurred while processing this directive

A lot of people suggested disabling plugins before, or even removing them all from the /wp-content/plugins/ folder. But one crucial detail was missing from most of these suggestions. WPMU keeps a special set plugins for itself in the folder /wp-content/mu-plugins/. It is very important to disable these, because chances are these are the most “incompatible” plugins that you have (because they are not updated like the regular WordPress plugins).