提问人:matead 提问时间:11/9/2023 最后编辑:matead 更新时间:11/9/2023 访问量:35
Wordpress - 在自定义设置页面添加第二个按钮
Wordpress - Add Second Button to Custom Settings Page
问:
如何在我的自定义 Wordpress 设置页面添加第二个按钮来执行 PHP 函数?
我尝试使用 Javascript,但这会导致其他 Wordpress 页面触发 POST 事件等问题。
function admin_settings_content_callback()
{
?>
<div class="wrap">
<h1>Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('menu_details_group'); ?>
<?php do_settings_sections('menu_details_group'); ?>
<br />
<br />
<hr />
<?php submit_button(); ?>
</form>
</div>
<!-- NOTE: The JavaScript code WAS originally here -->
<?php
}
add_action('admin_init', 'custom_codes_admin_init');
function custom_codes_admin_init()
{
add_settings_section('database_section', 'Copy Orders', 'database_section_callback', 'menu_details_group');
}
function database_section_callback()
{
echo '
<p>Copy all orders</p>
<button type="button" name="copy_orders_action" class="button" id="copy-orders-button">Copy All</button>
';
}
// this is the function I would like to execute
function update_order_button_action()
{
// get completed and on-hold orders with post ID's and associated user ID's
$orders = get_post_and_user_id_();
// loop through the users and process the orders as needed
foreach ($orders as $order)
{
$user_id = $order['user_id'];
$post_id = $order['post_id'];
// write to the database table (this function is in a different PHP file)
update_registrations(true, $post_id);
}
}
这是我正在使用的 JavaScript,但它似乎不可靠。消息框会弹出,但是当单击“确定”时,除非我手动刷新页面,否则PHP函数通常不会执行。
<script>
document.addEventListener('DOMContentLoaded', function()
{
const copyOrdersButton= document.getElementById('copy-orders-button');
copyOrdersButton.addEventListener('click', function(e)
{
e.preventDefault();
const confirmation = confirm('Copy All?');
if (confirmation)
{
jQuery.ajax(
{
type: 'POST',
url: '/wp-admin/admin-ajax.php',
data: {
action: 'update_order_button_action',
}
});
location.reload();
}
});
});
</script>
谢谢。
答:
0赞
Jarmo T
11/9/2023
#1
我仍然认为这将是解决这个问题的最佳方法。AJAX
如果您担心其他页面执行您的代码(尽管我不明白为什么会发生这种情况,假设您对 AJAX 触发器元素使用唯一的选择器),您可以只为特定页面将 JavaScript 排队。
JS 文件示例:
jQuery(document).ready(function($) {
$("#copy-orders-button").on("click", function(){
$.ajax({
url: ajaxurl,
data: {
'action':'update_order_button_action'
},
success:function(data) {
// Triggers when AJAX request successful
console.log(data);
},
error: function(errorThrown){
// Triggers when AJAX request not successful
console.log(errorThrown);
}
});
});
});
仅在特定页面上将 JS 排队的示例:
由于我不知道您在谈论哪个特定页面,因此您必须更改条件以匹配您的页面。
get_current_screen将返回当前 screen 对象。您甚至可能不需要检查查询参数。page
function enqueue_ajax_callback_script() {
$screen = get_current_screen();
if($screen->base == "your_screen_base" && $_GET['page'] == "your_page"){
wp_enqueue_script( 'your-ajax-callback-handler', 'path-to-your-js-file', array('jquery'), '1.0', true);
}
}
add_action( 'admin_enqueue_scripts', 'enqueue_ajax_callback_script' );
PHP 回调示例:
// this is the function I would like to execute
function update_order_button_action()
{
}
add_action( 'wp_ajax_update_order_button_action', 'update_order_button_action' );
评论
0赞
matead
11/9/2023
不起作用。我为您编辑了我的原始帖子,并添加了我的 JavaScript。如果你能看一下,也许你可以看到我在里面做一些愚蠢的事情。谢谢。
0赞
Jarmo T
11/9/2023
@matead我在你的PHP中没有看到AJAX回调
0赞
matead
11/9/2023
你是说,?add_action('wp_ajax_update_order_button_action', 'update_order_button_action');
0赞
Jarmo T
11/9/2023
是的。没有它,WordPress 的 AJAX 处理程序将不知道要执行什么函数
评论