WordPress插件:在管理面板中单击按钮时调用功能

NSj*_*nas 24 wordpress

我需要创建一个WordPress插件,在单击管理面板中的按钮时调用PHP函数.我一直在寻找编写基本WordPress插件和添加管理面板的教程,但我仍然不明白如何在我的插件中将按钮注册到特定功能.

这是我到目前为止所拥有的:

/*
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
} 


?>
Run Code Online (Sandbox Code Playgroud)

bra*_*fle 24

嗯,你有两个选择.

1)使用AJAX创建一个admin-ajax挂钩,当用户单击该按钮时,您可以使用JavaScript执行该挂钩.您可以在此处了解该方法:http://codex.wordpress.org/AJAX(确保添加nonce用于安全性(http://codex.wordpress.org/WordPress_Nonces)).这也是创建admin-ajax钩子的好资源:http://codex.wordpress.org/AJAX_in_Plugins

2)将按钮放在一个表单中,将表单POST到您的插件中并添加一些代码来处理POST表单(如果这样做,请确保包含一个nonce用于安全性(http://codex.wordpress.org/WordPress_Nonces)并确保尝试单击按钮的用户具有正确的权限http://codex.wordpress.org/Function_Reference/current_user_can

你要做的不是超级复杂,但它确实需要很好地理解表单,PHP和(可能)JavaScript.如果您的JavaScript没问题,我建议选项1,因为它不需要用户重新加载页面.

  • 任何示例代码?`form`的`action`属性应该怎么样?后端代码应如何显示? (2认同)

Obl*_*ely 24

虽然这个页面上的答案提供了一个有用的开始,但我花了一段时间才弄清楚如何让选项(2)工作.鉴于此,以下代码可能对某些人有所帮助.

如果您使用以下代码创建插件,当您在管理区域时,它将添加一个名为"测试按钮"的左侧菜单选项.点击这个,你会看到一个按钮.单击该按钮可运行该test_button_action功能.在我的示例函数中,我既在页面上放了一条消息又写入了一个日志文件.

<?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);
  }
}  
?>
Run Code Online (Sandbox Code Playgroud)