WPML: Restricting Editors to languages

This actually comes up quite a lot on Google and WPML’s forums. You have multiple Editors on your site and you want to be able to restrict each of them to certain languages. Unfortunately WPML doesn’t come with the ability built in and frankly they don’t make it easy to implement either (private variables)! Having spent a reasonable amount of time figuring out how to do it (and being overly pleased when I did) I thought I’d save other people a load of trouble and release it as a plugin.

I’ve actually written dozen of plugins and themes in the past but they’ve all been for private projects or for a very specific use, this is my first publicly available one. It actually turned out to be a pretty perfect project for my first plugin, the size and scope of it is just right and it touches on a lot of different areas. As I do with a lot of my plugins I used the rather excellent WordPress Plugin Boilerplate which I heartily recommend as a base for nay plugin. To be honest the hardest part of the lot was trying to use SVN, having never really used it and coming from a git background it really made very little sense to me and took longer than I feel it should for me to work it out. Anyway, without further ado…

My first plugin

Ok so it’s actually been out since May on the WordPress.org plugin directory but I’ve only just got round to writing this article, but I have just updated it to 1.0.2 with some important bug fixes. You can view it or download it here.

This plugin relies on WPML, it is useless without and won’t activate. You can buy WPML here.

WordPress: How to change the text labels on login and lost password pages

Editing the text labels on the WordPress login form and the lost password form is pretty easy. There’s a handy action called gettext that all WordPress translated text for the entire site goes through (whether it’s being translated or not) that can be hooked into allowing you to change anything you like. Using this we can change any text we like almost anywhere on the site.

Change Username to Email

As a quick example, below is how you’d use the gettext action to replace Username with Email in the form labels. We’re wrapping it in the login_head action to ensure that it is only replaceed on the login page, otherwise it would do it site wide which we probably don’t want.

/**
 * Changes 'Username' to 'Email Address' on wp-admin login form
 * and the forgotten password form
 *
 * @return null
 */
function oz_login_head() {
	function oz_username_label( $translated_text, $text, $domain ) {
		if ( 'Username or E-mail:' === $text || 'Username' === $text ) {
			$translated_text = __( 'Email' , 'oz-theme' );
		}
		return $translated_text;
	}
	add_filter( 'gettext', 'oz_username_label', 20, 3 );
}
add_action( 'login_head', 'oz_login_head' );

Yes, you can also use it to replace the callout text on the lost password page. Questions? comments? Criticism? Post ’em below.

WordPress: How to move the excerpt meta box above the editor

Adding and removing new meta boxes is pretty easy in WordPress thanks to the numerous native functions. The same functions can even be used to change the position of native meta boxes, moving them up, down and into the sidebar as you please. However an often googled request is how to move the excerpt meta box above the content wysiwyg editor. A lot of themes use it to summarise the post content and some even as a subheading in the actual post, so it just seems to make more sense in the page flow having it after the title.

Surprisingly despite the numerous questions asked on Stack Overflow and the WordPress forums almost no one seems to come up with an adequate solution. The most common hack is to output a form using the ‘edit_form_after_title’ action, a method that sort of works but lacks proper WordPress meta box styling and is frankly a bit nasty.

Moving the excerpt meta

If you want to skip the explanation just copy and paste the following code to your theme’s functions.php.

/**
 * Removes the regular excerpt box. We're not getting rid
 * of it, we're just moving it above the wysiwyg editor
 *
 * @return null
 */
function oz_remove_normal_excerpt() {
	remove_meta_box( 'postexcerpt' , 'post' , 'normal' );
}
add_action( 'admin_menu' , 'oz_remove_normal_excerpt' );

/**
 * Add the excerpt meta box back in with a custom screen location
 *
 * @param  string $post_type
 * @return null
 */
function oz_add_excerpt_meta_box( $post_type ) {
	if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
		add_meta_box(
			'oz_postexcerpt',
			__( 'Excerpt', 'thetab-theme' ),
			'post_excerpt_meta_box',
			$post_type,
			'after_title',
			'high'
		);
	}
}
add_action( 'add_meta_boxes', 'oz_add_excerpt_meta_box' );

/**
 * You can't actually add meta boxes after the title by default in WP so
 * we're being cheeky. We've registered our own meta box position
 * `after_title` onto which we've regiestered our new meta boxes and
 * are now calling them in the `edit_form_after_title` hook which is run
 * after the post tile box is displayed.
 *
 * @return null
 */
function oz_run_after_title_meta_boxes() {
	global $post, $wp_meta_boxes;
	# Output the `below_title` meta boxes:
	do_meta_boxes( get_current_screen(), 'after_title', $post );
}
add_action( 'edit_form_after_title', 'oz_run_after_title_meta_boxes' );

 

How does it work

Essentially it’s three steps, remove the existing postexcerpt, re-register postexcerpt with our own custom meta location, output the meta box at the new location.

1. De-register the existing postexcerpt.

/**
 * Removes the regular excerpt box. We're not getting rid
 * of it, we're just moving it above the wysiwyg editor
 *
 * @return null
 */
function oz_remove_normal_excerpt() {
	remove_meta_box( 'postexcerpt' , 'post' , 'normal' );
}
add_action( 'admin_menu' , 'oz_remove_normal_excerpt' );

 

2. Add the postexcerpt box back in but using a custom meta box location, (not normal, advanced or side)

/**
 * Add the excerpt meta box back in with a custom screen location
 *
 * @param  string $post_type
 * @return null
 */
function oz_add_excerpt_meta_box( $post_type ) {
	if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
		add_meta_box(
			'oz_postexcerpt',
			__( 'Excerpt', 'thetab-theme' ),
			'post_excerpt_meta_box',
			$post_type,
			'after_title',
			'high'
		);
	}
}
add_action( 'add_meta_boxes', 'oz_add_excerpt_meta_box' );

3. Using the `edit_form_after_title` action run the do_meta_boxes() function calling our custom location, `after_title`.

/**
 * You can't actually add meta boxes after the title by default in WP so
 * we're being cheeky. We've registered our own meta box position
 * `after_title` onto which we've registered our new meta boxes and
 * are now calling them in the `edit_form_after_title` hook which is run
 * after the post tile box is displayed.
 *
 * @return null
 */
function oz_run_after_title_meta_boxes() {
	global $post, $wp_meta_boxes;
	# Output the `below_title` meta boxes:
	do_meta_boxes( get_current_screen(), 'after_title', $post );
}
add_action( 'edit_form_after_title', 'oz_run_after_title_meta_boxes' );

And that’s it! Your excerpt should now be sitting pretty under the title box. Any comments let me know below.