Company & Industry Related News

Utilizing the functions.php Wordpress template

One of the cooler things about Wordpress is the many, many ways you can extend its functionality and bend it to your will.  From creating themes, to plugins, you can pretty much use Wordpress to do just about anything you can think of and then some.  Diving deeper into that concept we’ll take a look a the functions.php template file and how to use it.

Why am I using this?

Simple!  The more you use Wordpress and more importantly the more you develop around the Wordpress platform the more you are going to realize that you need certain things done that Wordpress doesn’t natively provide.

One thing that I find myself needing often is the user’s permission level.  This concept aims towards using Wordpress as a full blown CMS (which in my opinion is rapidly heading in that direction anyway).  So without any further a due, let’s map out our first function.

Making it Happen

Step 1 – Create the functions.php file in your theme directory. Pretty straight forward right?  I could put a picture of how to do this but,  I think you’ll get it.

Step 2 – Write the function. In the new file that we’ve just created, add the following code.

<?php

/********************************************************************
 * Determine if the user is logged in and what user level they have *
 ********************************************************************/

function the_user_level () {
	if (is_user_logged_in()) {
		global $current_user, $wpdb;
		$userLevel = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE user_id=$current_user->id AND meta_key='wp_user_level'");
		return $userLevel;
	} else {
		return 0;
	}
}
?>

Step 3 – Utilize the function within a template file. All you need to do now is make the call to the function.  Try implementing it in any one of your template files with a piece of code something like this.

<?php
    $accessLevel = the_user_level();
    if ($accessLevel > 0) {
        echo '<p>Welcome user!  Your current access level is <strong>' . $accessLevel . '.</strong></p>';
    } else {
        echo '<p>You are currently not logged in.</p>';
    }
?>

In conclusion

And that’s pretty much it.  Very straight forward.  Please note that this tutorial is not for beginners and resides on the more intermediate/advanced level.   Stay tuned for more Wordpress tricks!

Share and Enjoy:
  • Digg
  • del.icio.us
  • StumbleUpon
  • Design Float
  • Reddit
  • Sphinn
  • Technorati
  • Facebook
  • Tumblr
  • Twitter

Leave a Reply

Free Consultation

Logo Design

Wordpress Custom Theme Development

follow us on facebook follow us on twitter follow us on linkedin
Northeast Web Design 17 Main Street Hebron, CT 06248
Connecticut based web design, development & internet marketing company.
northeastwebdesign.com © 2010 Northeast Computer Services LLC All Rights Reserved