You need to toss global $current_user in there.
<?php
/*
Plugin Name: Hello-World
Plugin URI: http://yourdomain.com/
Description: A simple hello world wordpress plugin
Version: 1.0
Author: martin
Author URI: http://yourdomain.com
License: GPL
*/
global $current_user;
if (current_user_can('edit_pages') ) echo "Hi Admin!";
?>
Unfortunately this didn’t help. I’m getting another error:
Fatal error: Call to undefined function wp_get_current_user() in D:\www\adspace\wp-includes\capabilities.php on line 1039
When I put this line
if (current_user_can('edit_pages') ) echo "Hi Admin!";
into a function and then call it from theme, it works, But i need to run this code in constructor of a class – and there it does not work.
To make it simple: i cannot call any wordpress function in my plugin:-(
(i have upgraded wordpess to 3.2.1 meanwhile, code still doesn’t work)
Another observation:
that row of code is inside class constructor. If I call new instance of class inside main plugin file, it ends with error. But when I call constructor from theme, it works. It seems that wordpress functions are not “included” when plugin code is executed, but when theme code is executed, they are. Can this be true?
In part … that’s because you’re not telling your plugin WHERE you want to output 🙂 The rest is because you’re not telling it HOW to output
Look at Hello Dolly: http://svn.automattic.com/wordpress/trunk/wp-content/plugins/hello.php
Cribbing this off the cuff…
global $current_user;
// This just echoes, we'll position it later
function hello_world() {
if (current_user_can('edit_pages') )
{echo "<p id='hello-world'>Hello, Admin!</p>";}
else
{echo "<p id='hello-world'>Hello, User!</p>";}
}
// Now we set that function up to execute when the admin_notices action is called
add_action( 'admin_notices', 'hello_world' );
// We need some CSS to position the paragraph
function world_css() {
// This makes sure that the positioning is also good for right-to-left languages
$x = is_rtl() ? 'left' : 'right';
echo "
<style type='text/css'>
#dolly {
float: $x;
padding-$x: 15px;
padding-top: 5px;
margin: 0;
font-size: 11px;
}
</style>
";
}
add_action( 'admin_head', 'world_css' );
The major takeaway for you here is this:
1) You can’t just ECHO things in a plugin, you have to say when and where.
2) We do this by creating functions and calling them in actions.