WordPress Plugin: Call function on button click in admin panel

前端 未结 2 1297
清歌不尽
清歌不尽 2021-01-31 09:30

I need to create a WordPress plugin that calls a PHP function when a button in an admin panel is clicked. I\'ve been looking at tutorials for writing basic WordPress plugins an

2条回答
  •  失恋的感觉
    2021-01-31 10:08

    Well, you have two options.

    1) Use AJAX to create an admin-ajax hook that you execute with JavaScript when the user clicks the button. You can learn about that approach here: http://codex.wordpress.org/AJAX (make sure to add a nonce for security ( http://codex.wordpress.org/WordPress_Nonces )). This is also a good resource for creating admin-ajax hooks: http://codex.wordpress.org/AJAX_in_Plugins

    2) Put the button in a form, POST that form to your plugin and add some code to handle the POST'd form (if you do this, make sure you include a nonce for security ( http://codex.wordpress.org/WordPress_Nonces ) and also make sure that the user trying to click the button has the right privileges to do so http://codex.wordpress.org/Function_Reference/current_user_can

    What you're trying to do is not super-complex, but it does involve a good understanding of forms, PHP, and (maybe) JavaScript. If your JavaScript is ok, I'd recommend option 1, since it doesn't require the user to reload the page.

提交回复
热议问题