
https://www.ediok.cn/blog/2025/07/6193.html 基于这个教程的二次扩展,增加安装次数记录
在JS部分的最后一个});上面增加ajax回调函数:
window.addEventListener('appinstalled', () => {
$.post(TBUI.ajaxurl, {
action: 'install_notification_popup',
nonce: TBUI.nonce
});
});
主题的functions.php里找个合适的位置增加如下代码:
add_action('wp_ajax_install_notification_popup', 'install_notification_popup');
add_action('wp_ajax_nopriv_install_notification_popup', 'install_notification_popup');
//WebAPP安装统计
function install_notification_popup() {
check_ajax_referer('install_notification_popup', 'nonce');
$current_count = get_option('install_notification_popup', 0);
$popup = $current_count + 1;
update_option( 'install_notification_popup', $popup );
wp_send_json_success();
}
这是在安装的时候回调函数执行的ajax,在option表里增加一个自增的install_notification_popup
然后继续增加如下代码
//WebAPP安装统计widget
add_action('wp_dashboard_setup', 'my_custom_install_notification_popup');
function my_custom_install_notification_popup() {
wp_add_dashboard_widget(
'custom_dashboard_widget',
'WebAPP安装',
'custom_install_notification_popup'
);
}
function custom_install_notification_popup() {
// 获取并显示参数
$custom_param = get_option('install_notification_popup', '0');
echo '<ul class="wp_baidu_list"><li class="wp_baidu_other"><p>安装数量:<b style="font-size:24px">' . esc_html($custom_param) . '</b></p></li></ul>';
}
在后台首页增加一个名为WebAPP安装的widget仪表盘,显示安装数量
周涛博客


回复前必须登录!
注册