i have create shortcode that work but not perfect coding

I want to create a shortcode where content is displayed if user meta is equal to a value

It Works but it’s code isn’t perfect

how would you have done? How Can I improve it?

Example content display if firstname user is Jeff

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

This is the code handling the above shortcode

<?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' );

Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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' );

Method 2

You could clean up the code a bit, but nothing you did was that bad.

function prfx_conditional_user_content( $attributes, $content = null ) {

    if( is_user_logged_in() ) {
        $meta = $attributes['meta'];
        $value = $attributes['value'];
        $current_user_meta = get_userdata( get_current_user_id() );

        if( $current_user_meta->$meta === $value ) {
            return $content;
        }
    }

    return '';

}

add_shortcode( 'conditional-content', 'prfx_conditional_user_content' );

Edit: As Aditya pointed out, firstname isn’t a valid property. The correct one is first_name. Here is a list of properties available to you by the WP User object.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x