admin管理员组文章数量:1426066
I work for an architecture company and our project names mostly go like this: house|something
, bridge|somewhere
, building|whatever
.
Now, when I want to add a new project named like that, WordPress automatically converts it to housesomething
, bridgesomewhere
and puts that as the slug. I'd much prefer to keep some kind of separator, e.g. house-something
, bridge-somewhere
instead.
So, how to make WordPress convert |
to -
and not Empty String
(""
)? I'm obviously tired of doing that manually all the time.
It seems to me that it's very simple to do. It takes just a simple search and replace kind of thing if one knows where to look (in the WP core or wherever), but I haven't the slightest idea where to look, or what code to execute.
I work for an architecture company and our project names mostly go like this: house|something
, bridge|somewhere
, building|whatever
.
Now, when I want to add a new project named like that, WordPress automatically converts it to housesomething
, bridgesomewhere
and puts that as the slug. I'd much prefer to keep some kind of separator, e.g. house-something
, bridge-somewhere
instead.
So, how to make WordPress convert |
to -
and not Empty String
(""
)? I'm obviously tired of doing that manually all the time.
It seems to me that it's very simple to do. It takes just a simple search and replace kind of thing if one knows where to look (in the WP core or wherever), but I haven't the slightest idea where to look, or what code to execute.
Share Improve this question edited May 17, 2019 at 12:38 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 16, 2019 at 16:38 Marg9Marg9 354 bronze badges 02 Answers
Reset to default 7When WordPress inserts a post, it runs the title through a filter called sanitize_title
to get the slug. By default there is a function called santize_title_with_dashes
attached to this filter with priority 10. This function simply strips out the |
. If it is surrounded by spaces those spaces will be converted to hyphens.
So your task is to run a filter on the same hook before (say, priority 9) the default one and replace the |
with -
before it gets stripped away. Like this:
add_filter( 'sanitize_title', function ( $title ) {
return str_replace( '|', '-', $title );
}, 9 );
If you put spaces in between the words and the separator | the permalink will automatically include dashes between the words. For instance try this as your post title:
house | something, bridge | somewhere
That results in the slug:
house-something-bridge-somewhere
本文标签: filtersDon39t replace quotquot with Empty String (quotquot) when generating slugs from title
版权声明:本文标题:filters - Don't replace "|" with Empty String ("") when generating slugs from title 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465991a2659537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论