How to Acquire the URL of the Featured Image

Here’s yet another of those idiomatic manoevres you might want to perform in a WordPress template or plugin.

If you are in a situation where all you want is the URL of the featured image attached to a given post, there’s no direct WP function for it.

So, here’s code that works for this situation (should work for all WP versions since 2.7):

$featimgurl = "";
if (has_post_thumbnail( $post->ID ) ) {
$featimg = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$featimgurl = $featimg[0];
}

There’s a question of what to display if there is no featured image — that’s up to you. In my case, I wanted the variable to default to blank.

One sure route would be to include in your plugin package a small image to use as a default thumbnail, so you then do an “else” condition that assigns that image’s URL to $featimgurl.

Getting slightly more advanced, you could allow the admin to upload an image into the admin panel for your plugin for use as the default image.

23

01 2013

How to Add Links to the WordPress Plugins Page

Perhaps you’ve noticed that some plugins show up in the plugin install page with extra links beyond the typical “Activate” and “Delete”.

I wanted to know how to accomplish this in a hurry, so found this blog post which helped me out immediately.

Below is the method that works when your declarations occur *outside* of a class, so make sure you pay attention to the differences (and check the original post on the subject) to see how it’s done for a class.

1. Add a function that injects your link into the links array. The array_unshift adds your link to the front of the array. The $this_plugin wizardry is determining whether this instance of the filter is being called for your plugin or another one.
/**
* Add Settings link to plugins - code from GD Star Ratings
*/
function add_settings_link($links, $file) {
static $this_plugin;
if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);

if ($file == $this_plugin){
$settings_link = ''.__("Settings", "photosmash-galleries").'';
array_unshift($links, $settings_link);
}
return $links;
}

2. Add a filter for the plugin_action_links filter. When the Plugins page is displayed in Admin, this filter will pass your function the links array. Here’s the code:

add_filter('plugin_action_links', 'add_settings_link', 10, 2 );

That’s it! Remember to rename the “add_settings_link” function to something that will be unique in the WP namespace, e.g. a standard prefix you are using plugin-wide.

23

01 2013

Top 5 Tricks to Speed Up WordPress

Let’s face it: WordPress is not the most svelte of content management systems. But as we all know, it gets the job done.

And it’s not THAT slow, really. It’s just that it’s really easy to drag it down, especially in light of all the nifty bells and whistles you can throw in.

There are scores of ways to speed up WordPress, but many of them are highly situation-dependent. I want to focus on just the top 5 tweaks that will speed up WordPress page loads for EVERYBODY.

Tweak wp-config.php

Every time WordPress loads a page, it looks up the base URL of the website in the database. If I didn’t know WordPress so well, I’d be saying, “Are you fscking kidding me?”

But that means we can save a couple of database lookups, easy. Let’s add the following lines to the wp-config.php file:

