Single Product Title Post Author

#14154
  • Resolved Barry
    Rank Math pro

    Hello RankMath,

    The Single Product Title tag %name% is returning the post title instead of the author name.
    null

    I can confirm that the author is correctly set. Firstly, the %userid% tag correctly returns the author id. Secondly, you can view the product on the site here, where we have used function get_the_author() to display the author name.

    I would like to stress what we are seeking is the author display name and not the nicename referenced in your tooltip. Apparently , nicename is a slug form of the name and would not fit in a title description. Also, some often confuse nicename and nickname, but the sensible solution is neither of the aforementioned, but the display name as already stated.

    Should the nicename be appropriate elsewhere, then maybe a new tag of %author% or some such is needed?

    Thank you,
    Barry

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello Again,

    Don’t know what happened to the first screenshot, but here it is again.

    A preview of a ticket before posting would be helpful.

    Thank you,
    Barry

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    Please note that this variable is designed to work for single posts and not product pages. I can suggest that you create a custom title or description of the product pages by making use of the following filters:

    /**
     * Filter to change the page title.
     * 
     * @param string $title
     */
    add_filter( 'rank_math/frontend/title', function( $title ) {
    	return $title;
    });
    
    /**
     * Allow changing the meta description sentence from within the theme.
     *
     * @param string $description The description sentence.
     */
    add_filter( 'rank_math/frontend/description', function( $description ) {
    	return $description;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Hello Michael,

    Thank you for your suggestion and in the absence of a better alternative, this is what I’ve done:

    /**
    * Filter to change the page title.
     * 
     * @param string $title
     */
    add_filter( 'rank_math/frontend/title', function( $title ) {
    
    	global $post;
    	
    	$title_len = strlen($title);
    	$bloginfo = get_bloginfo();
    	$bloginfo_len = strlen($bloginfo);
    	
    	$author_name = get_the_author_meta( 'display_name', $post->post_author);
    	$title = substr($title, 0, $title_len - ($bloginfo_len + 3)) . ' by ' . $author_name . substr($title, -1 * $bloginfo_len - 3);
    	return $title;
    });
    

    This does achieve the correct end result, that the title in the structured data is as we want for our book products, however there are shortcomings in this solution, as follows:

    1. This does not show the title correctly in the backend preview of the snippet.
    2. The SEO score for the title in the backend is consequently too low.
    3. I conjecture, in consequence, this will discourage us from displaying our SEO score.
    4. We aim to have our book authors configure their own products and this solution will confuse them unnecessarily.
    5. As a novice PHP and WordPress internals functionary this was frustrating, because I would like to have split the $title at the separator character. Apparently, this can be fetched using WPSEO_Utils::get_title_separator(), but on trying that my functions.php couldn’t find the class, so rather than waste more time I used the kludge shown.

    All these shortcomings mentioned would be overcome by having an %author% tag as suggested. Once more, I observe that, %userid% correctly returns the author id. Further, the author display name is readily available as I’ve shown. Therefore I suggest an %author% tag is a highly desirable enhancement.

    Best regards,
    Barry

    Hello,

    Thank you for contacting Rank Math today.

    I am glad that you were able to resolve this, here is another snippet that you can try to overcome the shortcomings by replacing the usage of %name% with your custom value:

    /**
     * Filter to replace %name% with custom value.
     */
    add_filter( 'rank_math/replacements', function( $replacements ) {
        if ( is_singular() && isset( $replacements['%name%'] ) ) {
            global $post;
            $author_name = get_the_author_meta( 'display_name', $post->post_author);
            $replacements['%name%'] = $author_name;
        }
        return $replacements;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello Michael,

    I thought we were on a winner here and was anxious to give congratulations, but regrettably, at this point, no.

    While the replacement of %name% has effect in SEO Titles and Meta/Products as shown in this screenshot, it does not carry through to the products, as shown here, where it is actually needed.

    This test is on localhost, so no caching beyond the browser is involved. I have cleared the browser cache. I tried testing the tag directly on the product, but %name% there is still returning the post title. I have also tried a new product, but as expected, %name% is still returning the title.

    Do we need a different filter, another array or what?

    Thank you for your suggestions so far,
    Barry

    Hi Barry,

    Thanks for the response.

    I have tested the %name% variable on my end and even though the backend preview is not correct, the right titles are displayed on the frontend.
    Please check to confirm this from your end by inspecting the page source.

    Let me know how this goes. Thank you.

    Barry
    Rank Math pro

    Hi Michael,

    Again I will stress the points 1, 2 and particularly 4 that I made in my list of shortcomings above. So, at this stage, the initial frontend solution implemented is the better and less confusing option. We’ll just have to tell our authors (presently few, but we aim for thousands over time) don’t worry, your name will appear in the structured data title. So, for me regrettably, it is pointless to follow your suggestion to check the results of this replacements change as we clearly will not be using that.

    Again, can I urge Rank Math to include a tag for the author’s display name usable for WooCommerce products. Also, again, observing that the %userid% tag does return the author id, so surely the plugin is halfway there?

    With the frontend solution, I’d overlooked to restrict it’s scope, so the currently implemented code is:

    
    /**
    * Filter to change the page title.
     * 
     * @param string $title
     */
    add_filter( 'rank_math/frontend/title', function( $title ) {
    
    	global $post;
    	
    	// Detect if it is a single post with a post author of custom type 'product'
    	if ( is_single() && isset( $post->post_author ) && (get_post_type() == 'product')) {
    		$title_len = strlen($title);
    		$bloginfo = get_bloginfo();
    		$bloginfo_len = strlen($bloginfo);
    	
    		$author_name = get_the_author_meta( 'display_name', $post->post_author);
    		$title = substr($title, 0, $title_len - ($bloginfo_len + 3)) . ' by ' . $author_name . substr($title, -1 * $bloginfo_len - 3);
    	}
    	
    	return $title;
    }); 
    

    Best regards,
    Barry

    HI Barry

    Thanks for the suggestion 🙂

    I will pass this on to the team.

    Feel free to contact us for any other questions, comments or suggestions. Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)

The ticket ‘Single Product Title Post Author’ is closed to new replies.