Skip to navigation | Skip to content



Blog

Get a number of posts with optional sticky posts at the top

I’ve not really used sticky posts in WordPress. I’ve just been hacking around to try and use this system instead of my own custom field checkbox for “featuring” posts. There’s some definite pros: the user can toggle the status via the Quick Edit feature instead of having to edit the whole post. There’s also some cons: the sticky posts system has some quirks which, while understandable, limit it slightly.

One niggle is that sticky posts in standard post loops only really impact things (i.e. bubble to the top) when is_home() is true. The real killer is that when you’re only outputting a certain number of posts, using, say, get_posts and the numberposts argument, if there are sticky posts, you’ll get the sticky posts plus the number of posts you specify from the rest of the posts. So if you ask for 3 posts and there’s 1 sticky post, you’ll get 4 posts in total. Grrr!

This is acknowledged in a couple of trac tickets. There are reasons for this—it’s a feature, not a bug, as they say—but what do you do if want to do things your way?

Read more »

Using WordPress 3.0 custom menus for custom stuff

The introduction of custom menus in WordPress 3.0 was very welcome. They can be a bit baffling at first (see the ever-reliable Justin Tadlock’s guide if you’re confused), but they’re a great CMS addition to WP.

Here I want to detail a couple of tips for going beyond the standard usage.

Read more »

Customize the WordPress admin menus

I’ve recently been looking at de-cluttering the WordPress admin interface. There’s some good plugins for this, such as Adminimize, WP-CMS Post Control (for hiding boxes on the post screen, according to user role) and White Label CMS (for extensive customization of logos, menus, etc.).

As ever, I’m also interested in getting just a particular aspect working with a bit of theme code—if only one aspect is needed, it’s sometimes nice to drop it into the theme and have one less plugin loading. The aspect I’m thinking of here is removing a few menu items.

So, with the usual caveat that this is code for theme developers, to be dropped into the active theme’s functions.php file, and a nod to hungred.com for initial inspiration, here goes.

Read more »

WordCamp UK 2010

I’m just back from WordCamp UK 2010 in Manchester. Another inspiring gathering of WordPress people, from the curious to the obsessed, with a fantastic, vibrant city as the backdrop.

I’m too exhausted now for a detailed write-up, but I want to jot some things down while they’re fresh. Here’s the main things that stick in my mind…

Read more »

Solving WordPress problems

I periodically get queries about WordPress issues through this site, often in relation to code I’ve posted here. These days I’m usually far too busy to go into deep, free coding consultation; and usually when I do help out I’m just Googling and using the same resources available to everyone.

I totally appreciate that I might have much more experience than some people, and I’ll have a few little hints that could save a lot of time. But sometimes people emailing me seem to be doing something that I fully confess to being guilty of, in the past and even now: shouting out for help too soon instead of stepping back and tackling the problem properly.

So, here’s a little guide I can point people to, partly in lieu of an email reply template. If you’re facing a WordPress coding problem that either looks daunting right from the start, or is driving you insane after hours of trying, the following hints can help a lot.

Read more »

Enforce strong WordPress passwords

Here we go with some more nifty code for you WordPress developers… As ever, this code is roughly tested but probably not for novices. It’s designed to drop into a custom theme’s functions.php file. It probably should be a plugin, and it might make it one day when it’s thoroughly tested and I get time…

Anyway, it’s a solution to a problem that I’m very surprised isn’t built into the WP core (as an option at least), and isn’t addressed by any easily found plugin or code already out there. As we know, WP provides a good “password strength meter” on the user profile page, which is great as strong passwords are (or should be) one of the first lines of defence against attacks on your site. But it’s just an indicator—there’s nothing stopping someone using “password” as their password, or something dumb like that. All you need is one Administrator or Editor with a dumb password, and the whole site is highly vulnerable.

How about a little enforcement?

Read more »

Compare the WordPress post date

I find PHP’s date handling a bit confusing. It’s easy when you know how, of course; but even at the same time as appreciating that it’s good in the end that it offers a lot of flexibility, it’s sometimes seemed like there are too many hoops to jump through to do some simple date operations.

