Override WordPress Plugin Options for All Sites in MultiSite

These days, enabling a plugin for all sites in WordPress MultiSite is a breeze… you just install the plugin files, then click “Network Activate” from any site’s plugins page.  However, configuring plugins on a site-wide basis is a different matter.  If you don’t want to use the default settings for a plugin, or if you want to be sure the settings to be the same for all sites in your network, you’re forced to go through one site at a time and change the settings manually.

I’d run into this problem before, and since I’m typically up against a mountain of work, I’ve never had time to solve it in an elegant way.  But with flexibility of WordPress, I was sure there was a hook or action somewhere that could help make this a bit easier.

Using the “pre_option_{option_name}” Filter

Turns out, it’s pretty easy to override options saved in the database.  Just use the pre_option_{option_name} filter or the option_{option_name} filter.  As I understand it, you should use the first if you simply want to replace the option’s value outright, and the second if you want to manipulate the value that’s saved in the database.  For example, if you wanted to change the value of the “blogname” option, you could add a filter to “pre_option_blogname”.

Gotcha: Limits of Filtering Options from Within A Theme

Typically, I put this kind of hack somewhere in my functions.php file in my theme (I’m using the same theme for all sites in the network).  However, after a bit of debugging, I realized that plugins are loaded before the theme files (makes sense), so if you set up a pre_option_ filter in your theme, you’ll filter the options in time for use in your theme itself, but not in time to see the values reflected in the admin pages.

To solve that, I figured I’d just try to add the filter earlier…

Solved: Override Options with “Must Use” Plugins

Basically, I just created a “must use” plugin — which is just a php file in the wp-content/mu-plugins folder that WordPress loads automatically.  I named the file to match the plugin whose settings I was overriding… i.e. if the original plugin was called “foobar”, I called my plugin “foobar-settings.php”.  From within that file, I added the “pre_option_” filters I wanted, and viola, everything worked as expected.

Of course, things being every more complex, I also had to override the creation of some custom terms by the original plugin — also accomplished from my mu-plugin using register_activation_hook.  And then I had to install another mu-plugin called Proper Network Activation to be sure the plugin activation hooks were fired for all blogs when the plugin is network-activated.  But that’s a story for another time.

Hello world!

Welcome to Alert My Banjos United. This is your first post. Edit or delete it, then start blogging!

HTML Tags: An Opposite to Emphasis?

I thought I’d write a quick post about something that always seems missing when I’m marking up content for a web project.  HTML provides us with plenty of tags that increase the importance/weight of text, but very few (if any) to decrease the weight.  If you want to emphasize text, you have a number of reasonable options:

  • <em> – meaning “emphasis”
  • <strong> – meaning “strong emphasis”
  • <b> and <i> – deprecated, but accomplish similar goals
  • <h1> … <h6> – headers, usually larger fonts
  • <big> – may mean size, may mean importance

But where do I turn if I’d like to deemphasize text.  There are lots of situations where I’d like to semantically define some content as less important than the surrounding content.  Typically, I just use a <small> tag, but I feel like that’s not ideal… I don’t necessarily want to make the text smaller.

Here are some ideas I’ve brainstormed… I’m looking for short words that won’t interfere with legibility of inline content:

  • <de> – meaning “depreciate” or “deemphasize”
  • <dim> – meaning “diminish”
  • <dp> – meaning “downplay”
  • <ig> – meaning “ignorable”
  • <less> – meaning “lessen” or “less important”
  • <min> – meaning “minimize”
  • <un> – meaning “unimportant” or “unnecessary” or “understate”
  • <weak> – the opposite of <strong>

What do you think?  Would there be any value to an HTML tag that allows us to semantically de-emphasize text?  Do you have any ideas

WordPress MU: Cross-Posting with "switch_to_blog()"

This weekend I tackled a MU template challenge that I’d been procrastinating on because I thought it would be really tough.  The problem, simply, was that I needed to display posts from one MU blog on another blog within my MU installation.

The answer: a handly pair of WordPress MU functions called switch_to_blog() and restore_current_blog().  Using these functions, you can jump around temporarily between MU blogs, allowing you to pull posts — or anything else, I presume — from wherever you like.  It’s as easy as:

<?php
// do some stuff on the current blog...
switch_to_blog(9);
// ...do some stuff on the other blog...
restore_current_blog();
// ...do some stuff back on the current blog
?>

Read on for more detail…

Searching for and Discovering WordPress Functions

When I am trying to learn something new about WordPress’s code, the first thing I do is open the whole WordPress directory the wp-includes directory (where most of the interesting functions live) as a Textmate project.  That lets me do two important things:

  1. Search through all the files at once, using Find In Project (Cmd + Shift + F…) — useful for following the labyrinth of function calls.
  2. Within a single file, use the function drop-down menu in the Textmate window footer — useful for quickly scanning all the function names.

