Write a WordPress code snippet to limit every users Biographical info in the user's profile page to be 140 characters.

Home » Snippets » Write a WordPress code snippet to limit every users Biographical info in the user's profile page to be 140 characters.
0

Created with:

Visibility: 

public

Creator: Nick

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: WP Turbo Plugin
 * Description: Limits user's biographical info to 140 characters.
 * Version: 1.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 * Text Domain: wpturbo
 */

/**
 * Limit user's biographical info to 140 characters.
 *
 * @param int $user_id The ID of the user being updated.
 */
function wpturbo_limit_biographical_info( $user_id ) {
    // Verify the current user has the capability to update user meta
    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return;
    }

    $bio = get_user_meta( $user_id, 'description', true );

    if ( is_string( $bio ) && strlen( $bio ) > 140 ) {
        $bio = substr( $bio, 0, 140 );
        $bio = sanitize_text_field( $bio ); // Sanitize the biographical info

        // Update user meta and handle any errors
        if ( false === update_user_meta( $user_id, 'description', $bio ) ) {
            error_log( 'Failed to update user meta for user ID: ' . $user_id );
        }
    }
}
add_action( 'personal_options_update', 'wpturbo_limit_biographical_info' );
add_action( 'edit_user_profile_update', 'wpturbo_limit_biographical_info' );
				

By implementing these security measures, you can further protect the code and ensure the safe handling of the biographical info.

Register an account to save your snippets or go Pro to get more features.