Anyway, I’ve just cracked a minor issue I’m working on thanks to this post and this thread. I’m posting the solution here for my quick reference as much as anything…

Problem: how to check if a WordPress post_date (from a $post object) is from a day in the future? Solution:

$postDate = strtotime( $post->post_date );
$todaysDate = strtotime( date( 'Y-m-d' ) );
if ( $postDate > $todaysDate ) {
	[ post is from the future! ]
} else {
	[ post is from today or the past]
}

Adding scripts to the WordPress login or registration form

Most WP developers know by now that the right way to add script libraries from a plugin or theme is to use the wp_enqueue_script function. It makes sure scripts aren’t loaded more than once, and so on.

However, I just had some problems getting some jQuery to run on the login / registration form. I used wp_enqueue_script like a good WP developer but… no go. Where’s my jQuery?

It seems the answer lies in the fact that after scripts are enqueued in a normal WP page, they get spit out by something inside the call to wp_head(). I guess you could add wp_head() to the login header, but who wants all those redundant plugin scripts and styles that (presumably) aren’t even meant to be there.

After a bit of digging around it seems that the solution is the wp_print_scripts() function. So, to get jQuery working on the login form, put these lines in the code you hook onto login_head:

wp_enqueue_script( 'jquery' );
wp_print_scripts();

Easy child page creation for WordPress

Here’s a quickie that can help with WordPress sites that have a lot of pages. If some of the pages have longish titles, the “Parent Page” drop-down on the edit page screen can get unwieldy. The scroller for the drop-down can run off the right side of the screen.

OK, it’s fine (often easier) to use the keyboard to navigate drop-downs. But not everyone knows how to, and even so, with a large amount of pages, selecting the right parent can become a pain.

I think I adapted the following code from a plugin or someone else’s post. Thanks whoever you are, but frankly I’m so busy at the moment I’ve forgotten where it came from! Anyway, if you drop the following into your custom theme’s functions.php, you’ll get a new “Create child” link under every page listed in WP admin, when you hover over the title:

function slt_childPageAction( $actions, $page ) {
	$actions["create-child"] = '<a href="/wp-admin/page-new.php?parent_id=' . $page->ID . '" title="Create a new page with this page as its parent">Create child</a>';
	return $actions;
}
add_filter( 'page_row_actions', 'slt_childPageAction', 10, 2 );
function slt_setChildPage() {
	global $post;
	if ( $post->post_type == "page" && $post->post_parent == 0 && isset( $_GET["parent_id"] ) && is_numeric( $_GET["parent_id"] ) )
		echo '<script type="text/javascript">jQuery( document ).ready( function($) { $("#parent_id").val("' . $_GET["parent_id"] . '"); } );</script>';
}
add_action( 'edit_page_form', 'slt_setChildPage' );

The first bit adds the page action link. The second bit adds some jQuery to the right edit screen to set the specified parent, if it’s been passed. I couldn’t find another way to set the drop-down… Well, it works!

Formatting a string with optional elements

I’ve often found myself writing little bits of code to format strings with optional elements. The classic example is outputting the company someone works for, together with their position in that company—if the position is given. The format if both are given is:

[position], [company]

If the position isn’t given, you just output the company. Without, obviously, the comma. It’s a trivial thing to code, but on a current project I’ve found myself doing it in several different places. Of course, this suggests there should be a function written for it.

Ideally the function should be able to cope with any number of optional elements. With PHP’s func_get_args(), it’s easy:

function formatOptionalText( $delimiter ) {
	$elements = array_slice( func_get_args(), 1 );
	$text = "";
	$i = 0;
	foreach ( $elements as $element ) {
		if ( trim ( $element ) ) {
			if ( $i && $text ) $text .= $delimiter;
			$text .= $element;
		}
		$i++;
	}
	return $text;
}

Note that you have to pass a delimiter first, and you should include a space if necessary (e.g. ", "). After that, pass any number of strings and they’ll be output without gaps.

Archives