Limit User Bio (description) length

261 views
Skip to first unread message

Patricio G. Espinoza

unread,
Nov 2, 2011, 9:29:58 PM11/2/11
to wordpres...@googlegroups.com
Greetings, Is there a way to limit user description words? Like for example... to no more than 50 . Either within WP or a plugin...? I searched but couldn't find much...  Any feedback is greatly appreciated.  I user an author box, but need to limit user to the amount or words they typed in the WP profile.

Bill Erickson

unread,
Nov 2, 2011, 10:21:49 PM11/2/11
to wordpres...@googlegroups.com
That's a really interesting question. I've attempted it, but it's not working yet: https://gist.github.com/1335597#comments

I've basically hooked into the two functions that run when a profile is edited (either by the individual or by someone else). If the description is longer than 50 characters, I trim it down and then update the user meta.

For some reason update_user_meta isn't working here. Hopefully some other people will be able to figure out what I did wrong and comment on that gist.


---
Bill Erickson
WordPress Consultant
http://www.billerickson.net


On Wed, Nov 2, 2011 at 8:29 PM, Patricio G. Espinoza <patricio....@gmail.com> wrote:
Greetings, Is there a way to limit user description words? Like for example... to no more than 50 . Either within WP or a plugin...? I searched but couldn't find much...  Any feedback is greatly appreciated.  I user an author box, but need to limit user to the amount or words they typed in the WP profile.

--
You received this message because you are subscribed to the Google Groups "WordPress Austin" group.
To post to this group, send email to wordpres...@googlegroups.com
To unsubscribe from this group, send email to wordpress-aust...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/wordpress-austin?hl=en
 
Our meeting information is available at http://www.meetup.com/austinwordpress/

Paul Menard

unread,
Nov 2, 2011, 10:46:35 PM11/2/11
to wordpres...@googlegroups.com
Bill,

I think you are calling the hook too early or in other words the wrong hook. If you look at /wp-admin/user-edit.php line 104-107 you see the following line:

if ( IS_PROFILE_PAGE )
do_action('personal_options_update', $user_id);
else
do_action('edit_user_profile_update', $user_id);

These are the actions you are intercepting in your gist. The problem is if you keep reading the code past that point to about line 129 where the function 'edit_user()' is called. Inside this function is more logic and either 'wp_update_user()' or 'wp_insert_user()' is called. The 'wp_update_user()' actually in turn calls 'wp_insert_user()' with the prepared data structure of the user information. 

In my opinion the more correct hook to be called is within wp_insert_user() line 1444 you will see
$description = apply_filters('pre_user_description', $description);

So basically you would trim your function to be something like the following:


function be_limit_description($description)
{
if ( strlen( $description ) > $max_characters ) {

/** Truncate $text to $max_characters + 1 */
$description = substr( $description, 0, $max_characters + 1 );

/** Truncate to the last space in the truncated string */
$description = trim( substr( $description, 0, strrpos( $description, ' ' ) ) );
}
return $description; // Return the trimmed description to wp_insert_user();
}
add_filter('pre_user_description', 'function be_limit_description');

Paul Menard

unread,
Nov 2, 2011, 10:49:06 PM11/2/11
to Paul Menard, wordpres...@googlegroups.com
Sorry had a typo in my add_filter statement. Should be:

add_filter('pre_user_description', 'be_limit_description');

P-

Bill Erickson

unread,
Nov 2, 2011, 11:03:22 PM11/2/11
to wordpres...@googlegroups.com, Paul Menard
Ah that did it! I've updated the code on the gist.

Although I still don't understand why the other code didn't work. It shouldn't matter where I call the function - update_user_meta should always update the meta if the value you provide is different than that which is stored. 



---
Bill Erickson
WordPress Consultant
http://www.billerickson.net


Pat Ramsey

unread,
Nov 2, 2011, 11:38:49 PM11/2/11
to wordpres...@googlegroups.com
I love this list. :-)

Patricio, does this answer your question?

Cheers!

Pat

espiMedia

unread,
Nov 3, 2011, 12:44:02 PM11/3/11
to WordPress Austin
Paul, I think it does, thank you and Bill very much. I will try it
and report back. Just to make sure -since I'm a wknd warrior :) on a
volunteer project- the isntructions above indicate I need to put this
code within "wp_insert_user() line 1444" ?

----from above Paul's response, thank you -----

in my opinion the more correct hook to be called is within
wp_insert_user() line 1444 you will see
$description = apply_filters('pre_user_description',
$description);


((( above code goes here )))

----- end-----

** I will try the above. I do have however an additional question...
what I was hoping for is WP telling the user (when they try saving
their profile) " the text is too long, max allow 50 words) sor ot like
forms do when filled out on line. This is probably much involved...
however. Any plugins out there?


On Nov 2, 11:38 pm, Pat Ramsey <ramsey....@gmail.com> wrote:
> I love this list. :-)
>
> Patricio, does this answer your question?
>
> Cheers!
>
> Pat
>
> On Nov 2, 2011, at 10:03 PM, Bill Erickson wrote:
>
> > Ah that did it! I've updated the code on the gist.
>
> > Although I still don't understand why the other code didn't work. It shouldn't matter where I call the function - update_user_meta should always update the meta if the value you provide is different than that which is stored.
>
> > ---
> > Bill Erickson
> > WordPress Consultant
> >http://www.billerickson.net
>

Pat Ramsey

unread,
Nov 3, 2011, 12:47:16 PM11/3/11
to wordpres...@googlegroups.com
The code Bill wrote up needs to go in your theme's functions.php file. Don't edit anything inside the wp-includes folder.

Cheers!

Pat

Reply all
Reply to author
Forward
0 new messages