子比主题 – 友链页面一键获取网站信息
给子比主题友情链接导航页面加一个一键获取文章信息的功能,这样站长就不用费劲去自己输入了,非常的实用的功能,喜欢的自行部署吧!代码部署
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创一个,记得加上php头,要不然报错,将下面的代码放里面即可!
function zib_nav_links_ajax_hand() {
$url = isset($_GET['link_url']) ? sanitize_text_field($_GET['link_url']) : '';
if (empty($url)) {
wp_send_json_error(['msg' => '网址不能为空']);
}
try {
$api_response = zib_nav_links_curls('https://api.ahfi.cn/api/websiteinfo?url=' . urlencode($url));
$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['message']
]);
} else {
wp_send_json_error(['msg' => $response_data['message']]);
}
} catch (Exception $e) {
wp_send_json_error(['msg' => '请求API时发生错误: ' . $e->getMessage()]);
}
}
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');
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';
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_USERAGENT => $ua,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
];
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
if ($content === false) {
curl_close($ch);
throw new Exception("cURL Error: " . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
throw new Exception("HTTP Error: " . $httpCode);
}
return trim(mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content)));
}
function zib_nav_javascript() {
?>
<script>
$(function() {
const buttonHtml = '<style>#link_description{width:87%;}</style><span class="abs-right" style="top: 68%;right:5px;"><button type="button" class="but jb-pink zbtool-submit" style="overflow: hidden; position: relative;line-height: 1.7;">一键获取</button></span>';
const inputTag = $('input');
if (inputTag.length === 0) return;
inputTag.after(buttonHtml);
function toggleButtonState(disabled) {
$('.zbtool-submit').prop('disabled', disabled);
}
$('.zbtool-submit').on('click', function() {
const url = $("input").val();
if (!url) {
notyf("请输入网址", "danger", 0, "zib_nav_golink");
return;
}
toggleButtonState(true);
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) {
toggleButtonState(false);
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) {
toggleButtonState(false);
console.error("Ajax请求失败:", errorThrown);
notyf("请求失败,请重试", "danger", 0, "zib_nav_golink");
}
});
});
});
</script>
<?php
}
add_action('wp_footer', 'zib_nav_javascript');
页:
[1]