0

How to remove sanitize from edit_user_profile action?

I have a possibility to change user_nicename with the following code:

function ex_insert_nicename_input( $user ) {
    $content = ob_get_clean();

    // Find the proper class, try to be future proof
    $regex = '/<tr(.*)class="(.*)\buser-user-login-wrap\b(.*)"(.*)>([\s\S]*?)<\/tr>/';

    // HTML code of the table row
    $nicename_row = sprintf(
        '<tr class="user-user-nicename-wrap"><th><label for="user_nicename">%1$s</label></th><td><input type="text" name="user_nicename" id="user_nicename" value="%2$s" class="regular-text" />' . "\n" . '<span class="description">%3$s</span></td></tr>',
        esc_html__( 'Name' ),
        esc_attr( $user->user_nicename ),
        esc_html__( 'Unique' )
    );

    // Insert the row in the content
    echo preg_replace( $regex, '\0' . $nicename_row, $content );
}

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

If I enter "Ab" and updeate user info it will save it as "ab". Is it possible to filter this process and sanitize text, but without transform letters from uppercase to lowercase?

enter image description here enter image description here

The only thing I need to do is to remove or change this line $user_nicename = sanitize_title( $user_nicename ); in user.php. But I don't know how to do it correct, without changind core files...

enter image description here

1 Answer 1

1

The title is being converted to lowercase by filter sanitize_title

// in wp-includes/default-filters.php
add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 );

// it means => calling sanitize_title_with_dashes() in wp-includes/formatting.php

Here is explanation to what can do, recommended and not recommended method

Not Recommended

You may remove this filter but not recommmended Because this filter is for converting title with dashes, you may go to see the source code for reference.

// in theme's php
remove_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10 );

Recommended

add filter 'sanitize_title' with custom checking without affecting the original features

// load later than the original filters
add_filter( 'sanitize_title', 'ws365107_custom_sanitize_title', 15, 3 );
function ws365107_custom_sanitize_title( $title, $raw_title, $context ) {

    // may make use of the $_POST object data to check if it adding 

    // for original design $context = 'save' could distinguish, however, theme developers usually don't place this $context argument, so it render the argument useless
    if( $context === 'save' ) {     
    }

    // check by action if action = createuser from user-new.php, avoid affecting other situation
    // please adjust if use in other situations
    if( isset( $_REQUEST ) && isset($_REQUEST['action']) ) {
        // custom logic in checking user name using $raw_title
        $title = $raw_title;

        return $title;
    }

    // return original title for other situations
    return $title;
}

Extended reading:

1
  • Great job! Thank's for and example of usage! Commented May 4, 2020 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.