7折
减价出售
¥799
WordPress过滤器是Hook(钩子)系统的核心组成部分,专门用于修改数据。与动作(Action)钩子不同,过滤器必须返回修改后的值,允许开发者在数据处理的特定节点介入并改变其内容。
| 特性 | 过滤器(Filter) | 动作(Action) |
|---|---|---|
| 目的 | 修改数据 | 执行代码 |
| 返回值 | 必须返回值 | 无返回值 |
| 执行时机 | 数据处理过程中 | 特定事件发生时 |
// 定义过滤器钩子
$value = apply_filters( 'filter_name', $value, $param1, $param2 );
// 添加过滤器回调
add_filter( 'filter_name', 'my_filter_function', 10, 3 );
// 执行顺序:function_a → function_b → function_c
add_filter( 'the_content', 'function_a', 5 ); // 最早执行
add_filter( 'the_content', 'function_b', 10 ); // 默认优先级
add_filter( 'the_content', 'function_c', 15 ); // 最晚执行
/**
* 修改文章内容
*/
function enhance_post_content( $content ) {
// 只在单篇文章页面生效
if ( is_single() ) {
$custom_message = '<div class="custom-notice">感谢阅读本文!</div>';
$content = $custom_message . $content;
}
return $content;
}
add_filter( 'the_content', 'enhance_post_content' );
/**
* 自动为外部链接添加nofollow属性
*/
function auto_nofollow_external_links( $content ) {
$pattern = '/<a(.*?)href="(.*?)"(.*?)>/i';
$content = preg_replace_callback( $pattern, function( $matches ) {
$url = $matches[2];
if ( strpos( $url, home_url() ) === false ) {
return '<a' . $matches[1] . 'href="' . $url . '" rel="nofollow"' . $matches[3] . '>';
}
return $matches[0];
}, $content );
return $content;
}
add_filter( 'the_content', 'auto_nofollow_external_links' );
/**
* 在标题前添加图标
*/
function add_title_icon( $title, $id = null ) {
if ( in_the_loop() && is_main_query() ) {
$title = $title;
}
return $title;
}
add_filter( 'the_title', 'add_title_icon', 10, 2 );
/**
* 自定义摘要长度
*/
function custom_excerpt_length( $length ) {
return 30; // 将摘要长度改为30字
}
add_filter( 'excerpt_length', 'custom_excerpt_length' );
/**
* 自定义摘要更多链接文本
*/
function custom_excerpt_more( $more ) {
return '... <a href="' . get_permalink() . '">阅读更多</a>';
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
/**
* 修改管理员工具栏
*/
function customize_admin_bar( $wp_admin_bar ) {
// 移除WordPress logo
$wp_admin_bar->remove_node( 'wp-logo' );
// 添加自定义链接
$wp_admin_bar->add_node( array(
'id' => 'my-site',
'title' => '我的网站',
'href' => home_url()
) );
}
add_filter( 'admin_bar_menu', 'customize_admin_bar', 25 );
/**
* 修改登录页面样式
*/
function custom_login_logo() {
echo '<style>
.login h1 a {
background-image: url(' . get_stylesheet_directory_uri() . '/images/custom-logo.png) !important;
}
</style>';
}
add_filter( 'login_head', 'custom_login_logo' );
/**
* 根据用户角色显示不同内容
*/
function role_based_content_filter( $content ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( in_array( 'administrator', (array) $user->roles ) ) {
// 管理员看到额外内容
$content .= '<div class="admin-notes">管理员备注:这是敏感内容</div>';
} elseif ( in_array( 'subscriber', (array) $user->roles ) ) {
// 订阅者看到简化内容
$content = wp_trim_words( $content, 100 );
}
} else {
// 未登录用户看到摘要
$content = wp_trim_words( $content, 50 );
}
return $content;
}
add_filter( 'the_content', 'role_based_content_filter' );
/**
* 处理带多个参数的过滤器
*/
function advanced_comment_filter( $comment_text, $comment, $args ) {
if ( $comment->user_id == 1 ) {
// 管理员评论高亮显示
$comment_text = '<div class="admin-comment">' . $comment_text . '</div>';
}
// 添加评论时间
$comment_time = get_comment_time( 'Y-m-d H:i:s' );
$comment_text .= '<p class="comment-time">评论时间:' . $comment_time . '</p>';
return $comment_text;
}
add_filter( 'comment_text', 'advanced_comment_filter', 10, 3 );
/**
* 链式过滤器应用
*/
function first_content_filter( $content ) {
// 移除短代码
$content = strip_shortcodes( $content );
return $content;
}
function second_content_filter( $content ) {
// 转换表情符号
$content = convert_smilies( $content );
return $content;
}
function final_content_filter( $content ) {
// 自动段落
$content = wpautop( $content );
return $content;
}
// 按顺序应用过滤器
add_filter( 'the_content', 'first_content_filter', 5 );
add_filter( 'the_content', 'second_content_filter', 6 );
add_filter( 'the_content', 'final_content_filter', 7 );
/**
* 调试过滤器执行情况
*/
function debug_filters() {
global $wp_filter;
if ( WP_DEBUG && current_user_can( 'manage_options' ) ) {
echo '<pre>';
foreach ( $wp_filter as $filter_name => $filter_data ) {
if ( strpos( $filter_name, 'the_content' ) !== false ) {
print_r( $filter_data );
}
}
echo '</pre>';
}
}
add_action( 'wp_footer', 'debug_filters' );
/**
* 检查过滤器执行时间
*/
function measure_filter_time( $value ) {
$start_time = microtime( true );
// 模拟耗时操作
usleep( 1000 );
$end_time = microtime( true );
$execution_time = $end_time - $start_time;
error_log( "Filter executed in: " . $execution_time . " seconds" );
return $value;
}
add_filter( 'some_expensive_filter', 'measure_filter_time' );
/**
* 只在需要时加载过滤器
*/
function conditional_filter_loading() {
// 只在文章页面加载SEO相关过滤器
if ( is_single() ) {
require_once get_template_directory() . '/inc/seo-filters.php';
}
// 只在管理后台加载管理员过滤器
if ( is_admin() ) {
require_once get_template_directory() . '/inc/admin-filters.php';
}
}
add_action( 'wp_loaded', 'conditional_filter_loading' );
/**
* 防止用户名枚举
*/
function prevent_username_enumeration( $query ) {
if ( ! is_admin() && is_author() ) {
wp_redirect( home_url() );
exit;
}
return $query;
}
add_filter( 'author_rewrite_rules', '__return_empty_array' );
/**
* 隐藏WordPress版本号
*/
function remove_wp_version() {
return '';
}
add_filter( 'the_generator', 'remove_wp_version' );
/**
* 优化页面标题
*/
function seo_optimized_title( $title ) {
if ( is_single() ) {
$post_title = get_the_title();
$blog_name = get_bloginfo( 'name' );
return "$post_title - $blog_name";
}
return $title;
}
add_filter( 'wp_title', 'seo_optimized_title' );
/**
* 添加Open Graph元数据
*/
function add_og_metadata( $content ) {
if ( is_single() ) {
$og_image = get_the_post_thumbnail_url( get_the_ID(), 'large' );
$og_description = wp_trim_words( get_the_excerpt(), 25 );
$og_meta = '<meta property="og:title" content="' . get_the_title() . '">';
$og_meta .= '<meta property="og:description" content="' . esc_attr( $og_description ) . '">';
$og_meta .= '<meta property="og:image" content="' . $og_image . '">';
return $og_meta . $content;
}
return $content;
}
add_filter( 'wp_head', 'add_og_metadata' );
WordPress过滤器系统为WordPress主题开发者提供了强大的数据修改能力,合理运用可以极大地扩展WordPress的功能性和灵活性,是高级WordPress开发必须掌握的核心技术。
减价出售
减价出售
减价出售
减价出售
电话咨询
1855-626-3292
微信咨询