0
votes

I'm customizing my theme right now. The Standard is, that the logged-in Users Avatar is shown in the Menu bar but I want to display a simple Icon there. I already wright the Code and everything works fine but the Code is located in the mains functions.php File. So in order to make the Site upgradeable, I need to embed the code in the child themes function.php. I found no help there so maybe here is someone who could help me!

Thanks a lot

Below is the Code which I need to embed in the functions.php File of my Child Theme:

add_action( 'init', 'gt3_get_page_id_from_menu' );

function gt3_user_avatar_out($avatar_size=80,$show_email=false){
    $user_id = get_current_user_id();
    $current_user = get_user_by( 'id', $user_id );
    echo "<i class='gt3_login_icon gt3_login_icon--avatar'>";
        echo '<i class="fa fa-user"></i>';
    echo "</i>";
    if ($show_email) {
        echo "<span class='gt3_login__user_email'>".esc_html( $current_user->user_email )."</span>";
    }
}
2
If you have created child theme, then put it in the child's functions.php. Is it anything wrong?Pawan Thakur
I then get a white Screen, I think its a Problem because the Script is executed twice...JulianLaibach

2 Answers

0
votes

If a function is declared in the parent's functions.php, declaring a function with the same name in the child theme will cause a fatal error in the child theme itself. I'd suggest doing something like this instead:

parent function .php

// This can become a filter if you'll need to manipulate the current user
function gt3_get_current_user() {
    return get_user_by( 'id', get_current_user_id() );
}

add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
    $html .= '<i class="gt3_login_icon gt3_login_icon--avatar"><i class="fa fa-user"></i></i>';
    if( $show_email ) {
        $html .= sprintf( '<span class="gt3_login__user_email">%s</span>', esc_html( gt3_get_current_user()->user_email ) );
    }

    return $html;
}, 10, 2 );

child theme's function .php

add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
    // $html here is the one that returned from the parent's theme. You can manipulate it or replace the whole output inside here. 

    return $html;
}, 10, 2 );

Example usages:

echo apply_filters( 'gt3/current_user_avatar', '', false ); // This will echo the icon without the email
echo apply_filters( 'gt3/current_user_avatar', '', true ); // This will echo the icon with the email
echo apply_filters( 'gt3/current_user_avatar', '<p>Append</p>', false ); // This will echo the icon without the email and the '<p>Append</p>' html string before everything else

The result will vary depending on where you use this filter. If you are in the child theme and you altered the html adding the said filter, the result will be customized for that theme. Remember that you can use the use statement to bring in external variables ina closure. For example:

$user_name = 'Mike';
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) use ( $user_name ) {
    $html .= sprintf( '<p>Hello, %s!</p>', $user_name ); 

    return $html;
}, 10, 2 );
// This will cheer the user, and it will be place at the end of the snippet.
0
votes

your code is not overwritten by adding a new functions.php file in the same folder, but do not copy/paste the code from the parent theme’s file, because it needs to remain separate from any modifications you make to the child theme. Instead, create a blank file or add any new .php functions required for your child theme