Cleverness – WordPress Resources

My Top WordPress Plugins

There are many top ten lists of WordPress plugins out there. They all seem to contain the same thing, with one or two variations. Contact Form 7, Google XML Sitemaps, All-in-one SEO, etc. They are great plugins and I use them as well, but I really love when they list a lesser known plugin that ends up being useful to me.

These are some of the other plugins I use.

  • Cleverness To-Do List Plugin: Yep, my own plugin. I install it on every one of my sites and I adopted it after it was abandoned because I found it so useful.
  • Greg’s High Performance SEO: This is the SEO plugin that I prefer to use. I like it better than All-in-one SEO
  • Syntax Highlighting Evolved: This is the plugin that I use to display PHP and other code on this site. I tried out several syntax highlighters and I found this one to be the best. There are several color schemes you can choose from as well.
  • Theme Test Drive: I use this whenever I’m designing a new theme for one of my sites. It allows you to work on a theme without anyone else being able to see it.
  • Slideshow Gallery Pro: Lots of clients want slideshows on their page and I’ve found this to be the best plugin for them.
  • Advanced Excerpt: This plugin gives you much more control over how an excerpt looks.
  • Subscribe to Comments Reloaded: I don’t know how many users subscribe to comments, but I wanted to give people that option.
  • Relevanssi: The default search is not very good. This will provide more relevant search results.
  • Custom Field Template: This makes it easy to provide nice graphical interfaces to enter custom fields in posts.
  • WP-PageNavi: I sometimes like to have numbers at the bottom of pages rather than previous and next links.
  • jQuery Lightbox For Native Galleries: I’ve used several different lightbox plugins through the years and I’ve found this to be one of the better ones.
  • WP Maintenance Mode: This works great for new sites you’re working on or sites you’re redoing. It displays a coming soon/maintenance mode page for non-logged in users.

Updated BuddyPress User Roles Code

My post on adding user types to WordPress and Buddypress has been very popular.

This code will soon be available as a plugin.

I’ve updated the code so it’s cleaner and easier to implement. This code also works with or without allowing users to add new sites.

This only works for BuddyPress running on the multisite version of WordPress.
If you’re using the regular version, someone else has a plugin available for that: WP Roles at Registration. It works for BuddyPress (but not the multisite version).

Instructions

Set up your custom WordPress roles using Capability Manager or a similar plugin.
Place the following code into the functions.php file in your theme.
Edit the section titled Define User Roles and replace the values with your own.

<?php
/*------ Adding User Types to BuddyPress - http://cleverness.org ------*/

/**** == Define User Roles == ****/
/*
Replace usertypea with the name of the WordPress Role.
Replace User Type A with the label that you want to display on the registration form.
Repeat for the other roles.
Add or remove lines as needed, using this format:
'myrole' => 'My Role',
*/
 $customroles = array(
	'usertypea' => 'User Type A',
	'usertypeb' => 'User Type B',
	'usertypec' => 'User Type C',
	);
/**** == end Define User Roles == ****/

add_filter( 'bp_signup_usermeta', 'customroles_user_signup_field' );
add_filter( 'bp_core_activate_account', 'customroles_user_activate_field' );
add_action( 'wpmu_activate_user', 'customroles_register_role', 10, 3 );
add_action( 'wpmu_new_blog', 'customroles_register_role_blog', 10, 6 );
add_action( 'bp_after_account_details_fields', 'customroles_registration_fields' );

/* Add sign-up field to BuddyPress sign-up array */
function customroles_user_signup_field( $usermeta ) {
	$usermeta['signup_type'] = $_POST['signup_type'];
	return $usermeta;
}

/* Add sign-up field to usermeta on user activation */
function customroles_user_activate_field( $signup ) {
	update_usermeta( $signup['user_id'], 'signup_type', $signup['meta']['signup_type'] );
	return $signup;
}

/* Update the user's WP role and user meta */
function customroles_register_role($user_id, $password, $meta) {
	global $bp, $wpdb, $customroles;
   	$userdata = array();
   	$userdata['ID'] = $user_id;
   	$userdata['role'] = $meta['signup_type'];
	$type = $meta['signup_type'];

   	//only allow if user role is one of the allowed roles
   	if ( array_key_exists( $userdata['role'], $customroles ) ) {
      	wp_update_user($userdata);
		update_usermeta( $user_id, 'signup_type', $type );
   	}
}

