新版链接页面增加一键获取——粗略版

文章最后更新时间:2025-09-07 23:17:14

本来没想过继续写主题的美化代码的,也是刚好开了个API站点里面就有网址信息查询的接口,顺手花几分钟写了个粗略版出来,有兴趣的朋友可以自己在优化优化

截图演示

 

20250924015648268-20250924015648556971

20250924015659915-20250924015659985676

 

说明

代码不多,所包含bug肉眼可见,不过问题都是不大的,功能正常,没有问题,不需要额外修改文件,仅需在functions.php内插入代码即可

建议在func.php文件内插入代码,主题更新也不影响

// functions.php 或者func.php
function zib_nav_links_ajax_hand() {
    // 获取传入的 URL
    $url = isset($_GET['link_url']) ? sanitize_text_field($_GET['link_url']) : '';

    if (empty($url)) {
        zib_send_json_error(['msg' => '网址不能为空']);
    }

    // 调用 cURL 函数获取数据
    $api_response = zib_nav_links_curls('https://api.zbtool.cn/api/icp?url=' . urlencode($url));

    // 解码 JSON 响应
    $response_data = json_decode($api_response, true);

    if ($response_data && isset($response_data['code']) && $response_data['code'] === 200) {
        wp_send_json_success([
            'title' => $response_data['data']['title'],
            'description' => $response_data['data']['description'],
            'msg' => $response_data['data']['msg']
        ]);
    } else {
        zib_send_json_error(['msg' => $response_data['msg']]);
    }
}

// 注册 AJAX 动作
add_action('wp_ajax_zib_nav_links_ajax_hand', 'zib_nav_links_ajax_hand');
add_action('wp_ajax_nopriv_zib_nav_links_ajax_hand', 'zib_nav_links_ajax_hand');

// cURL 函数
function zib_nav_links_curls($url) {
    $ch = curl_init();
    $timeout = 30;
    $ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    $content = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpCode !== 200) {
        $error = curl_error($ch);
        curl_close($ch);
        throw new Exception("cURL Error: " . $error . " | HTTP Code: " . $httpCode);
    }

    curl_close($ch);

    // 尝试将内容转换为 UTF-8 编码
    $content = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content));
    // 去除字符串两端的空白字符  
    $content = trim($content); 

    return $content;
}

// 自定义 JSON 错误响应函数
function zib_nav_javascript() {
?>
<script>
$(function() {
    // 定义要插入的 HTML 内容
    const htmlToInsert = '<span class="tuc-c602702c-f4afdf-0 abs-right tuc-c602702c-f4afdf-0" style="top: 67%;"><button type="button" class="tuc-c602702c-f4afdf-0 but c-blue zbtool-submit tuc-c602702c-f4afdf-0" style="overflow: hidden; position: relative;">一键获取</button></span>';

    // 查找 name="link_description" 的 input 标签
    const inputTag = $('input[name="link_description"]');

    if (inputTag.length > 0) {
        // 在 input 标签后面插入 HTML 内容
        inputTag.after(htmlToInsert);
    }

    // 处理按钮点击事件
    $('.zbtool-submit').click(function() {
        var url = $("#link_url").val();

        // 显示加载提示
        notyf("加载中,请稍等...", "load", 2000, "zib_nav_golink");

        jQuery.ajax({
            type: "GET",
            dataType: "json",
            url: "<?php echo esc_url(admin_url('admin-ajax.php')) ?>",
            data: {
                action: "zib_nav_links_ajax_hand",
                link_url: url,
            },
            success: function (response) {
                if (response.success) {
                    $("#link_name").val(response.data.title);
                    $("#link_description").val(response.data.description);
                    notyf(response.data.msg, "", 0, "zib_nav_golink");
                } else {
                    notyf(response.data.msg, "danger", 0, "zib_nav_golink");
                }
            },
            error: function(errorThrown) {
                console.error("Ajax请求失败:", errorThrown);
                notyf("请求失败,请重试", "danger", 0, "zib_nav_golink");
            }
        });
    });
});
</script>
<?php 
}
add_action('wp_footer', 'zib_nav_javascript');
© 版权声明
THE END
喜欢就支持一下吧
点赞0
评论 抢沙发

请登录后发表评论

    暂无评论内容