• Resolved tech techno

    (@asifshaikhtsn)


    i m using these code to hide extra field from profile page but these code will apply this setting for All Roles but i need to exclude admin and superadmin from this code.

    can someplease please customize this code so that these extra field will hide only for other roles user not for admin

    function remove_extra_field_profile()
    {
    
        $current_file_url =  preg_replace( "#\?.*#" , "" , basename( $_SERVER['REQUEST_URI'] ) );
    
        if( $current_file_url == "profile.php" )
        {
            add_action( 'wp_loaded', function(){ ob_start("profile_callback"); } );
            add_action( 'shutdown', function(){ ob_end_flush(); } );
        }
    }
    add_action( 'init', 'remove_extra_field_profile' );
    
    function profile_callback( $html )
    {
        $profile_dom = new DOMDocument;
        $profile_dom->loadHTML( $html );
    
        $all_lines = $profile_dom->getElementsByTagname( 'tr' );
    
        $excludes = array(
            'user-rich-editing-wrap',
            'user-admin-color-wrap',
            'user-comment-shortcuts-wrap',
            'show-admin-bar user-admin-bar-front-wrap',
            'user-url-wrap',
    		'user-behance-wrap',
    		'user-nickname-wrap',
    		'user-display-name-wrap',
            'user-description-wrap'
            );
    
        $deletes = array();
    
        foreach ( $all_lines as $line ) 
        {
            $tr_calss = $line->getAttribute("class");
    
            if( in_array( $tr_calss, $excludes ) )
            {
                $deletes[] = $line;
            }
        }
    
        $deletes[] = $profile_dom->getElementsByTagname( 'h2' )->item(0);
    
        foreach ($deletes as $delete) 
        {
            $delete->parentNode->removeChild( $delete );
        }
    
        return $profile_dom->saveHTML();
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • You seem to be checking the current URL to see if it’s the profile page. At that point, you may want to check check if the user is a (super) admin or not…

    if( ($current_file_url == "profile.php") && !current_user_can( 'manage_options' ) )
        {
            add_action( 'wp_loaded', function(){ ob_start("profile_callback"); } );
            add_action( 'shutdown', function(){ ob_end_flush(); } );
        }
    }

    See if that works?

    Thread Starter tech techno

    (@asifshaikhtsn)

    Thank you @webcreative it’s Worked

    Thread Starter tech techno

    (@asifshaikhtsn)

    Hey @webcreative Please Help me in this case also.i have these two moew code

    This one is for hiding some option in classic editor

    add_filter("mce_buttons", "tinymce_editor_buttons", 99); //targets the first line
    add_filter("mce_buttons_2", "tinymce_editor_buttons_second_row", 99); //targets the second line
    
    function tinymce_editor_buttons($buttons) {
    return array(
        "undo", 
        "redo", 
        "separator",
        "bold", 
        "italic", 
        "underline", 
        "strikethrough", 
        //"separator",
        //"bullist", 
        //"separator",
        //add more here...
        );
    }
    
    function tinymce_editor_buttons_second_row($buttons) {
       //return an empty array to remove this line
        return array();
    }
    
     function my_editor_settings($settings) {
        $settings['quicktags'] = false;
        return $settings;
        }
    
        add_filter('wp_editor_settings', 'my_editor_settings');

    and this one is for hiding post slug option while writing post

    function hide_all_slugs() {
    global $post;
    $hide_slugs = "<style type=\"text/css\"> #slugdiv, #edit-slug-box { display: none; }</style>";
    print($hide_slugs);
    }
     
    add_action( 'admin_head', 'hide_all_slugs'  );

    i use these line to exclude admin from these code but it’s not worked for me
    if ( !current_user_can( 'manage_options' ) )

    All these 2 code is working fine but these code is applying for all roles and i want to exclude admin role for these code..

    please tell me how to add any custom code with exclude admin role

    i use these line to exclude admin from these code but it’s not worked for me
    if ( !current_user_can( 'manage_options' ) )

    Let me see how you are using this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Code for Hiding Profile Fields’ is closed to new replies.