文章信息:


文章类型:

文章发布日期:

最后修改日期:

文章摘要:


有时候有些想法的时候,会需要用到一些碎片短代码,故此制作了以下,此处作为记载,以便日后如有需要可快速的查看,而…

整理下用过的,准备删除的一些好用简码:输出时间/访客数/访客信息/页面加载信息等

192 Views


有时候有些想法的时候,会需要用到一些碎片短代码,故此制作了以下,此处作为记载,以便日后如有需要可快速的查看,而不用再弄一遍。

访客信息输出,IP 客户端 手机

// 访客 IP 地址短代码
function visitor_ip_shortcode() {
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 
    return $_SERVER['REMOTE_ADDR'] ?? '无法获取IP';
}
add_shortcode('visitor_ip', 'visitor_ip_shortcode');
// 访客客户端信息短代码
function visitor_client_type_shortcode() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '未知客户端信息';
    // 判断客户端类型
    if (strpos($user_agent, 'Windows') !== false || strpos($user_agent, 'Macintosh') !== false) {
        return 'PC';
    } elseif (strpos($user_agent, 'Android') !== false) {
        return 'Android';
    } elseif (strpos($user_agent, 'iPhone') !== false) {
        return 'iPhone';
    } else {
        return '未知客户端';
    }
}
add_shortcode('visitor_client_type', 'visitor_client_type_shortcode');
// 浏览器类型短代码
function visitor_browser_shortcode() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '未知浏览器信息';
    // 简单的浏览器检测
    if (strpos($user_agent, 'Firefox') !== false) {
        return 'Firefox';
    } elseif (strpos($user_agent, 'Chrome') !== false && strpos($user_agent, 'Edge') === false) {
        return 'Chrome';
    } elseif (strpos($user_agent, 'Safari') !== false && strpos($user_agent, 'Chrome') === false) {
        return 'Safari';
    } elseif (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Trident') !== false) {
        return 'Internet Explorer';
    } elseif (strpos($user_agent, 'Edge') !== false) {
        return 'Edge';
    } else {
        return '未知浏览器';
    }
}
add_shortcode('visitor_browser', 'visitor_browser_shortcode');

输出访客数

function display_styled_visitor_count() {
    // 确保用户具有更新选项的权限,例如在需要的地方添加权限检查
    if (!current_user_can('manage_options')) {
        return '<span style="color: #ffffff; font-size: 24px;">#无权限查看访客数</span>';
    }
    // 使用 WordPress 的选项 API 存储和更新访客数
    $visitor_count = get_option('visitor_count', 0);
    $visitor_count++;
    // 使用 wp_cache_inc() 函数,这样可以避免并发问题
    update_option('visitor_count', $visitor_count);
    // 输出访客数,并添加简单的内联样式
    return '<span style="color: #ffffff; font-size: 24px;">#' . esc_html($visitor_count) . '</span>';
}
// 注册短代码
add_shortcode('styled_visitor_count', 'display_styled_visitor_count');

页面加载进度显示[page_load_time]/page_query_count/page_loading_progress

// 页面加载耗时短代码
function page_load_time_shortcode() {
    static $load_start_time;
    if (!isset($load_start_time)) {
        $load_start_time = microtime(true);
    }
    
    // 结束时间
    $load_time = (microtime(true) - $load_start_time) * 1000; // 转换为毫秒
    
    return sprintf('%.4f 毫秒', $load_time); // 修改返回格式
}
add_shortcode('page_load_time', 'page_load_time_shortcode');
// 页面查询数短代码
function page_query_count_shortcode() {
    global $wpdb;
    if (isset($wpdb)) {
        return '页面查询数: ' . get_num_queries();
    } else {
        return '无法获取页面查询数';
    }
}
add_shortcode('page_query_count', 'page_query_count_shortcode');
// 页面加载进度条短代码
function page_loading_progress_shortcode() {
    ob_start();
    ?>
    <div id="loading-progress" style="width: 100%; background: #f3f3f3; position: fixed; top: 0; left: 0; z-index: 9999;">
        <div id="progress-bar" style="width: 0; height: 5px; background: #4caf50;"></div>
    </div>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            var progressBar = document.getElementById('progress-bar');
            var width = 0;
            var interval = setInterval(function() {
                if (width >= 100) {
                    clearInterval(interval);
                } else {
                    width++;
                    progressBar.style.width = width + '%';
                }
            }, 10); // 进度条更新频率
            window.onload = function() {
                // 当页面完全加载后,隐藏进度条
                clearInterval(interval);
                progressBar.style.width = '100%'; // 确保进度条显示满
                setTimeout(function() {
                    document.getElementById('loading-progress').style.display = 'none';
                }, 500); // 延迟隐藏,给用户一个视觉反馈
            };
        });
    </script>
    <?php
    return ob_get_clean();
}
add_shortcode('page_loading_progress', 'page_loading_progress_shortcode');

输出当前时间current_time/current_day/current_date

// 创建一个短代码来输出当前时间
add_shortcode('current_time', 'display_current_time');
function display_current_time($atts) {
    // 获取当前时间
    $current_time = current_time('H:i'); // 24小时制
    return '<span style="font-size: 30px; color: white; background-color: black;">' . $current_time . '</span>'; // 返回当前时间,设置样式
}
// 创建一个短代码来输出当前星期几
add_shortcode('current_day', 'display_current_day');
function display_current_day($atts) {
    // 获取当前星期几
    $current_day = current_time('l'); // 获取完整的星期几名称
    return $current_day; // 返回当前星期几
}
// 创建一个短代码来输出当前年月日
add_shortcode('current_date', 'display_current_date');
function display_current_date($atts) {
    // 获取当前日期
    $current_date = current_time('Y-m-d'); // 格式为 年-月-日
    return $current_date; // 返回当前日期
}
// 调用示例:在 WordPress 编辑器中使用以下短代码
// [current_time], [current_day], [current_date]

// 创建一个短代码来输出访客的地理位置visitor_location

// 创建一个短代码来输出访客的地理位置
add_shortcode('visitor_location', 'display_visitor_location');
function display_visitor_location($atts) {
    // 获取真实的访客 IP 地址
    $ip = $_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]; // 获取代理的真实 IP
    }
    // 使用百度开放数据 API 获取位置信息
    $response = wp_remote_get("https://opendata.baidu.com/api.php?co=&resource_id=6006&oe=utf8&query=" . urlencode($ip));
    // 检查 API 响应是否有效
    if (is_array($response) && !is_wp_error($response) && isset($response['body'])) {
        $data = json_decode($response['body'], true);
        if (isset($data['status']) && $data['status'] === "0" && isset($data['data'][0]['location'])) {
            $location = esc_html($data['data'][0]['location']); // 获取地理位置
            return "欢迎您,来自【{$location}】的朋友"; // 返回格式化的欢迎信息
        }
    }
    return '无法获取位置信息'; // 如果无法获取位置,返回默认消息
}

网站文章分类及页面


格言警句