Limit the length of the Author Profile Biographical Text

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?

Read More

If it matters, I’m using <?php echo $curauth->user_description; ?> to grab the text.

Thanks.

Related posts

Leave a Reply

1 comment

  1. 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 verifies that we’re on the profile page by checking the title
    • It adds the max length attribute (arbitrarily set at 140) to the description text area

      (function($) {
          $(function() {
              // Verify that we're on the "Profile" page
              if($.trim($('h2').text()) === 'Profile') { 
      
                  // Add the max length attribute to the description field.
                  $('#description').attr('maxlength', 140);
      
              } // end if
          });
      })(jQuery);
      

    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:

    <?php
    
    /*
    Plugin Name: Profile Limitation
    Plugin URI: http://wordpress.stackexchange.com/questions/43322/limit-the-length-of-the-author-profile-biographical-text
    Description: This plugin limits the user profile description from exceeding 140 characters.
    Author: Tom McFarlin
    Version: 1.0
    Author URI: http://tom.mcfarl.in
    */
    
    ?>
    

    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:

    function profile_limitation_scripts() {
    
        wp_register_script('profile-limitation', plugin_dir_url(__FILE__) . '/plugin.js');
        wp_enqueue_script('profile-limitation');
    
    } // end custom_profile_scripts
    add_action('admin_enqueue_scripts', 'profile_limitation_scripts');
    

    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:

    function profile_description_length_limit() {
    
        global $current_user;
        get_currentuserinfo();
    
        if(strlen(($description = get_user_meta($current_user->ID, 'description', true))) > 140) {
            update_user_meta($current_user->ID, 'description', substr($description, 0, 140));
        } // end if
    
    } // end custom_profile_description
    add_action('profile_personal_options', 'profile_description_length_limit');
    

    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…