i have create shortcode that work but not perfect coding

So, there are 2 wrong things.

First of all, your shortcode itself is wrong.

You used

[check-if-equal usermeta="firstname" uservalue="Jeff"] Yes [/check-if-equal]

but there is nothing like firstname in WordPress, It’s first_name

So your shortcode should look like this,

[check-if-equal usermeta="first_name" uservalue="Jeff"] Yes [/check-if-equal]

Once that is done, rest looks cool I edited the question itself, for formatting syntax correctly. So it should look like this:


<?php
function func_check_if_equal( $atts, $content = null ) { 
if ( is_user_logged_in() ) { /* check if logged in */

    $user_meta = $atts['usermeta'];
    $user_value = $atts['uservalue'];


    /* get value from shortcode parameter */
    $user_id = get_current_user_id(); /* get user id */
    $user_data = get_userdata( $user_id ); /* get user meta */
    if ( $user_data->$user_meta == $user_value ) { /* if user meta is equal meta value */
        return $content; /* show content from shortcode */
    } else {
        return ''; /* meta field don't equal */ }
    } else {
        return ''; /* user is not logged in */
    }
}
add_shortcode( 'check-if-equal', 'func_check_if_equal' );

You asked me to Improve the code also, so there are 2 things which can be improved.

It should work perfectly, but there is one first thing u missed is that It can be that someone used the shortcode and types “jeff” instead of “Jeff” or may be they write “JEFF” instead of “Jeff”. This can be problematic, as they type the same name, but still condition won’t match, so below I have first of all made it work with all capital small variations, and then removed unnecessary else.

Secondly you unnecessarily cluttered the else statements, it could be done in simpler way. Both things corrected.

function func_check_if_equal( $atts, $content = null ) { 
    if ( is_user_logged_in() ) { /* check if logged in */

        $user_meta = $atts['usermeta'];
        $user_value = $atts['uservalue'];


        /* get value from shortcode parameter */
        $user_id = get_current_user_id(); /* get user id */
        $user_data = get_userdata( $user_id ); /* get user meta */
        if ( strtolower($user_data->$user_meta) == strtolower($user_value) ) { /* if user meta is equal meta value */
            return $content; /* show content from shortcode */
        }
    }
return '';
}
add_shortcode( 'check-if-equal', 'func_check_if_equal' );