{"id":4172,"date":"2023-11-10T17:58:49","date_gmt":"2023-11-10T16:58:49","guid":{"rendered":"https:\/\/www.knowboard.de\/?p=4172"},"modified":"2023-11-13T14:09:40","modified_gmt":"2023-11-13T13:09:40","slug":"shortcodes-in-titles-can-be-problematic-how-to-fix-this","status":"publish","type":"post","link":"https:\/\/www.knowboard.de\/shortcodes-in-titles-can-be-problematic-how-to-fix-this\/","title":{"rendered":"Render shortcode in title in menu and post\/page but strip shortcode for page title"},"content":{"rendered":"\n

If you use shortcodes in post titles they might not be rendered or if they are rendered they might not create a proper result or might not be suitable for being a title in the source code because HTML should not be in a title.<\/p>\n\n\n\n

Still, I have titles in a page where there is a special icon. A shortcode that creates HTML output to render that icon. But now I faced the problem that I do not want HTML in the site title in the source, but I want that HTML in the actual inline title top of the post or page. <\/p>\n\n\n\n

Shortcodes in titles can be problematic – How to fix this. <\/p>\n\n\n\n

Here are three snippets that help to achieve this. The two above force to render the shortcode in the post\/page title and in the menu but the third snippet strips the shortcode from the page title in the <title> HTML tag in the source. So you now have the best from both world.<\/p>\n\n\n\n

\/\/ Render shortcode in the page\/ post titles.\nadd_filter('wp_title', 'do_shortcode', 10);\nadd_filter('the_title', 'do_shortcode', 10);\n\n\/\/ Render shortcode in the menu.\nadd_filter('walker_nav_menu_start_el', 'do_shortcode', 10);\n\n\/\/ Strip shortcode from the actual html page title\nfunction custom_title($title_parts) {\n\t$title_parts['title'] = strip_shortcodes( $title_parts['title'] );\n    return $title_parts;\n}\nadd_filter( 'document_title_parts', 'custom_title' );<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

If you use shortcodes in post titles they might not be rendered or if they are rendered they might not create a proper result or might not be suitable for being a title in the source code because HTML should not be in a title. Still, I have titles in a page where there is…<\/p>\n","protected":false},"author":1,"featured_media":4187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-4172","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allgemein","entry","has-media"],"_links":{"self":[{"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/posts\/4172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/comments?post=4172"}],"version-history":[{"count":4,"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/posts\/4172\/revisions"}],"predecessor-version":[{"id":4188,"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/posts\/4172\/revisions\/4188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/media\/4187"}],"wp:attachment":[{"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/media?parent=4172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/categories?post=4172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.knowboard.de\/wp-json\/wp\/v2\/tags?post=4172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}