在 WordPress 中向子主题添加过滤器
我在 WordPress 中有一个基于二十的儿童主题。
我的一些作者在他们的帖子标题中硬编码了 URL,我想删除这些 URL。
我将以下代码放入子主题的 functions.php
文件中,但它对帖子标题的显示没有影响:
add_filter( ‘the_title’, ‘ib_strip_tags_from_titles’ );
function ib_strip_tags_from_titles( $title ) {
$title = strip_tags( $title );
return $title;
}
有什么建议吗?
I have a child theme in wordpress that is based on twentyten.
Some of my authors have hardcoded URLs in their post titles and I want to remove those URLs.
I put the following code in my functions.php
file in the child theme, but it has no effect on the display of the post title:
add_filter( ‘the_title’, ‘ib_strip_tags_from_titles’ );
function ib_strip_tags_from_titles( $title ) {
$title = strip_tags( $title );
return $title;
}
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
strip_tags()
仅删除 HTML 标签 - 在您的情况下,它将更改Some Text LINK 的 标题Other Text
到
Some Text LINK Other Text
如果我理解正确,这就是你想要的:
按照上面的示例,它将输出
Some Text Other Text
请注意,鉴于您尝试使用
strip_tags()
完成任务,我假设您所描述的“硬编码 URL”包含在标签。如果不是这种情况,您将需要一个匹配 URL 的正则表达式。这要棘手得多,具体取决于您的作者使用的 URL 是否国际化/是否具有不同的域,是否都只是 http:// 开头等等。
我保证,如果它们包含在标签中,上述内容就可以工作,如果没有,这个正则表达式将捕获大多数网址,但我不能保证在每种情况下都能工作:
(([A-Za-z]{3, 9})://)?([-;:&=\+\$,\w]+@{1})?(([-A-Za-z0-9]+\.)+[A -Za-z]{2,3})(:\d+)?((/[-\+~%/\.\w]+)?/?([&?][-\+=& ;%@\.\w]+)?(#[\w]+)?)?
您可以将其放在上述函数中的“/ 和 /”之间。
strip_tags()
only removes HTML tags - in your case it will change the title fromSome Text <a href="http://someurl.com">LINK</a> Other Text
to
Some Text LINK Other Text
If I understand you correctly, this is what you want:
going with the above example it will output
Some Text Other Text
Note that given that you tried to accomplish the task with
strip_tags()
, I am assuming the "harcoded URLs", as you described them, are enclosed in<a [...] ></a>
tags. If that's not the case you would need a regular expression that matches URLs. That is much more tricky, depending on whether the URLs your authors use are internationalized / have different domains, are not all just http:// prefaced and so on.I vouch for the above to work if they are enclosed in tags, if not, this regex will catch most URLs, but comes without my guarantee to work in every case:
(([A-Za-z]{3,9})://)?([-;:&=\+\$,\w]+@{1})?(([-A-Za-z0-9]+\.)+[A-Za-z]{2,3})(:\d+)?((/[-\+~%/\.\w]+)?/?([&?][-\+=&;%@\.\w]+)?(#[\w]+)?)?
You'd have put that between the '/ and /' in the above function.