Saturday, September 21, 2024
HomeWordPress DevelopmentProfile Picture displaying on Unsuitable aspect of Wordpress Navigation Menu

Profile Picture displaying on Unsuitable aspect of WordPress Navigation Menu


Am making a WordPress website whereby am enabling customers to add their very own {custom} profile photographs from a {custom} kind. As soon as they add am storing the picture URL in a database desk and the picture is then saved contained in the WordPress wp-content/uploads folder. This works wonderful.

When a consumer logs in, I wish to show the profile picture subsequent to the username on the top-left nook of the dashboard.

To realize this I’ve utilized the init motion hook that fetches the picture URL from the database and shops it in a wp_cache.

operate fectch_custom_profile_picture() {


    if ( is_user_logged_in() ) {

        world $wpdb;
        $user_id = get_current_user_id(); 
        $table_name  = "profile_details";
    
    //Question desk to fetch picture Url
        $query_result = $wpdb->get_results("SELECT * FROM  $table_name WHERE user_id =  $user_id");
    
    //Retailer picture url in cache
        wp_cache_set( 'profileImageUrl', $query_result[0]->avatarUrl);
    } 
}
add_action( 'init', 'fectch_custom_profile_picture' );

Subsequent, am using nav_menu_item_title to append the picture subsequent to username, this shoudl be displayed immediately. Ideally it must be: Welcome check [profileimagehere]

operate add_image_beside_username($title, $menu_item , $args, $depth) {
    
    //Fetch the picture from wp_cache and retailer in a variable
    $img = '<img src="' . wp_cache_get( 'profileImageUrl' ) .'" class="custom-avatar">';
     
    //Final Member logged in particulars
    $user_id = get_current_user_id();
    um_fetch_user( $user_id );
    $display_name = um_user('display_name');

     //Examine if title is the same as: Welcome {username}, if true appand the profile picture subsequent to it
    if($title == 'Welcome ' .  $display_name){
        $title .=  print_r($img);
     }
     return $title;
}
add_filter('nav_menu_item_title', 'add_image_beside_username', 10, 4);

The picture shows on the display screen nevertheless it shows on the flawed aspect. See under.

enter image description here

I need it to show on the far-left nook after Welcome check.

Researched this for hours however not profitable, Please help?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments