What it says on the tin: how can I limit the amount of text a user enters in the Biographical Info area of the User Profile?
With a function? Or, jQuery if there is no way with a function?
If it matters, I’m using <?php echo $curauth->user_description; ?>
to grab the text.
Thanks.
You can do this using both client-side and server-side validation. If you’re doing something on the client-side then you should duplicate the functionality on the server, too. Modern debugging tools make it far too easy to circumvent JavaScript functionality.
This questions lends itself well to begin solved as plugin, so I’m approaching it as such.
First, create a directory in your /wp-content/plugins directory named “profile-limitation.”
Next, create a JavaScript file that will contain a little bit of jQuery for constraining the length of the description field:
It adds the max length attribute (arbitrarily set at 140) to the description text area
Save this file as “plugin.js.”
Next, open a new file. First, we need to add a header so that WordPress will read this as a plugin:
Secondly, we need to include the JavaScript source that we just created. We only want to do this in the dashboard, so we’re using the
admin_enqueue_scripts
function:After that, we need to perform some server-side validation so we’ll create a function that read’s the user’s description and, if it’s longer than 140 characters, will truncate it down to the first 140 characters of whatever they have stored.
Add this function directly below the one that we added above:
Note that we’re hooking into the
profile_personal_options
hook so that this fires when the “Profile” page is rendered.Save this file as plugin.php.
Next, hop over to the plugin menu in WordPress and look for “Profile Limitation.” Activate it, then navigate to your User Profile page. Attempt to edit the description – permitting you added all of your code correctly, profiles should no longer exceed 140 characters in length.
Functions used…