Sanitize textarea instead of input

I want to add a new field to the user registration. I’m using this tutorial by Paul Underwood.

The tutorial works but I want to switch the input text to a textarea which I do know how to do. The problem is making sure WordPress saves this information correctly.

I realized it’s because of sanitize_text_field( $_POST['facebook_profile'] )

I’m trying to find a way to accomplish the same thing for textarea instead of text field but I’m not finding anything (that I understand. I’m new as a PHP developer). How can I make it so that the textarea information is saved?

Here is the exact code I’m using:

add_action( 'show_user_profile', 'add_extra_social_links' );
add_action( 'edit_user_profile', 'add_extra_social_links' );

function add_extra_social_links( $user )
{
    ?>
        <h3>Additional Information</h3>

        <table class="form-table">
            <tr>
                <th><label for="facebook_profile">Facebook Profile</label></th>
                <td><input type="text" name="facebook_profile" value="<?php echo esc_attr(get_the_author_meta( 'facebook_profile', $user->ID )); ?>" class="regular-text" /><BR><span class="description">Please enter your Twitter username.</span></td>
            </tr>
        </table>
    <?php
}
get_the_author_meta( $field, $userID ); 
update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );

add_action( 'personal_options_update', 'save_extra_social_links' );
add_action( 'edit_user_profile_update', 'save_extra_social_links' );

function save_extra_social_links( $user_id )
{
    update_user_meta( $user_id,'facebook_profile', sanitize_text_field( $_POST['facebook_profile'] ) );
} 

4 Answers
4

Since WordPress 4.7 there is sanitize_textarea_field().

Which does exactly as you want, from the Codex.

Sanitizes a multiline string from user input or from the database.

Leave a Comment