• I created a custom textarea field in the user info edit page, the problem is that I can get the point in this area. I cant display the text on different lines or leaving a blank line. This is possible on the contrary in the default textarea with author biographical info, so I’d like to have the same beheviour. My code:

     <textarea id="periodo_affitti" name="periodo_affitti"rows="5" cols="30" class="regular-text" ><?php echo esc_attr( get_the_author_meta( 'periodo_affitti', $user->ID ) ); ?></textarea><br />
                    <span class="periodo_affitti">periodo affitti</span>
    

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not sure I understand. You can line feed or add blank lines by pressing Enter one or more times, no? Perhaps do you mean when the saved meta is output with echo that any extra line feeds that were initially there are stripped out?

    If so the issue is in how the submitted form data is processed and saved. It’s not the textarea itself that’s the problem.

    Thread Starter sacconi

    (@sacconi)

    I can press enter and create a blank line, but when I save this blank line desappears, I mean even in /wp-admin/user-edit.php? not only where the text is displayed

    Moderator bcworkz

    (@bcworkz)

    What is the code for saving this added field? In an attempt to sanitize the field’s input, it’s likely stripping out line feeds.

    Thread Starter sacconi

    (@sacconi)

    
    // Save the extra fields
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    function my_save_extra_profile_fields( $user_id ) {
        if ( ! current_user_can( 'edit_user', $user_id ) ) {
            return false;
        }
        update_user_meta( $user_id, 'cell_two', sanitize_text_field( $_POST['cell_two'] ) );
        update_user_meta( $user_id, 'richiesta', sanitize_text_field( $_POST['richiesta'] ) );
    update_user_meta( $user_id, 'periodo_affitti', sanitize_text_field( $_POST['periodo_affitti'] ) );
        update_user_meta( $user_id, 'cell', sanitize_text_field( $_POST['cell'] ) );
        update_user_meta( $user_id, 'saldo', sanitize_text_field( $_POST['saldo'] ) );
    }
    
    Moderator bcworkz

    (@bcworkz)

    sanitize_text_field() will strip out line feeds. Textarea fields should be sanitized with sanitize_textarea_field()

    Thread Starter sacconi

    (@sacconi)

    Ok, with sanitize_textarea_field() blank lines remains visible in user-edit.php? after editing, but I cant see them in the output, I should do the last mile!

    Moderator bcworkz

    (@bcworkz)

    The line feeds are saved as actual line feed characters, ASCII code 10 and/or 13, depending on your O/S. Browsers don’t normally honor those characters, they are seen as a simple space character. You either need to convert these characters into <br> tags (with str_replace()) or output the saved value into a <pre> block which does honor line feeds. Pre(-formatted) tag content is often styled differently, which may or may not be what you want. You might need some more CSS to get the look you want.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Getting the point in a textarea custom field’ is closed to new replies.