define(‘WP_HOME’, ‘http://wordpressconsultant.net’);
define(‘WP_SITEURL’, ‘http://wordpressconsultant.net’);

How much this tweak speeds up your page loads will depend on the configuration of your server and database and what kind of strain they are commonly under. Dreamhost shared-host customers often see the greatest benefit here. (No offense, Dreamhost! You guys are great. Don’t kill my accounts. haha)

Use a Caching Plugin

This one trick can improve your page load time significantly (by how much will depend on your hosting provider), because it essentially lets the WordPress engine be a slacker: when a query comes in that exactly matches a page load already completed for a previous website viewer, the caching plugin will serve up a saved, static page instead of a dynamically generated page.

Plenty of decent caching plugins exist, but the one I’ve come to standardize on is W3 Total Cache.

Use of a caching plugin is also the #1 way to prevent site overloads during heavy bursts of traffic (we used to call it “getting slashdotted” back in the day).

Cut Back on Dynamic Page Elements

Pop Quiz, hotshot! Which do you think loads faster?
<?php bloginfo(name); ?><?php bloginfo(description); ?>

or

<title>The WordPress Consultant</title>

I hope you guessed the second one.

This tip regards the coding (or choice) of WordPress themes: wherever possible, replace dynamic lookups with static displays.

Do you really need to be able to set all of your META keywords in an admin interface in the WordPress Dashboard? If you think you do, consider that each one of those configurable elements kills another baby CPU cycle.  Think of the child processes!!

(I realize that many webmasters concerned about optimizing page titles for SEO purposes will want to dynamically set their title tags, so the above example was chosen for ease of illustration, and also to encourage tedious pedantry in the comments.)

Prune Plugins

Each time WordPress loads, it spins up each and every plugin marked as Activated in the Dashboard. If you have lots of fun “might use it someday” kinds of things hanging out in there — or worse, plugins you used to use but have let languish in obscurity — then it’s time for some spring cleaning.

Go through the list and Deactivate and/or Delete all the plugins you don’t use. You may be surprised at how well your site runs after this. Many older websites frequently see “gremlins” (unexplainable system behavior) disappearing after getting rid of unused plugins.

Look, it’s time for a little self-honesty. You’re never going to do a podcast. Or at least, you’re not going to do one this week, and having that plugin turned on is making your website LESS AWESOME. De-activate PodPress and get on with your life.

Work the Kinks Out of Your Database

You might be able to speed up your database queries by eliminating crufty old tables and bits of data no longer relevant to your interests. Get phpMyAdmin, which is not a WordPress tool per se, but can be used to “Repair” and “Optimize” your WordPress database. You can also use it to scan for tables related to plugins you no longer use so that you can delete them.

My Secret To-Do List

This is it! The above are the 5 operations I go through on every single WordPress site I install, fix, upgrade, and maintain.

According to Matt Mullenweg at WordCamp 2011 just this weekend, roughly 22% of new domains this past year were set up with WordPress on them (I think I was responsible for like 1% of them). You can help make the web faster by implementing the above 5 tricks to speed up WordPress.

15

08 2011

Squeeze Page Templates: Using WordPress for Squeeze Pages

WordPress is good for making squeeze pages (websites designed to encourage the extraction of the viewer’s email address and/or credit card numbers). You could set up static HTML to make your squeeze pages, but with WordPress’s plugin system you can get analytics, A/B testing, and other fun stuff up and running quickly. And whenever you need to modify your sales copy, there’s no need to dig through website code — you just edit the Page in WordPress.

Here are a few WordPress squeeze page templates I dug up around the web, along with how much they cost. Bonus: we learn a little about copywriting and keyword research along the way.

WordPress Squeeze Page Templates

Free WordPress Squeeze Page Theme Cost: your email address. (Of course!)

Notice they’re all named pretty much the same thing, because people who make squeeze pages are usually trying to claim search engine traffic for keyword phrases that, you know, actually get searched a lot.

WordPress Squeeze Pages Cost: $47

They all pretty much look the same, too, because they’re following the patterns laid down by guys like Frank Kern, Ed Dale, Mike Filsaime, and Joe Vitale, who have made millions with their copywriting.

WordPress Squeeze Page Cost: $10

The above will get the job done, I’m sure. But this one’s my favorite:

A humble but very good looking squeeze page template by CEA Web Solutions. Cost: Totally and completely free.

How to Run Squeeze Pages in WordPress

The disadvantage of using WordPress for squeeze pages is the performance hit. Each time the page loads, you’re running the whole WordPress engine to generate the page…

…unless you don’t! This is where a plugin like W3 Total Cache really shines. Since you’re just serving up the exact same page every time, with little to no dynamic content whatsoever, you’ll get all the benefits of WordPress with almost no downsides when you configure your squeeze page site to use W3 Total Cache.

But still make sure you install the site on a reasonably fast web host, one that can handle a lot of web traffic. If you’ve studied your target keyword traffic well, you should have a good grasp on the actual numbers you’ll need to handle.

10

08 2011

BoingBoing Goes WordPressLasix No Prescription Canada

The venerable pop culture blog Boing Boing, over 15 years old as a website, has just converted to WordPress (from MovableType). For those of us who have been watching the blog engine wars since pre-2000 (and written blog engines of our own), that’s a pretty swell endorsement!

The “new” website looks exactly like the website running its previous engine — which just goes to show, WordPress themes can easily encompass the feature set, graphics, and layout of just about any type of website.

Boing Boing Screenshot 7-27-2011

Only one apparent difference between the old and the new: threaded comments! No more in-line comments where replies have to reference “Commenter #5″. That’s a net win if you ask me.

27

07 2011

How to Bulletproof Your Blog

Socialmouths.com posted this list of great tips for how to keep your WordPress blog safe from such internet hazards as data corruption, malicious hackers, and the dreaded “oops I deleted it” error.

Go read them all, but here are a couple of really clever ones:

Automatic Database Backup

Here’s how you can have your WordPress blog’s database emailed to you on a regular basis. (Most people’s blog databases only take up a few megabytes.)

There are a few Wordpress Plugins that can perform a remote backup of your entire database.

The one I’ve been using for a while is called Remote Database Backup, it’s very easy to configure and it gives you very few options to worry about. You can manually run a backup at any moment and drop it in your hard drive or  you can schedule hourly, daily or weekly backups to be delivered via email as you can see in the following image.

Scheduling blog database backups

Set it up and forget about it! Even better, if you have an additional email account to manage your subscriptions and other services, have your backups go to that address, you don’t need to be looking at this stuff every day.

Automatic File Backups to Dropbox

You’ll need a (free) Dropbox account for this one. If you haven’t used Dropbox before, then you’re in for a treat. The service allows you to sync a few gigabytes for free (or hundreds with a paid account) across as many computers as you want.

Now there’s a WordPress plugin that lets you automatically backup the files that your website is made of to Dropbox.

Step 1: Install the Plugin WordPress Backup to Dropbox and activate

Step 2: Click “Authorize” to be redirected to your Dropbox account

Connect WordPress and Dropbox to run blog backups

Step 3: Allow. That’s it.

16

07 2011

Mobile Browser ID Strings

Zytrax.com keeps an astonishingly well maintained webpage listing every possible ID string sent by web browsers on mobile platforms. Everything from the most recent Internet Exploder string, iPhone, Android, all the way down to the now-defunct PocketLink for PalmOS. They got everything.

Tags:

13

07 2011

How to Create a WordPress Theme Demo Site

Sometimes you want to give a remote client a few different options for the same website, and you don’t want to do a lot of back-and-forth with them about it. Besides, some variations are super quick to make but much slower to explain. A demonstration is in order!

The goal is to allow the client to go to the site and sift through several different theme choices that you make available.

The first step is to set up a separate WordPress site for this purpose. You could use the client’s own website if it’s not yet “live”, but I would advise using one of your own properties, making sure to un-tick the checkbox for “Make my site searchable by Google, Technorati, etc.”

(By the way, setting up WordPress itselfLasix No Script is outside the scope of any of my articles; if you’re reading my blog, you’re probably pretty well beyond that.)

I have a “site layout demo” set up on a subdomain of wordpressconsultant.net (with its own WordPress installation, not a WP multisite sort of thing).

Next, install the themes you’ll be making available. If it’s variations on one theme, make sure they’re all different folders in the /wp-content/themes directory and that their meta-information in style.css contains different names from one another (otherwise your client’s going to be choosing from a maze of twisty themes, all named alike). Choose one to Activate, and then you’re done with that step.

Now we’re going to grab two excellent plugins that save us a hell of a lot of time.

The first is WP Example Content. This handy-dandy plugin allows you to deploy dummy content containing all of the typography gymnastics the client is likely to want to check out. Thoughtfully, the plugin gives you a button to instantly retract and make disappear these content-less posts and plugins at a moment’s notice.

The second plugin we want is Theme Switcher Reloaded, which provides a widget that creates a list of themes available for the website viewer to switch between.

The sad thing, though, is that when you load in a different theme, you lose your widget options and can no longer see the Theme Switcher Reloaded list in the new theme’s sidebar. Whoops!

Now you have two options. You could deploy the following code into every sidebar.php of every theme you’re displaying (changing html tags as appropriate):
<?php if (function_exists(‘wp_theme_switcher’)) { ?>
<h3><?php _e(‘Themes’); ?></h3>
<ul>
<?php wp_theme_switcher(‘dropdown’); ?>
</ul>
<?php } ?>

OR… You could create a Page that functions as a makeshift Theme Switcher:

Configure the widget on one of the themes and then go load the site in a browser. The list of theme choices will show up in the sidebar (like you told it to do!). Do a control-U to look at the HTML source of the resultant page, and look for the list of theme choices. You’re scouting for an unordered list that has a lot of the following string in its links:

?wptheme=

So now copy everything between the two UL tags, and abscond with it to the site’s dashboard, where you will create a new Page or Post. Paste the pilfered HTML in, hit publish, and voila! You’ve got a “theme switcher” that sticks around between themes.

I went one better and set this new Page to be the content that displays when the root site is loaded. That way it’s always obvious where to find the site layout options.

And that’s it! That’s basically all you need to do to set up a theme demo site within the span of 15 minutes.

Did you find this post useful? Have you done something like this before? Do you think you have a better idea, punk? Let me know!

25

06 2011

Search Engine Optimization is Trivial (Link Building is Not)

A very significant portion of the WordPress consulting I do revolves around getting the new website noticed by search engine users. So in my consulting, the topic of Search Engine Optimization (SEO) comes up often.

The common perception seems to be that SEO-ing a website is a difficult or tricky process, and that there’s an inordinate amount of stuff to learn to get it right.

Well, it’s not. SEO is trivial. Read the rest of this entry →

21

05 2011

Handy Tool to Generate Gradients in CSS

Sometimes you need a gradient in a place in your design that will be dynamically sized. Using an image for a gradient background in this case will result in something awkward looking.

Here’s a background gradient generator that produces pure CSS gradients, for your convenience!

I generated this lovely horizontal-gradient blue box generated using the above tool.

. . .

Compatibility

Chrome, Firefox, Safari: This code should generate gradients in webkit browsers with no problem.

Internet Explorer: This code probably won’t work on any Internet Explorer version prior to IE9… which as of this article, isn’t released yet.

Opera: Some developers have reported that replacing the -webkit- prefix with the “-o-” prefix allows this code to work in Opera. Others have reported that Opera now respects CSS that works in webkit browsers, so perhaps it’s now a nonissue.

25

04 2011