设置已保存!
';
}
// 获取当前设置
$custom_css = get_option('nenghui_custom_css', '');
$custom_html = get_option('nenghui_custom_html', '');
$css_enabled = get_option('nenghui_custom_css_enabled', '1');
$html_enabled = get_option('nenghui_custom_html_enabled', '1');
?>
自定义代码管理
在这里可以添加自定义的CSS和JavaScript代码,这些代码将在网站前端加载。
\n";
echo '\n";
echo "\n";
}
}
add_action('wp_head', 'nenghui_output_custom_css', 999);
/**
* 在前端输出自定义HTML/JavaScript
*/
function nenghui_output_custom_html() {
$html_enabled = get_option('nenghui_custom_html_enabled', '1');
$custom_html = get_option('nenghui_custom_html', '');
if ($html_enabled === '1' && !empty($custom_html)) {
echo "\n\n";
echo '' . "\n";
// 检查用户权限,只有管理员或编辑者才能使用此功能
if (current_user_can('edit_theme_options')) {
// 对于有权限的用户,直接输出HTML内容(已在后台进行基本验证)
echo wp_unslash($custom_html);
} else {
// 对于其他情况,进行严格的安全过滤
echo wp_kses($custom_html, array(
'script' => array(
'type' => array(),
'src' => array(),
'async' => array(),
'defer' => array()
),
'div' => array(
'id' => array(),
'class' => array(),
'style' => array()
),
'span' => array(
'id' => array(),
'class' => array(),
'style' => array()
),
'p' => array(
'id' => array(),
'class' => array(),
'style' => array()
),
'a' => array(
'href' => array(),
'target' => array(),
'id' => array(),
'class' => array(),
'style' => array()
),
'img' => array(
'src' => array(),
'alt' => array(),
'width' => array(),
'height' => array(),
'id' => array(),
'class' => array(),
'style' => array()
),
'h1' => array('id' => array(), 'class' => array(), 'style' => array()),
'h2' => array('id' => array(), 'class' => array(), 'style' => array()),
'h3' => array('id' => array(), 'class' => array(), 'style' => array()),
'h4' => array('id' => array(), 'class' => array(), 'style' => array()),
'h5' => array('id' => array(), 'class' => array(), 'style' => array()),
'h6' => array('id' => array(), 'class' => array(), 'style' => array()),
'ul' => array('id' => array(), 'class' => array(), 'style' => array()),
'ol' => array('id' => array(), 'class' => array(), 'style' => array()),
'li' => array('id' => array(), 'class' => array(), 'style' => array()),
'br' => array(),
'strong' => array('id' => array(), 'class' => array(), 'style' => array()),
'em' => array('id' => array(), 'class' => array(), 'style' => array()),
'b' => array('id' => array(), 'class' => array(), 'style' => array()),
'i' => array('id' => array(), 'class' => array(), 'style' => array())
));
}
echo "\n
\n";
echo "\n";
}
}
add_action('wp_footer', 'nenghui_output_custom_html', 999);
/**
* 为自定义代码管理页面添加帮助信息
*/
function nenghui_add_custom_code_help() {
$screen = get_current_screen();
if ($screen && $screen->id === 'appearance_page_nenghui-custom-code') {
$screen->add_help_tab(array(
'id' => 'nenghui-custom-code-help',
'title' => '使用帮助',
'content' => '自定义代码管理帮助
' .
'CSS代码示例:
' .
'/* 修改标题颜色 */\n' .
'h1, h2, h3 {\n' .
' color: #333;\n' .
'}\n\n' .
'/* 自定义按钮样式 */\n' .
'.custom-button {\n' .
' background: #007cba;\n' .
' color: white;\n' .
' padding: 10px 20px;\n' .
' border-radius: 4px;\n' .
'}' .
'JavaScript代码示例:
' .
'// 页面加载完成后执行\n' .
'jQuery(document).ready(function($) {\n' .
' // 添加点击事件\n' .
' $(".custom-button").click(function() {\n' .
' alert("按钮被点击了!");\n' .
' });\n' .
'});\n\n' .
'// 原生JavaScript示例\n' .
'document.addEventListener("DOMContentLoaded", function() {\n' .
' console.log("页面加载完成");\n' .
'});' .
'注意事项:
' .
'' .
'- CSS代码会在页面头部加载
' .
'- JavaScript代码会在页面底部加载
' .
'- 可以随时启用或禁用代码而不删除内容
' .
'- 建议在测试环境中先测试代码效果
' .
'
'
));
$screen->set_help_sidebar(
'相关链接:
' .
'CSS文档
' .
'JavaScript文档
' .
'jQuery官网
'
);
}
}
add_action('current_screen', 'nenghui_add_custom_code_help');
/**
* Header/Footer代码管理页面
*/
function nenghui_header_footer_admin_page() {
// 处理表单提交
if (isset($_POST['submit']) && wp_verify_nonce($_POST['nenghui_header_footer_nonce'], 'nenghui_header_footer_action')) {
// 保存Header代码
if (isset($_POST['header_code'])) {
update_option('nenghui_header_code_content', wp_unslash($_POST['header_code']));
}
// 保存Footer代码
if (isset($_POST['footer_code'])) {
update_option('nenghui_footer_code_content', wp_unslash($_POST['footer_code']));
}
// 保存启用状态
update_option('nenghui_header_code_enabled', isset($_POST['header_enabled']) ? '1' : '0');
update_option('nenghui_footer_code_enabled', isset($_POST['footer_enabled']) ? '1' : '0');
echo '';
}
// 获取当前设置
$header_code = get_option('nenghui_header_code_content', '');
$footer_code = get_option('nenghui_footer_code_content', '');
$header_enabled = get_option('nenghui_header_code_enabled', '0');
$footer_enabled = get_option('nenghui_footer_code_enabled', '0');
?>
Header/Footer自定义代码管理
在这里可以管理网站头部和底部的自定义HTML代码,如统计代码、验证代码、客服代码等。
\n";
echo wp_unslash($header_code);
echo "\n\n";
}
}
add_action('wp_head', 'nenghui_output_header_code', 998);
/**
* 在网站底部输出自定义Footer代码
*/
function nenghui_output_footer_code() {
$footer_enabled = get_option('nenghui_footer_code_enabled', '0');
$footer_code = get_option('nenghui_footer_code_content', '');
if ($footer_enabled && !empty($footer_code)) {
echo "\n\n";
echo wp_unslash($footer_code);
echo "\n\n";
}
}
add_action('wp_footer', 'nenghui_output_footer_code', 998);
?>