• I took the sample code from https://developer.www.ads-software.com/plugins/security/nonces/ and created a plugin using but it causes wordpress to crash with the following message:

    atal error: Uncaught Error: Call to undefined function wp_get_current_user() in C:\dev\xampp\htdocs\wpSandBox\wp-includes\capabilities.php:652 Stack trace: #0 C:\dev\xampp\htdocs\wpSandBox\wp-content\plugins\marys_sandbox.php(65): current_user_can(‘edit_others_pos…’) #1 C:\dev\xampp\htdocs\wpSandBox\wp-settings.php(360): include_once(‘C:\\dev\\xampp\\ht…’) #2 C:\dev\xampp\htdocs\wpSandBox\wp-config.php(90): require_once(‘C:\\dev\\xampp\\ht…’) #3 C:\dev\xampp\htdocs\wpSandBox\wp-load.php(37): require_once(‘C:\\dev\\xampp\\ht…’) #4 C:\dev\xampp\htdocs\wpSandBox\wp-admin\admin.php(34): require_once(‘C:\\dev\\xampp\\ht…’) #5 C:\dev\xampp\htdocs\wpSandBox\wp-admin\plugins.php(10): require_once(‘C:\\dev\\xampp\\ht…’) #6 {main} thrown in C:\dev\xampp\htdocs\wpSandBox\wp-includes\capabilities.php on line 652

    There has been a critical error on your website. Please check your site admin email inbox for instructions.

    ——-

    the line causing the error is if (current_user_can(‘edit_others_posts’))
    if i remove the if statement it works fine.

    sorry i cannot share access to the site as it is on a local installation which i did yesterday so very latest release.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Dion

    (@diondesigns)

    There are multiple errors in that example. The “pluggable” wp_get_current_user() function is not defined when plugins are loaded, and the user is set up later in the WP load process, so that part of the example should be in the init action. The example also has a return null; statement at the end of the wporg_generate_delete_link function — it should instead be return $content;.

    The problem is that current_user_can() relies on wp_get_current_user() which is only available in the pluggable.php file. The pluggable.php file is loaded after plugins have been loaded. The Pluggable file allows plugins to overwrite the functions in the specific file.

    So to get your code to work you would need to wrap that whole thing in a plugins_loaded hook or later:

    /**
     * Initialize plugin by adding hooks
     *
     * @return void
     */
    function marys_sandbox_plugin_init() {
    	
    	if( current_user_can( 'edit_others_posts' ) ) {
    		/**
    		* add the delete link to the end of the post content
    		*/
    		add_filter('the_content', 'wporg_generate_delete_link');
    
    		/**
    		* register our request handler with the init hook
    		*/
    		add_action('init', 'wporg_delete_post');
    	}
    	
    }
    add_action( 'plugins_loaded', 'marys_sandbox_plugin_init' );
    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Code in the handbooks is not meant to be taken literally and copy/pasted directly into plugins.

    It is example code. You would create code like that in a plugin, but you would very likely have other code in that plugin doing other things. The if (current_user_can('edit_others_posts')) { code block would be one that you would want to put into some kind of initialization routine in your own plugin, probably hooked to the init action hook or something like that.

    Code in the documentation *should* break when you paste it in. I have intentionally put minor syntax errors into example code in the past for this very reason. This doesn’t mean that it is broken, it means that you’re supposed to read it and understand the lessons that it is trying to teach you.

    Don’t copy and paste code, write your own code to do what it is that you want to do. That code is teaching you about nonces. Not how to filter content, or to delete posts. There is no reason to ever attempt to actually run that code, you read it instead.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘plugin development document sample code breaks wordpress’ is closed to new replies.