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

2021年04月21日

原创内容,转载请注明出处: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() 函数直接调用。


sicnature ---------------------------------------------------------------------
Your current IP address is: 54.91.51.101
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://myzhenai.com.cn/post/3791.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注