0

I am writing a WordPress plugin which is creating a form in one of its functions. It looks like this (very simplyfied):

class MyPlugin {
   public function createForm() {
      $nonce = wp_create_nonce('my_form_nonce');
      echo '<input type="hidden" value=" . $nonce . ">';
   }
}

This is not working. I get PHP error message function wp_create_nonce doesn't exist.

Well I understand that there are not all WP functions available in my custom plugin class context. But how do I do this? How do I implement WP token security inside my plugin form? What is the common approach here?

0

wp_create_nonce is a pluggable function loaded after plugins are loaded.

Be sure to call your class method on proper hook, 'init' (or later) is a good place: once your function output something (a form) there is no reason to run earlier than that.

  • Thank you for explaining how things inside Wordpress works. But the problem is, my plugin outputs the HTML form via shortcode function and in that moment wp_create_nonce in not available but needed for the creation of the form. I am stuck here :-) – Macejkou Oct 22 '14 at 11:47
  • @Macejkou a shortcode function should run when post content is shown on page, and there wp_create_nonce should be available. If isn't available means you are running the shortcode too early, impossible to say what you are doing wrong without seeing the code. – gmazzap Oct 22 '14 at 11:51
  • Thank you so much for such a quick reaction. You were right! I was calling the shortcode callback in plugins constructor. It was indeed to early. Wrapped it in init callbaback and works like charm. – Macejkou Oct 22 '14 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.