It’s important to read the documentation for filters. The documentation for pre_get_document_title
says (emphasis mine):
Filters the document title before it is generated.
and
$title
(string) The document title. Default empty string.
So when using pre_get_document_title
, the title has not been set yet, so when you do this:
return $title . ' new title';
$title
is — as documented — an empty string, so the result will be:
' new title'
So if you want to modify the existing title, you need to use document_title_parts
, where you’ll modify $title['title']
:
add_filter(
'document_title_parts',
function( $title ) {
$title['title'] = substr( $title['title'], 4 ); // This needs to be 4, to account for the space.
return $title;
}
);
Related Posts:
- WP action/filter to modify title before header output and article output?
- get_the_archive_title hook unwanted changes!
- Hide H1 Title using the_title filter
- Anyway to edit the titlebar of WordPress Widgets in the Admin area?
- Strip $before & $after strings from a page title
- Filter the title to only affect the_title() template function
- Auto append text after the title?
- How many filter/action hooks are healthy?
- Earliest hook to reliably get $post/$posts
- What does (10, 2) mean when used with add_filter
- Clarification on filters and hooks
- Changing title of a page dynamically from within a plugin
- Valid characters for actions, hooks and filters
- How to hook into unregistering a widget instance?
- How to check if a hook is hooked or not?
- Passing Additional Parameters to add_filter Callable
- Modify WordPress Rest Api Request/Response
- How to add some custom HTML into wordpress admin bar?
- Custom theme hooks / filters – passing arguments
- Remove “Private” + “:” in title does not work
- How to hook wp_list_pages?
- Don’t replace “|” with Empty String (“”) when generating slugs from title
- How to use the_excerpt in a filter hook?
- Hook into admin post list page
- Alter only the page title, not the post titles within
- Change text of Description in Image Library
- Load different template file when condition met?
- Am I using the right hook for removing quicktags on the admin TinyMCE?
- About Hooks and Filters
- Changing document title only on a custom page template
- Should I use add_action(‘publish_post or add_filter(‘publish_post?
- Filter all html output
- Add a Span Around a Product Title in WooCommerce [closed]
- Too many actions/filters!
- How to get list of all hooks of current theme / plugin?
- Which hook is fired when inserting media into a post
- Change WordPress RSS link with filter?
- add_filter() function misunderstanding
- Is it possible to track down Actions and Filters?
- Replace category titles
- Filter get_the_title to remove certain characters?
- get_title without filter(the_title)
- Filter or Hook to catch pre-rendering of post content
- Can the wp_filter object hold multiple values with the same key
- check to see if hook is available
- Can not set custom title on some WordPress setups
- apply_filters/do_action tag characters limit
- How do I hide the current page’s title?
- How to modify core when there is no hook?
- Post Content, Special Characters and Filters
- How to filter for user registration, be able to throw error message
- How to remove action with slashes and arrows?
- Better extend a class or use add filter/action hooks?
- Add content as soon starts
- Access get_the_title() from ‘excerpt_length’ filter
- Changing WordPress core without hacking core
- Customize title, description and focused keyword [closed]
- Comment search plugin
- Plugin options, presets and filters : can you help me improve my workflow?
- how to customize rss feed tags using hooks?
- Filtering the post list in the admin area
- How can I replace an oEmbed URL in post with actual embed HTML
- Woocommerce “added to cart” message on main(home) page
- How to check if a protected hook is hooked?
- Wrapping my head around add_filter
- Gutenberg Block – Post Featured Image Filter Hook
- Change custom post type GUID in RSS
- filter hook to load a different post/page on current post/page
- Redirect to woocommerce checkout after adding to cart – item already in cart
- How can I edit comment meta value before it is saved?
- How To Get Search Term and Use in Function
- Remove Site Name from wp_title WITHOUT removing from attribute
- Where exactly does the edit_{taxonomy} hook fire?
- Is there any reason for the nested filters to be applied on the first case and not in the second one?
- Same URL for portfolio and for a page creates 404 error. Is there any filter that i can use for a child page?
- Filter Media by attached page or blog post in Library
- How do I add a line break to a string that is output by PHP?
- Filter widget_posts_args not working
- style_loader_tag not changing stylesheet to preload
- How can I filter the comment action links so that I can display the actions links based on user capabilities?
- Right filter for rewriting page statuscode
- Changing title using filter not working with argument
- How to filter page title for certain page?
- Re-order search results with posts_orderby filter and post meta value
- Adding Filter | Page Title | Template | Why the_title affecting on menu?
- How to filter wp_get_recent_posts() to only posts that have thumbnails?
- Adding user filter – Not updating data in URL
- How to check which submission button was clicked?
- Converting restricted html in comments to bbcode
- Why none of xmlrpc filters work
- How to add a class to Buddypress avatars in the Activity stream? [closed]
- Which hook should be used in this case?
- Getting entry ID from frm_email_message filter in formidable
- Show page name in browser
- How to use apply_filters(‘get_calendar’) to change get_calendar() output?
- Better way to change the default password reset url with the woocommerce one?
- Hooking/Filtering theme name to add theme directory
- Replacing text using add_filter
- Passing value from one hook to another
- Change password reqts with NO plugin without breaking resetpass link?