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
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.
';
echo 'Test Button Demo
';
// 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 '';
echo '
' .'The "Call Function" button was clicked.' . '
Could not write the log file to the temporary directory: ' . $path . '
'; } else { echo 'Log of button click written to: ' . $path . '
'; fwrite ($handle , "Call Function button clicked on: " . date("D j M Y H:i:s", time())); fclose ($handle); } } ?>