Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Plugin: Call function on button click in admin panel

Tags:

wordpress

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 and adding admin panels but I still don't understand how exactly to register a button to a specific function in my plug-in.

Here's what I have so far:

/* Plugin Name:  Plugin URI:  Description:  Author: Version: 1.0 Author URI: */   add_action('admin_menu', 'wc_plugin_menu');  function wc_plugin_menu(){  add_management_page('Title', 'MenuTitle', 'manage_options', 'wc-admin-menu', 'wc_plugin_options');   }  function wc_plugin_options(){ if (!current_user_can('manage_options'))  {     wp_die( __('You do not have sufficient permissions to access this page.')    ); } echo '<div class="wrap">'; echo '<button>Call Function!</button>'; //add some type of hook to call function echo '</div>';  }  function button_function() { //do some stuff }    ?> 
like image 572
NSjonas Avatar asked Dec 21 '11 23:12

NSjonas


People also ask

How do you call a function on a click in WordPress?

Simply go to Settings » WP Call Button and click on the Static Call Button menu on the top. Next, you can customize the call button text, color, and choose whether you want to show or hide the phone icon. After that simply copy the shortcode below and paste it inside the post or page where you want to add it.


2 Answers

Although the answers on this page provided a useful start, it took a while for me to figure out how to get option (2) working. Given this, the following code might be of help to some people.

If you create a plugin with the following code and it will add a left hand menu option called 'Test Button' when you are in the admin area. Click on this and you will see a button. Clicking that button runs the test_button_action function. In my example function I've both put a message on the page and written to a log file.

<?php  /* Plugin Name: Example of Button on Admin Page Plugin URI:  Description:  Author: Version: 1.0 Author URI: */   add_action('admin_menu', 'test_button_menu');  function test_button_menu(){   add_menu_page('Test Button Page', 'Test Button', 'manage_options', 'test-button-slug', 'test_button_admin_page');  }  function test_button_admin_page() {    // This function creates the output for the admin page.   // It also checks the value of the $_POST variable to see whether   // there has been a form submission.     // The check_admin_referer is a WordPress function that does some security   // checking and is recommended good practice.    // General check for user permissions.   if (!current_user_can('manage_options'))  {     wp_die( __('You do not have sufficient pilchards to access this page.')    );   }    // Start building the page    echo '<div class="wrap">';    echo '<h2>Test Button Demo</h2>';    // Check whether the button has been pressed AND also check the nonce   if (isset($_POST['test_button']) && check_admin_referer('test_button_clicked')) {     // the button has been pressed AND we've passed the security check     test_button_action();   }    echo '<form action="options-general.php?page=test-button-slug" method="post">';    // this is a WordPress security feature - see: https://codex.wordpress.org/WordPress_Nonces   wp_nonce_field('test_button_clicked');   echo '<input type="hidden" value="true" name="test_button" />';   submit_button('Call Function');   echo '</form>';    echo '</div>';  }  function test_button_action() {   echo '<div id="message" class="updated fade"><p>'     .'The "Call Function" button was clicked.' . '</p></div>';    $path = WP_TEMP_DIR . '/test-button-log.txt';    $handle = fopen($path,"w");    if ($handle == false) {     echo '<p>Could not write the log file to the temporary directory: ' . $path . '</p>';   }   else {     echo '<p>Log of button click written to: ' . $path . '</p>';      fwrite ($handle , "Call Function button clicked on: " . date("D j M Y H:i:s", time()));      fclose ($handle);   } }   ?> 
like image 104
Obliquely Avatar answered Nov 09 '22 10:11

Obliquely


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.

like image 37
brandwaffle Avatar answered Nov 09 '22 10:11

brandwaffle