Skip to navigation | Skip to content



Archive for the category ‘WordPress’

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 diggiing 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!

Control your own WordPress custom fields

I’m currently working on a client’s WordPress site where there’s quite a few bits of custom functionality in my custom theme that rely on them entering values for posts or pages using WP’s custom fields.

Custom fields are really flexible. However, they’re not perfectly user-friendly. For instance, if no post or page is currently using a custom field that you’ve built functionality on, the user has to enter the name as well as the value of the field the first time it’s used. The drop-down of field names is dynamically gathered from the fields currently in use. Also, sometimes you want to make things easier for clients by having inline tips, and inputs that suit the field (e.g. a checkbox or select drop-down instead of just a plain text entry).

So, I set about piecing together a way to take over the Custom Fields meta box…

Read more »

Get WordPress users by role

There’s a few posts around that reveal a nifty use of the WP_User_Search class that’s part of WordPress’ core code, in order to select all users that have a specific role (the trick seems to have originated with John Kolbert).

But… WP_User_Search is only loaded when you’re inside the WP admin area. What about front-end template code?

Read more »

New WordPress plugin: Lock Pages

I’m very please to announce my first WordPress plugin! It’s called Lock Pages.

The idea came from work with various clients where certain pages started to get moved around, or just vanished. A page can “move” by having its parent page or the slug changed. Sometimes clients would delete a page and replace it with an updated version which had a slightly different slug.

Of course, many of these things just stem from usually harmless habits picked up from working with files on a computer. But on a website, you want to persist your URLs as long as possible. Unless absolutely necessary, pages should be kept with the same permalink, to avoid breaking links and to maximize PageRank and such like.

Read more »

Plugins that detect whether they’re being used

There’s been a few posts round and about recently with people chipping in to make suggestions for WordPress plugins. Here’s my tuppence worth…

Read more »

WordPress server requirements

WordPress has famously low-level basic server requirements. However, most projects I deploy—because of plugins I rely on and other aspects—usually need a little more for “ideal” hosting.

Someone else used to maintain a really good “ideal” WP requirements page, but it seems to have vanished from their site. I thought I’d start my own, for my reference, to point potential clients to, and maybe of use to others.

Read more »

Double slashes in Analytics URLs

double-slash

I’ve just been dealing with an issue on a site where Google Analytics is logging a lot of pages twice, once normally and once with a double slash—”//”—at the end.

Obviously this is worrying. If Google is seeing the same page in two “places” via two technically different URLs, duplicate content penalties and PageRank squandering are distinct possibilities. It also seems to break a lot of the Analytics “Site Overlay” functionality.

Here I’m going to go through what I’ve done to isolate the cause of the issue, and approaches to fixing it.

Read more »

WordPress dashboard widget for PHP errors log

Jeff Starr recently posted on ways to monitor PHP errors. For some reason his method using WordPress’s wp-config.php file didn’t work for me, but I got the .htaccess version working OK.

I thought I’d knock out some code to display the latest errors as a widget on the WordPress dashboard.

As usual, this code is provided “as is”—use as you will, and let me know about any problems, but I can’t really offer support to people who aren’t comfortable with WordPress theme development. If anyone finds the time to wrap this up into a user-friendly plugin, do let me know (likewise if there’s already a plugin that does this better—I’ve not looked properly!).

Read more »

Careful with Cron!

soft-clock I’ve been strutting my stuff with handy “how-to” posts for a while. Time to eat some humble pie and give you a “how-not-to”…

Cron is a Unix staple for scheduling tasks. WordPress has a scheduling system that’s named after it. I think recently it got a new set of functions to make it easier to work with—things like wp_schedule_event. Anyway, I’m not going to explain it all to you because I don’t know. As I said, I’m going to show you what not to do.

Read more »

More archives