WordPress自动替换文章标题、文章内容、tag标题的方法

原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/3791.html

在WordPress下有一个钩子函数add_filter() 可以挂载一个函数到指定的过滤器上。

add_filter( $tag, $function_to_add, $priority, $accepted_args );

 

$tag (字符串)(必须)所挂载的过滤器名字(和目标 apply_filters() 函数的 $tag 属性一样)。 默认值:None $function_to_add (回调)(必须)要挂载的回调函数,参考 PHP 回调函数类型文档。 默认值:None $priority (整数)(可选)执行顺序,越小函数越先被执行。 默认值:10 $accepted_args (整数)(可选)回调函数接收的参数数量,设置多个可以接收更多 apply_filters() 函数传进来的参数。 默认值:1

add_filter(‘the_content’, ‘自定议函数’); -->替换文章正文中的文本内容
add_filter(‘the_excerpt’, ‘自定议函数’); -->替换文章摘要中的文本内容
add_filter(‘the_title’, ‘自定议函数’); -->替换文章标题中的文本内容
add_filter(‘the_tags’, ‘自定议函数’); -->替换文章中的tag信息

add_filters() 有两种方式触发,一种是当程序运行到特定时候 WordPress 程序会判断用户是否对某些参数做了新的定义,如果有则优先使用用户的设置;另一种方式是,用户可以直接通过 apply_filters() 函数直接调用。

IP 地址 216.73.216.138
区域位置 美国加利福尼亚洛杉矶
系统信息 🇨🇳 系统浏览器
最后修改:2021 年 04 月 21 日

赞赏支持

文章二维码