WordPress.org

Forums

No access to wordpress functions in plugin (6 posts)

  1. martinstrouhal
    Member
    Posted 4 years ago #

    Hi,
    I'm trying to write my first wordpress plugin.
    Here is my code:

    <?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
    */
    
    if (current_user_can('edit_pages') ) echo "Hi Admin!";
    
    ?>

    And here is the result:
    Fatal error: Call to undefined function wp_get_current_user() in D:\www\adspace\wp-includes\capabilities.php on line 1028

    Similar situation appears when calling any wordpress function or trying to use global variable (eg. global $current_user). I simply have no access to WP functions. Can you tell me why, please?

    Thanks for any advice,
    Martin

  2. 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!";
    
    ?>
  3. martinstrouhal
    Member
    Posted 4 years ago #

    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:-(

  4. martinstrouhal
    Member
    Posted 4 years ago #

    (i have upgraded wordpess to 3.2.1 meanwhile, code still doesn't work)

  5. martinstrouhal
    Member
    Posted 4 years ago #

    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?

  6. 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.

Topic Closed

This topic has been closed to new replies.

About this Topic