/* Updates the user's WP role for when a new user creates a blog */
function customroles_register_role_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
	global $bp, $wpdb, $customroles;
   	$userdata = array();
   	$userdata['ID'] = $user_id;
   	$userdata['role'] = $meta['signup_type'];
	$type = $meta['signup_type'];

   	//only allow if user role is one of the allowed roles
   	if ( array_key_exists( $userdata['role'], $customroles ) ) {
      	wp_update_user($userdata);
		update_usermeta( $user_id, 'signup_type', $type );
   	}
}

/* Add user type fields to registration form */
function customroles_registration_fields() {
	global $customroles;
	echo '
		<!-- Begin User Type code -->
		<div class="register-section" id="usertype-details-section">
		<div class="editfield">
			<label for="signup_type">Account Type:</label>
			<p><select id="signup_type" name="signup_type">';
			foreach ( $customroles as $key => $value ) {
				echo '<option value="'.$key.'">'.$value.'</option>';
				}
		echo'</select></p>
		</div></div>
	<!-- end of User Type code -->';
}
/*------ end Adding User Types to BuddyPress ------*/
?>

You can use get_user_meta() in your theme files to show different things to different user types.

<?php
global $bp;
if ( get_user_meta($bp->displayed_user->id, 'signup_type') == 'usertypea' ) : ?>
    Put your HTML code here
<?php endif; ?>

JavaScript and AJAX for Dummies Review

I just finished reading JavaScript and AJAX For Dummies by Andy Harris.

I like the Dummies and Idiots books. I think they’re great if you don’t know much about a subject and want to get an introduction to it.

I took a course at college about JavaScript ten years ago. Back before jQuery and all that. The knowledge didn’t really stick with me and I’ve always felt uncomfortable using JS and tried to avoid it. I want to be able to use AJAX (which I know nothing about) so I need to update my JS skills.

One of the things I like best about this book is that it covers debugging. I use Firebug in Firefox, but all I really know how to do is the CSS stuff. The Inspect Element menu item is the BEST THING EVER. It’s so helpful in fixing CSS problems. Anyway, I never bothered reading more about Firebug and what it can do. Now I know how to use it and Aptana to debug JavaScript. The author is a fan of using Aptana for JS and it seems to me that it’s good.

The first JavaScript section goes over the fundamentals. Variables, loops, arrays. But it also covers objects and JSON. I hadn’t used objects before in JavaScript, only PHP and C++. I had no idea what JSON was. Now I know that it stands for JavaScript Object Notation and what it is.

I don’t know how easy this section would be to understand if you didn’t have a programming background, but I thought he explained everything clearly and with good examples. But I don’t think too many people without a programming background would be or should be reading a book that involves AJAX anyway.

The second JavaScript section talks about actually changing elements on a page with JavaScript, using the DOM. There’s a small section on regular expressions, which I didn’t expect. This section was interesting but I can’t really think of anything to say about it.

The third part covers jQuery and AJAX. I was expecting a little more than two chapters about AJAX, but maybe more isn’t really necessary. After the first chapter in this section, he uses jQuery for all the examples. He mentions other JS libraries but recommends jQuery. I’ve “used” jQuery before, adding image galleries and things, but not modifying it or writing it myself. I was surprised at how easy it is to use jQuery and AJAX to interact with PHP.

I think this book was a good introduction to JavaScript and AJAX and would recommend it. I can’t come up with any cons to this book except the example that consisted of the ants song.

This book is not for complete beginners. You need to know HTML and CSS. It would be best if you’ve at least fooled around with JavaScript before and are familiar with PHP.


2011

Thank you to those who have donated to me for the To-Do List plugin in the past few months. I really appreciate any donations. They motivate me to improve my plugin.

One recent donation and feature suggestion was to implement AJAX so things like the dashboard widget will not reload the page when updated. I tried briefly to do this back in the summer but didn’t get it working.

I purchased the WordPress & AJAX ebook today so I can learn how to do that. I also plan to add new features and fix anything that isn’t working correctly on the plugin.

Aside from that, some of my plans for 2011 include a new design for this site, major revisions to my base theme, a free complete theme, developing new plugins, and continuing to share WordPress knowledge.


To-Do List Plugin Bug Fix 2.2.8

Download To-Do List Plugin v2.2.8Last Updated 12-06-2010

I’ve fixed an issue with the To-Do List plugin where completed items were not showing up using list view in the shortcode.


I will be unavailable in November

I’m participating in NaNoWriMo next month, so I won’t be responding to any comments or emails. There will also be no plugin updates during this time.

I will be back in December.


To-Do List Plugin Bug Fix v2.2.7

Download To-Do List Plugin v2.2.8Last Updated 12-06-2010

