[wp-hackers] checking roles in a plugin?

DD32 wordpress at dd32.id.au
Fri Sep 7 06:34:31 GMT 2007


Your problem is that WP isnt fully loaded at the point where you're running the current_user_can() function.
If you have errors enabled then you'd be seeing this error instead of the white screen:

 Fatal error: Call to undefined function wp_get_current_user() in C:\www\wordpress\wp-includes\capabilities.php on line 446
Call Stack
#	Time	Memory	Function	Location
1	0.0213	116592	{main}( )	..\index.php:0
2	0.0219	176184	require_once( 'C:\www\wordpress\wp-admin\admin.php' )	..\index.php:2
3	0.0222	186056	require_once( 'C:\www\wordpress\wp-config.php' )	..\admin.php:5
4	0.0248	341784	require_once( 'C:\www\wordpress\wp-settings.php' )	..\wp-config.php:22
5	0.8930	8162112	include_once( 'C:\www\wordpress\wp-content\plugins\test_plugin_\test.php' )	..\wp-settings.php:231
6	0.8930	8162136	current_user_can( )	..\test.php:13
(Well, thats got a stack trace too..)

By the look of it you'll want to change to using a hook instead:

add_action('load-index.php','test_init');
function test_init(){
global $mindshareContent;

$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>';

add_action('admin_head', 'dashboard_start');
}

Executing code outside of a hook in a plugin is a bad idea in a WP plugin, it can lead to all kinda of unexpected results.

D

On Fri, 07 Sep 2007 16:20:33 +1000, Mindshare Studios <info at mindsharestudios.com> wrote:

> Hi again,
>
> Here's an even more concise example of my issue, when I activate this plugin
> all bets are off - blank white screen. Why would this be killing WP?
>
> <?php
> /*
> Plugin Name:poop
> */
>
> if ( current_user_can('edit_posts') ) {
> 	//stuff
> }
>
> ?>
>
> Damian
>
> -----Original Message-----
> From: wp-hackers-bounces at lists.automattic.com
> [mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of Keith
> Constable
> Sent: Thursday, September 06, 2007 8:47 PM
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] checking roles in a plugin?
>
> 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
>
> _______________________________________________
> 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