How to limit characters in widget titles

How to limit characters in widget titles

Recently I had a request from a client to “make the titles in widget shorter”.

The thing that bummed me out was that the widget “recent posts” was a core file from wordpress. The problem with this was the fact that if you modify wordpress core file, at the next update, the file would get rewritten… So after some research on how to get this done, i’ve decided to take a break, as i couldn’d find a solution that woked. At a later time and with a fresh mind i took another aproach on this.

I tought that it must be some plugin that i can use in order to have a new widget with the same functions that the core file has, and to write that down. I found Custom Recent Post Widget can do this for me and i was so happy. Now all i had to do was to find the files that i needed to edit and to write some code over there… and that is what i’ve done.

Here is an example of how i did just that

1. Install and activate Custom Recent Post Widget
2. With your FTP client search inside the plugin folder search for includes>crpw-cat.php and open this file
3. Now you shoud find the class crpw-item, and inside that search for the tag (about line 52) like in the code below

1
2
<br />
<!--?php the_title(); ?--><br />

4. Replace the code above with this one

1
2
<br />
<a class="crpw-title" title="Permanent link to <?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>" rel="bookmark"><!--?php if ( get_the_title() ) { $t = get_the_title(); $t = substr($t, 0, 40); /// if this doesn't work } echo mb_strimwidth(get_the_title(), 0, 50, '...'); ?--></a><br />

the next text