If user_meta is equal to X then show a div - php

On my site when a user is registering there is an option to pick from 3 values (House, car, boat). Once the user has registered and viewing their profile, I want to show a button on the front-end based on what meta value they have selected when registering. If they have no meta key, then nothing is shown.
Here is my code attempt, but does not work!
<?php global $current_user;
get_currentuserinfo(); //wordpress global variable to fetch logged in user info
$userID = $current_user->ID; //logged in user's ID
$havemeta1 = get_user_meta($userID,'house',true); //stores the value of logged in user's meta data for 'house'
$havemeta2 = get_user_meta($userID,'car',true); //stores the value of logged in user's meta data for 'car'
$havemeta3 = get_user_meta($userID,'boat',true); //stores the value of logged in user's meta data for 'boat'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div class="Button3"></div>
<?php } else { ?>
<div></div>
<?php }?>
In short - The expected result is, if the user has choose House when registering, then show the DIV with the class=Button1 etc etc
Update:
This code above now works!

Related

ACF Checkbox in User Profile - can't get the values - Wordpress

I've created an ACF Checkbox field that I assigned to the User Profile area.
I'm using a form in order to bring up a popup with the checkboxes where users choose from the available options, the form then saves and updates the choices in user's profile. That all seems to be working perfectly fine.
But now I need to show the selected choices in User Profile template using a shortcode.
My ACF return value is set to value and I'm using the snippet exactly as per ACF's docs in order to display a list of labels.
This is my shortcode:
function get_user_profile_choices() {
$field = get_field_object('color_choices');
$colors = $field['value'];
// Display labels.
if( $colors ): ?>
<ul>
<?php foreach( $colors as $color ): ?>
<li><?php echo $field['choices'][ $color ]; ?></li>
<?php endforeach; ?>
</ul>
<?php endif;
}
add_shortcode( 'user-profile', 'get_user_profile_choices' );
I am currently just testing this with my own admin account, so I'm trying to show my choices in my profile.
I'm not getting anything. Could it be because the checkbox is in User Profile? Do I need to declare any user variable? Any ideas?
Check this out. This way you will get the value for the currently logged-in user.
function get_user_profile_choices() {
$user_id = get_current_user_id();
$field = get_field_object('color_choices', 'user_'.$user_id);
}
add_shortcode( 'user-profile', 'get_user_profile_choices' );
Add second parameter "user_USERID" in the function "get_field_object" in order to get value from the user profile fields.
change "1" to your user ID
$user_id = 1;
$field = get_field_object('color_choices', 'user_' . $user_id);

Wordpress - how to show/hide content in page

In Wordpress page text and other elements when added from admin, hidden for guests. When user is logged, private content showed for this user. This function created with plugin wp-private. I need get balance for user from other site api, if balance < n , should hide all content for logged users and showing other content . If balance > n , should show all content for logged users. For this I used this code:
wp-content/themes/currentTheme/page.php:
<?php if(is_page("id_page")):
$user_id = get_current_user_id();
$balance = get_balance($user_id);
if($balance <= 0){
remove_post_type_support( 'post', 'editor' );
echo "<a href='pay.php'>Pay</a>";
}
endif;
?>
After that still leaves the content page. What to do to not show page content?

Refine If Else statement - or - end - Wordpress / Php

I was wondering if there is a way of refining my If Else statement in the following code:
<?php if ( ms_has_membership(01) ): ?>
// HTML - USER PROFILE
<?php global $current_user;
get_currentuserinfo();
elseif (is_user_logged_in() && $current_user->ID == $post->post_author): ?>
// HTML - USER PROFILE - (Same as above)
<?php else: ?>
// LINK - Upgrade Your Subscription
<?php endif; ?>
Basically, I need to say that if the user has a required membership ( in the first statement, then user profiles will be visible.
If the user doesn't have a required membership, but is logged in and is visiting his own profile ( is the author ), then only his own profile will be accessible.
And If the user is not logged in, nor is author, nor has a valid membership, no profiles will be visible, instead a link to upgrade subscription will show up.
My problem is that the HTML of the user profile is pretty long and I don't want to repeat the whole thing again in the second statement.
So is there a way to refine the statement, so it reads:
If the user is logged in AND has a valid membership OR is and author, then display the profile?
Could I use || and && in one single statement??
As you said:- So is there a way to refine the statement, so it reads: If the user is logged in AND has a valid membership OR is and author, then display the profile?
You can do it in following way:-
<?php
global $current_user;
get_currentuserinfo();
?>
<?php if ( is_user_logged_in() && (ms_has_membership(01) || $current_user->ID == $post->post_author) ): ?>
// HTML - USER PROFILE
<?php else: ?>
// LINK - Upgrade Your Subscription
<?php endif; ?>

avatar is not displaying in custom template

I have created a custom profile page in which I want to show profile picture. I use the below code to show the avatar:
<div class="activity-avatar">
<?php bp_activity_avatar(); ?>
</div>
<?php bp_displayed_user_avatar( 'type=full' ); ?>
<?php global $userdata; get_currentuserinfo();
echo get_avatar($userdata->ID, 46 ); ?>
But I don't get the avatar of user. If I use <?php bp_activity_avatar(); ?> outside of the main <div> then it shows the avatar of the last updated profile picture, not of the current user. But I don't get Avatar at the needed position. How can I get avatar at any position in template? I use bootstrap classes, could the problem occur from there?
Try:
$userid = bp_loggedin_user_id();
echo bp_core_fetch_avatar( array( 'item_id' => $userid) );
You don't explain the context of your custom profile page, so $userid might need to be changed to $userid = bp_displayed_user_id() or however you determine the user id for a page view.

How to check that if current user (ID) has posts or not wordpress

I have a Custom template. In it i want to check that if the current logged in user has custom posts named (student_form) or not if posts equals true i want to redirect it to some page other wise i want to show him/her a form which i created using wp fronted user.
Following code i wrote but its not redirecting to the desired Page if user has custom posts .
<?php
/*
* Template Name: Form Page
*/
get_header();
global $post,$current_user;
get_currentuserinfo();
if ($post->post_author == $current_user->ID) {
exit( wp_redirect(http://MYurl/dashboard/') );
}
else
{
echo do_shortcode('[wpuf_form id="414"]');
}
wp_reset_query(); // Restore global post data stomped by the_post().
get_footer();
?>

Categories