Adding HTML code on Woocommerce product title

Hi There,

My Woocommerce product titles are longer so I wanted to make them in multiple lines and I have used this “
” in between the title like this: http://prntscr.com/j0gl2l

Now, is it allowed by woocommerce or ThemeX or what’s the best way? Because, I have made all of my product titles into multiple lines using the
but repeatedly that html line breaking tag
is removing automatically from some or many or even all products just after a day around.

What could be the reason or solution for this? I want my product titles into multiple lines with line brakes on certain points, not randomly.

Thanks

Hello @alessandrorussino,

Thanks for asking. :slight_smile:

You can take a look at the solution mentioned in following Stackoverflow thread.

Thanks.

Hi Prasant,

Thanks for your reply, but the thread you’ve attached isn’t relevant. There explained just how to replace a tag/code in the titles, where, I already can make my lines break down into multiple lines using this tag “
” but the problem is, after I add this on my product titles, later in a couple of days, that tag removes automatically and the title became single line again which is weird and I need help on that.

Even if there’s any best option or way to break the titles into multiple lines, then please provide, the link you gave me doesn’t have a good or bad option, that just shows how to replace a line breaking tag in the php file which is irrelevant.

Please check my screenshot and try to understand my request or let me know if you need anything from me to proceed further.

Thanks

Hi alessandrorussino,

I believe Prasant’s suggestion could be helpful in your case, you can just add | character in the exact position you want to add a line break between your titles words like this My Product Title | Second line then add this function in your child theme functions.php file:

add_filter( 'the_title', 'custom_the_title', 10, 2 );
function custom_the_title( $title, $post_id ){
    $post_type = get_post_field( 'post_type', $post_id, true );
    if( $post_type == 'product' || $post_type == 'product_variation' ){
        $title = str_replace( '|', '<br/>', $title );
    }
    return $title;
}

This code snippet will automatically replace this character | that you entered while writing your product title in the backed with <br/> on the front end.

I hope this helps.
Thanks.

Hi Alaa,

Thanks for the code snippet. I understood the matter, but my concern is not only this, it’s all about, after I use a “br” then it automatically removes in a couple of days later, then only by replacing the “br/” with a “|” pipe, will that solve my automatic removing issue?

Please let me know and I hope I was able to explain my concern and problem I’m having.

Hi alessandrorussino,

Yes, I understand your point, thanks for clarifying that.

Using the direct HTML <br/> tag in the product title field is not the same as using a normal character like the pipe | one. Some plugins or even WordPress itself might interfere with these tags added in the title directly, but this will not happen if you used a normal character and got it replaced with the <br/> tag just before rendering the content on the frontend.

Give it a try and it should work flawlessly.

Thanks.

Hi Alaa,

Thanks for your reply on my actual issue this time. The pipe line tag worked as a line break and I have tested on two products to see how it goes after a couple of days. I’ll wait a few days and see, if it doesn’t removes automatically anymore, then we got the solution, otherwise, we would need to find an alternative solution.

Hope, this will fix my issue.

No problem. Please let us know if the problem comes back again.
If you need anything else we can help you with, don’t hesitate to open another thread.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.