【问题标题】:WordPress Plugin: Call function on button click in admin panelWordPress插件:在管理面板中单击按钮时调用函数
【发布时间】:2012-01-25 17:43:42
【问题描述】:

我需要创建一个 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
} 


?>

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    虽然此页面上的答案提供了一个有用的开始,但我花了一段时间才弄清楚如何让选项 (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);
      }
    }  
    ?>
    

    【讨论】:

    • action="options-general.php?page=test-button-slug" 部分是否可以调整?
    • 感谢您的建议! :)
    • 希望我有更多的沙丁鱼
    • 这是一个很棒的样板。谢谢!
    【解决方案2】:

    嗯,你有两个选择。

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

    2) 将按钮放入表单中,将该表单发布到您的插件并添加一些代码来处理已发布的表单(如果您这样做,请确保您包含一个随机数以确保安全(http://codex.wordpress.org/WordPress_Nonces)以及确保尝试单击按钮的用户具有这样做的正确权限http://codex.wordpress.org/Function_Reference/current_user_can

    您尝试做的事情并不复杂,但它确实涉及对表单、PHP 和(也许)JavaScript 的良好理解。如果你的 JavaScript 没问题,我推荐选项 1,因为它不需要用户重新加载页面。

    【讨论】:

    • 正是我需要的。我对php,js,ajax等有很好的理解。我只是wp的新手
    • @brandwaffle,我很难理解如何将功能附加到按钮。你能指出我正确的方向吗?
    • 任何示例代码? formaction 属性应该如何看待?后端代码应该怎么看?
    • 我正在处理插件中的类似需求,我的解决方案是使用第二个表单(由“brandwaffle”回答),然后添加代码以检查第二个表单是否已提交,通过查看 $_POST[submit] 值。在那个“如果”中,我放置了我希望通过单击按钮发生的代码(或函数)。表单的“action”参数留空,因此它会重新加载插件的设置屏幕。
    猜你喜欢
    • 2015-02-03
    • 1970-01-01
    • 2014-08-08
    • 2012-06-20
    • 2016-04-08
    • 2014-12-03
    • 2021-10-08
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多