To solve this problem, I had very little idea what to look for.  At first, I examined the get_posts() function in /wp-includes/post.php, thinking I’d be able to pass in a blog ID, and just get posts from a specific blog.  That yeilded some interesting information, but I couldn’t find any way to get another blog’s posts simply.  So I switched tracks and focused on the file /wp-includes/wpmu-functions.php, figuring there must be some hints amongst the MU-specific code.

Didn’t take much looking to discover switch_to_blog()… not exactly what I had been looking for, but I figured it was worth a shot.

An Example of Switching Blogs

After a little theme massaging, here’s what I ended up with, and it worked perfectly!

<?php

$other_blog_id = 1;

// get posts from current blog
$self_posts = get_posts(array(
    'numberposts' => 4
));

// switch blogs
switch_to_blog($other_blog_id);

// get some posts from the other blog
$other_posts = get_posts(array(
    'numberposts' => 4
));

// ok, we're done with that other blog
restore_current_blog();

// merge the two arrays
$posts = array_merge($self_posts, $other_posts);

// sort the entire array by date
function sort_posts_array_by_post_date($a, $b) {
    if ($a->post_date == $b->post_date)
        return 0;
    return $a->post_date < $b->post_date ? -1 : 1;
}
usort($posts, 'sort_posts_array_by_post_date');
?>

That left me with a sorted array of posts, from two separate blogs.  All I had to do then was loop through and output all the post content…

One More Gotcha… Switch Again For Post Template Functions Use get_blog_permalink()

Everything looked ok, but a little testing revealed that permalinks weren’t working… they were linking to the wrong URL.  So, I had to switch back to right blog for each post before using standard WP template functions, like this:

UPDATE: @Bira pointed out in the comments that there’s a function called get_blog_permalink() that seems to do exactly what I wanted… get the permalink to a post in another blog.  So I deleted the code sample below.  Instead of switching back-and-forth for each post, it would make sense to stash the blog id for posts when you retrieve them from the other blog, then use it when calling get_blog_permalink().

Installing Brother Printer Driver from OS X Install Discs

After a hard drive failure and replacement, I reinstalled OS X with “printer drivers” un-checked.  Of course it would be easy to get the ONE driver I needed later, right?  Wrong.  It wasn’t super-difficult, but I figured I’d save y’all the trouble of repeating the research…

How to Install the Brother MFC7820N Driver
For OS X users at IndyHall
The Abridged Instructions:

  1. Download this Brother Printer Driver:  Brother MFC-7820N.gz, and double-click to un-gzip it.
  2. Open System Preferences > Print & Fax.
  3. Add a printer (+), select Print Using: “Other…”, choose the file “Brother MFC-7820N”
  4. Click “OK”, print a test page… you’re done!

Director’s Cut:

Here’s what I had to do to gain access to the IndyHall printer… documenting this as I do it, and will summarize afterward.

  1. Google search: “os x install printer drivers from dvd”
    Result 1 looked promising:  Installing printer drivers only – OS X Server – MacTalk Forums
  2. Reply to forum question mentions “Pacifist” as software that will let you get the printer drivers off the install dics… but there’s no link.
  3. Google search: “os x pacifist”
    Result 1 wins again: CharlesSoft – software you always wished someone would write
  4. Downloaded and installed Pacifist, which lets you look inside the OS X install discs or installer packages.
  5. Ran the app, clicked on the “Open Apple Install Discs” button (duh) :)
  6. Searched down through the directories and found my Brother driver at:
    • Contents of OSInstall.mpkg
      • Contents of PrinterDriversGroup
        • Contents of BrotherPrinterDrivers.pkg
          • Library
            • Printers
              • PPDs
                • Contents
                  • Resources
                    • Brother MFC-7820N.gz
  7. Dragged-and-dropped that file to the desktop (had to enter admin password), then double-clicked the file to un-gzip it
  8. Opened OS X System Preferences > Print & Fax
  9. Clicked on “+” below left table to add a printer
  10. Selected the printer from the list of available printers
  11. In the “Print Using” drop-down menu, selected “Other…”, which opened a find-file sheet.
  12. Picked the brother driver from the desktop… but hesitated, wondering if I should store the printer driver elsewhere before doing this.  Asked Allum, who said to just go for it.  Clicked “OK”.
  13. Printed a test page with no problems… printer installed!

Google Analytics and its Tiny URLs

Google.  I love your products.  But please… usability!

As a user on several Google Analytics accounts encompasing many website profiles, it would be really helpful if I could tell — at a glance — which one I’m looking at.  Typically, one would do that with some type of contextual information — a header of sorts — that contains the title of the page.

The Google Analytics solution to the “what page am I on problem”?  A drop-down selection item.  Boo.

Move that stuff down.  Make it bigger!

google-analytics-title-fail