Hot answers tagged

40

Try with this: <?php echo get_the_title( $ID ); ?>


20

Problem Description: Let me rephrase the question first. You want to: Set new title to all post and page type from a meta field. You want this to happen everywhere (home page, single page, widgets etc.) However, you don't want this title change to happen if the title is on the Navigation Menu. Solution: Before I give you the CODE, let me explain a few ...


15

I ran your filter in my development area. It didn't work. Then I switched off the Yoast SEO plugin, which I knew was also messing with the page title. Then it worked. So my suggestion would be another plugin is messing with it. In the case of Yoast, it was a filter call to pre_get_document_title returning non empty. In that case wp_get_document_title is ...


12

As of Wordpress 4.4, you can use the Wordpress filter document_title_parts to change the title. Add the following to functions.php: add_filter('document_title_parts', 'my_custom_title'); function my_custom_title( $title ) { // $title is an array of title parts, including one called `title` $title['title'] = 'My new title'; if (is_singular('post')) {...


11

Yes sure you can achive that simply using enter_title_here filter hook. add_filter('enter_title_here', 'my_title_place_holder' , 20 , 2 ); function my_title_place_holder($title , $post){ if( $post->post_type == 'portfolio' ){ $my_title = "Add new Portfolio"; return $my_title; } return $title; ...


10

The two title tags can be explained as that you are using a theme that is written for Wordpress4.1 and actually is using 4.1. As from 4.1 you don't need to call wp_title() in the head any more, you can make use of new title_tag theme support tag which automatically adds the wp_title() tag in the header The parent theme you are using are most probably ...


9

wp_get_document_title() has some interesting filters - pre_get_document_title and document_title_parts. /** * Filter the parts of the document title. * * @since 4.4.0 * * @param array $title { * The document title parts. * * @type string $title Title of the viewed page. * @type string $page Optional. Page number if paginated. * ...


9

You can see everything added to wp_head in the file /wp-includes/default-filters.php. If your theme supports the title tag, you can remove it entirely with remove_action: remove_action( 'wp_head', '_wp_render_title_tag', 1 ); Though it may be simpler/better to use remove_theme_support( 'title-tag' ) in a child theme, which is what _wp_render_title_tag ...


9

A post or page has only one title, the title tag <title> is the document title. The filter wp_title filters the output of wp_title() function, which was used before to output the title of the document. In WordPress 4.1, the title-tag support in themes was introduced and wp_get_document_title() is used instead of wp_title(). So, if your theme supports ...


8

Sorry to dig this question up from grave, but there's a better solution provided since WordPress 3.1. The enter_title_here filter. function change_default_title( $title ){ $screen = get_current_screen(); // For CPT 1 if ( 'custom_post_type_1' == $screen->post_type ) { $title = 'CPT1 New Title'; // For CPT 2 } elseif ( '...


8

I use a code which differs from yours a little bit. There is no need to get the current screen using the enter_title_here-filter because you have already a post-object: /** * Filter: Modifies the standard placeholder text * @param string $title * @param WP_Post $post * @return string */ function my_enter_title_here( $title, $post ) { if ( '...


8

It looks like your site's title is empty. Fill it out or try for example: add_filter( 'wp_title', function( $title ) { return trim( $title ); } ); to remove the blank space in front. For your setup, the following part of wp_title() is responsible for the blank space: $title = $prefix . implode( " $sep ", $title_array ); So when you're on a single ...


7

You can do this : add_action('admin_init', 'wpse_110427_hide_title'); function wpse_110427_hide_title() { if (current_user_can('subscriber')) remove_post_type_support('post', 'title'); } This would hide title for subscriber. Replace 'post' with your custom post type


7

Today there is a native option to do this without hacking the core, or doing tricky filters with output buffer. You just need to use the filter 'comment_form_defaults' and edit the values from 'title_reply_before' and 'title_reply_after' key: add_filter( 'comment_form_defaults', 'custom_reply_title' ); function custom_reply_title( $defaults ){ $defaults['...


7

For those wishing to change the document's title attribute, I found that using the wp_title filter no longer works. Instead, use the pre_get_document_title filter: add_filter("pre_get_document_title", "my_callback"); function my_callback($old_title){ return "My Modified Title"; } Source


7

There are actually two problems: The & is encoded as &amp;, so you have to use html_entity_decode() first. Multi-byte characters need more than one byte, and strlen() will fail with them. Don’t use strlen(). So use something like this: $title = html_entity_decode( get_the_title(), ENT_XML1, 'UTF-8' ); $length = mb_strlen( $title, 'utf-8' );


7

Strange. Outside the loop, the_title() should give you the current page name, if you really are on a page, and not viewing a specific post. If it gives post title instead, it may mean that you are somehow inside a loop. But if that were true, wp_title shouldn't show "Blog". See if other options give the same result: //the_title(); single_post_title(); echo ...


7

After some experimenting I came to the following suggestion: could it be, that the <title> tag is "hard coded" inside your parent theme's header.php? If that is the case, you could try to remove the <title> tag from your child theme's header.php (copy your parent's header.php into your child theme folder) and then add the theme support back ...


7

Use in_the_loop() conditional tag to make sure you are modifying the title inside the Loop and to prevent the hook changing titles somewhere else: function customize_wishlist_title( $title ){ $current_user = wp_get_current_user(); $loggeduser = $current_user->user_login; // give attention to the following condition: if ( is_page( 258 ) &...


7

When adding title-tag support in a theme, the title tag can be filtered by several filters, but not wp_title. The reason is that if the theme supports title-tag, WordPress uses wp_get_document_title() instead of wp_title(). For themes with support for title-tag you can use document_title_parts: add_filter( 'document_title_parts', '...


7

You can create a shortcode to fetch the title of current page. Try copying the following snippet in your child theme's functions.php function post_title_shortcode(){ return get_the_title(); } add_shortcode('post_title','post_title_shortcode'); Use [post_title] shortcode to display the title of the post or page.


7

When 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 ...


6

Instead of replacing spaces you should use the build in function sanitize_title() which will take care of the replacing for you. Like this: sanitize_title( $post_title, $post->ID ); Also, you should use a unique slug. Which you can get with the function wp_unique_post_slug() So putting it all together a solution might be: function ...


6

Your problem is that you can not use wp_title() in the theme if the theme already supports title-tag. The <head> of your theme should look like this: <head> <meta charset="<?php bloginfo( 'charset' ); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <?php wp_head(); ?> </head> ...


6

As of WP 4.4, wp_title is deprecated. If you simply need to override your title tag, pre_get_document_title is the filter you want to use. add_filter( 'pre_get_document_title', 'generate_custom_title', 10 ); and the function would look something like this function generate_custom_title($title) { /* your code to generate the new title and assign the $...


6

You can make use the queried object to return the title of the page used as blogpage You can use the following: (Require PHP 5.4+) $title = get_queried_object()->post_title; var_dump( $title );


6

WordPress has a function called url_to_postid that may work in this situation. You can try the following (untested): <?php $url = htmlspecialchars($_SERVER['HTTP_REFERER']); $back_id = url_to_postid($_SERVER['HTTP_REFERER']); if( $back_id > 0 ){ $back_title = get_the_title( $back_id ); echo "<a href='{$url}'>Go back ...


6

You can try the following to clear the image attachment's title when it's inserted but not updated: /** * Empty the image attachment's title only when inserted not updated */ add_filter( 'wp_insert_attachment_data', function( $data, $postarr ) { if( empty( $postarr['ID'] ) && isset( $postarr['post_mime_type'] ) &&...


6

There's a wpseo_title filter you can hook into. Example: add_filter('wpseo_title', 'add_to_page_titles'); function add_to_page_titles($title) { $title .= $addToTitle; return $title; }


6

Add this code to your functions.php if ( !function_exists( 'yourtheme_setup' ) ) { function yourtheme_setup() { /* * Let WordPress manage the document title. * By adding theme support, we declare that this theme does not use a * hard-coded <title> tag in the document head, and expect WordPress to * ...


Only top voted, non community-wiki answers of a minimum length are eligible