[wp-hackers] checking roles in a plugin?

Mindshare Studios info at mindsharestudios.com
Fri Sep 7 05:45:51 GMT 2007


Hi - 

I'm trying to add some custom code to the Dashboard but when I add the
current_user_can('edit_posts') bit in all I get is a blank white page.

Here's basically what I'm trying to do (add a custom 'Getting started'
section):


<?php
/*
Plugin Name: Customizations
Plugin URI: Yada yada
Description: Some tweaks to the WP backend.
Version: 1.0
Author: Yada yada
Author URI: Yada yada
*/

$mindshareContent = '<h3>Welcome to ' . get_bloginfo('title') . '</h3><p>Use
these links to get started:</p><ul>';

if (current_user_can('edit_posts')) {
	$mindshareContent .= '<li><a href="page-new.php">Write a new
page</a></li>';
	$mindshareContent .= '<li><a href="edit-pages.php">Edit a
page</a></li>';
}
$mindshareContent .= '<li><a href="profile.php">Update your profile or
change your password</a></li></ul>';


if(strpos($_SERVER['SCRIPT_NAME'],'/index.php') == true &&
$_SERVER['QUERY_STRING'] == '') {
	add_action('admin_head', 'dashboard_start');
	function dasboard_wipe($buffer) {
		global $mindshareContent;
		// remove getting started section
		$buffer =
preg_replace('/\<\/div\>\s*\<p\>.*?\<\/p\>[\S\s]*?\<\/ul\>/','</div>',$buffe
r);
		// add custom code to dashboard
		$parts = preg_split('/\<div id="devnews">[\S\s]*\<div
id=\"footer\"\>/',$buffer); 
		if(count($parts) > 0){
			echo $parts[0];
			eval ('?>'.stripslashes($mindshareContent)) ;
			echo $after;
			echo '<div style="clear: both">&nbsp;<br clear="all"
/></div></div><div id="footer">';
			echo $parts[1];
		} else {
			echo $buffer;
		}
	}
	function dashboard_start($buffer){
		ob_start();
		add_action('admin_footer', 'dashboard_end');
		
	}
	function dashboard_end($buffer){
		$buffer = ob_get_contents();
		ob_end_clean();
		dasboard_wipe($buffer);
	}
}

?>
-----Original Message-----
From: wp-hackers-bounces at lists.automattic.com
[mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of John
Blackbourn
Sent: Thursday, September 06, 2007 10:06 PM
To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] checking roles in a plugin?

Sure you're echoing the variable out? (There's no echo in your code
snippet, that's all, and sometimes it's the simplist things...).

On 9/7/07, Keith Constable <kccricket at gmail.com> wrote:
> Mindshare Studios wrote:
> > Hi -
> >
> > How can you do a simple role check within a plugin before echoing some
> > content to the screen?
> >
> > I'm trying to do something like this:
> >
> > if (current_user_can('edit_posts')) {
> >       $var = 'sweet!';
> > }
> >
> > But that isn't working at all. Any ideas?
>
>
> Well, that's how you do it.  Are you testing it as the administrator?
> If not, are you absolutely sure that the user you're testing it as has
> the edit_posts capability?  Refer to Roles_and_Capabilities [1] on the
> Codex to be sure.
>
> [1] http://codex.wordpress.org/Roles_and_Capabilities
>
> -Keith Constable
> http://kccricket.net/
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list