admin管理员组

文章数量:1323330

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

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

Share Improve this question edited Sep 3, 2020 at 10:11 Aditya Agarwal 2764 silver badges22 bronze badges asked Sep 2, 2020 at 12:20 OlivierOlivier 196 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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

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.

本文标签: i have create shortcode that work but not perfect coding