I’m very sorry about the fatal error caused by a missing semi-colon in a file.

This is now fixed in verson 2.2.7. It is a available as a zip file here and should be updated in the WordPress SVN in about fifteen minutes.

Thanks to those who reported the issue.


To-Do List Plugin Updated v2.2.6

Download To-Do List Plugin v2.2.8Last Updated 12-06-2010

* Added value check for assign and priority variables
* Echoing the email values to the screen on failed email sending, for troubleshooting


User Types in BuddyPress – Assign xprofile Groups

As requested by Janis, here is the code I use to show only certain xprofile groups to each user type. In the default BuddyPress theme, profile fields without a value aren’t shown, so only the ones for that group will appear to the public.

You will need to replace a function call in your BuddyPress theme, in /members/single/profile/edit.php. It is at line 12 in mine.

Find

<?php bp_profile_group_tabs(); ?>

And replace it with this:

<?php my_bp_profile_group_tabs(); ?>

In /plugins/bp-custom.php place the following code. You will need to edit it to fit your user type and profile group needs.

function my_bp_profile_group_tabs() {
	global $bp, $group_name;

	$signup_type = get_user_meta($bp->loggedin_user->id, 'signup_type', true);

	if ( !$groups = wp_cache_get( 'xprofile_groups_inc_empty', 'bp' ) ) {
		$groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
		wp_cache_set( 'xprofile_groups_inc_empty', $groups, 'bp' );
	}

	if ( empty( $group_name ) )
		$group_name = bp_profile_group_name(false);

	for ( $i = 0; $i < count($groups); $i++ ) {
	if ( $signup_type == 'usertypea' && ( $groups[$i]->name == 'Base Profile' || $groups[$i]->name == 'Profile Group A' || $groups[$i]->name == 'Profile Group B' ) ) {
		if ( $group_name == $groups[$i]->name ) {
			$selected = ' class="current"';
		} else {
			$selected = '';
		}

		if ( $groups[$i]->fields )
			echo '<li' . $selected . '><a href="' . $bp->displayed_user->domain . $bp->profile->slug . '/edit/group/' . $groups[$i]->id . '">' . attribute_escape( $groups[$i]->name ) . '</a></li>';

	} elseif ( $signup_type == 'usertypeb' && ( $groups[$i]->name == 'Base Profile' || $groups[$i]->name == 'Profile Group C' ) ) {
		if ( $group_name == $groups[$i]->name ) {
			$selected = ' class="current"';
		} else {
			$selected = '';
		}

		if ( $groups[$i]->fields )
			echo '<li' . $selected . '><a href="' . $bp->displayed_user->domain . $bp->profile->slug . '/edit/group/' . $groups[$i]->id . '">' . attribute_escape( $groups[$i]->name ) . '</a></li>';
	} elseif ( $signup_type == 'usertypec' && ( $groups[$i]->name == 'Base Profile' || $groups[$i]->name == 'Profile Group D'  ) ) {
		if ( $group_name == $groups[$i]->name ) {
			$selected = ' class="current"';
		} else {
			$selected = '';
		}

		if ( $groups[$i]->fields )
			echo '<li' . $selected . '><a href="' . $bp->displayed_user->domain . $bp->profile->slug . '/edit/group/' . $groups[$i]->id . '">' . attribute_escape( $groups[$i]->name ) . '</a></li>';
		}
	}

	do_action( 'xprofile_profile_group_tabs' );
}

Adding User Types to WordPress and BuddyPress – with User Blogs

There is a new version of this code located at http://cleverness.org/2011/01/27/updated-buddypress-user-roles-code/. Please use that code and leave all comments on that page.

I’ve previously talked about adding user types to BuddyPress. I’ve found out recently that if you allow new users to create their own blogs, you will need to add the following code to your functions.php file.

function synchro_wp_usermeta_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
	global $bp, $wpdb;

	$type = $meta[signup_type];

	update_usermeta( $user_id, 'signup_type', $type );

}

add_action( 'wpmu_new_blog', 'synchro_wp_usermeta_blog', 10, 6 );

function register_role_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
	global $bp, $wpdb;

   	$userdata = array();
   	$userdata['ID'] = $user_id;
   	$userdata['role'] = $meta['signup_type'];

   	//only allow if user role is my_role

   	if ($userdata['role'] == 'usertypea' || $userdata['role'] == 'usertypeb' || $userdata['role'] == 'usertypec' ){
      	wp_update_user($userdata);
   	}

}

add_action( 'wpmu_new_blog', 'register_role_blog', 101, 6 );