Title: plugin development document sample code breaks wordpress
Last modified: February 28, 2020

---

# plugin development document sample code breaks wordpress

 *  [mnewi](https://wordpress.org/support/users/mnewi/)
 * (@mnewi)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/plugin-development-document-sample-code-breaks-wordpress/)
 * I took the sample code from [https://developer.wordpress.org/plugins/security/nonces/](https://developer.wordpress.org/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](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/plugin-development-document-sample-code-breaks-wordpress/#post-12491760)
 * 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;`.
 *  [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * (@howdy_mcgee)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/plugin-development-document-sample-code-breaks-wordpress/#post-12491791)
 * 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)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [6 years, 1 month ago](https://wordpress.org/support/topic/plugin-development-document-sample-code-breaks-wordpress/#post-12491870)
 * 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.

## Tags

 * [current_user_can](https://wordpress.org/support/topic-tag/current_user_can/)
 * [nonces](https://wordpress.org/support/topic-tag/nonces/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 4 participants
 * Last reply from: [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * Last activity: [6 years, 1 month ago](https://wordpress.org/support/topic/plugin-development-document-sample-code-breaks-wordpress/#post-12491870)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
