php echo wordpress user meta data - php

On my site when a user is registering there is an option to pick from an additional option that pears their account up with a non-profit. Once the user has registered and viewing specific pages of the site I want the site to be tailored to them using php that grabs their meta info. For this I will echo a button that tailors 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,'nch',true); //stores the value of logged in user's meta data for 'National Coalition for the homeless'
$havemeta2 = get_user_meta($userID,'rotary-international',true); //stores the value of logged in user's meta data for 'Rotary International'
$havemeta3 = get_user_meta($userID,'khan-academy',true); //stores the value of logged in user's meta data for 'Khan Academy'
$havemeta4 = get_user_meta($userID,'wwf',true); //stores the value of logged in user's meta data for 'World Wildlife Fund (WWF)'
$havemeta5 = get_user_meta($userID,'bcrf',true); //stores the value of logged in user's meta data for 'The Breast Cancer Research Foundation'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div <p>nch</p> class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div <p>rotary-international</p>class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div <p>khan-academy</p>class="Button3"></div>
<?php } elseif ($havemeta4) { ?>
<div <p>wwf</p>class="Button4"></div>
<?php } elseif ($havemeta5) { ?>
<div <p>bcrf</p>class="Button5"></div>
<?php } else { ?>
<div><p>None - No Matching Affiliation</p></div>
<?php }?>
-----------------------New Code----------------------
This allows me to see what the affiliation variable is pulling for the user
The result is this: 'User Affiliation: khan-academy'
<?php global $current_user;
get_currentuserinfo();
echo 'User Affiliation: ' . $current_user->affiliation . "\n";
?>

can you pass the meta into a session var, ie $_SESSION['havemeta5'];

get_user_meta is set up wrong. Try this:
$havemeta1 = get_user_meta( $userID, 'nch', true );
...and so on. You need to pass the $userID as the first parameter of get_user_meta() - not $affiliation.
update
If your user meta is 'affiliation' (which allows you to call $current_user->affiliation), you can do something like this:
<?php
global $current_user;
get_currentuserinfo();
$userID = $current_user->ID;
$affiliation = get_user_meta( $userID, 'affiliation', false );
$affiliation = $affiliation[0];
if( 'nch' == $affiliation ){
print '<div class="Button1"><p>nch</p></div>';
} elseif( 'rotary-international' == $affiliation ){
print '<div class="Button2"><p>Rotary International</p></div>';
} elseif( 'khan-academy' == $affiliation ){
print '<div class="Button3"><p>Khan Academy</p></div>';
} elseif( 'wwf' == $affiliation ){
print '<div class="Button4"><p>wwf</p></div>';
} elseif( 'bcrf' == $affiliation ){
print '<div class="Button5"><p>bcrf</p></div>';
} else {
print '<div><p>None - no matching affiliation</p></div>';
print '<div>User ID: '.$userID.', Affiliation: '.$affiliation.'</div>';
}

Related

Is there a specific way to read POST data from wpforms?

I'm trying to access form data entered by the user on a form created with wpforms and save them as session variables, but no matter what I try, I can't seem to get anything from the form. I can hardcode the information into the session variables, and they work as expected, but it needs to pull the data from the form.
Here's the snippet from the php code I have on the page:
<?php
echo session_id();
$_SESSION['age'] = $_POST['wpforms[fields][3]'];
$_SESSION['gender'] = $_POST['wpforms[fields][2]'];
?>
I've also included some code from the second page once the form has been submitted, which is where I see that the session variables are set correctly when I hard code them in the first page, but not set when I try to pull from the form.
echo session_id();
if ( isset($_SESSION['gender'])) {
echo "Gender is set.<br>";
}
else {
echo "Gender not set<br>";
}
if ( isset($_SESSION['age'])) {
echo "Age is set.<br>";
}
else {
echo "Age not set.<br>";
}
echo $_SESSION['gender'];
echo $_SESSION['age'];
Any insight will be greatly appreciated! Thanks!
Screenshot of page inspection
I figured it out. I had to add the following code to my functions.php file on the wordpress theme I was using:
add_action('init', 'register_my_session');
function register_my_session() {
if (!session_id() ) {
session_start();
}
function wpf_dev_process_entry_save( $fields, $entry, $form_id, $form_data ) {
// Only run on my form with ID = 4117
if ( absint( $form_data['id'] ) !== 4117 ) {
return $fields;
}
$_SESSION['age'] = $fields[3]['value']; // This is where we access the form data
$_SESSION['gender'] = $fields[2]['value'];
}
add_action( 'wpforms_process_entry_save', 'wpf_dev_process_entry_save', 10, 4 );
}

userbased content from session php

I'm looking to setup a group field for my custom user script. How can I go about only showing certain content to certain groups
Example:
admin (all content)
moderator (extended content)
user (user content)
guest (general overview)
4 group id's 0 - 4
4 banned
3 admin
2 moderator
1 user
0 guest
Example of the PHP in use
// Keep reminding the user this account is not active, until they activate
if($row['group_id'] == 4) { //display all
echo
'<div class="info">
This account is at risk of being banned Please obey the site rules.
</div>';
} else {
exit();
}
Example of the PHP MySQL session
/* Displays user information and some useful messages */
session_start();
// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: http://localhost/login-system/error.php");
}
else {
// Makes it easier to read
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
$group_id = $_SESSION['group_id'];
}
You can do this with this example:
Lets say in this is a blog.
<?php
// 0 = Banned
// 1 = User
// 2 = Admin
if($_SESSION['group_id'] == 0) {
?>
<h1>You Are Banned</h1>
<?php
} elseif($_SESSION['group_id'] == 1) {
?>
<p>Welcome to my blog.</p>
<?php
} elseif($_SESSION['group_id'] == 2) {
?>
<p>Welcome to my blog.</p><button>Delete Post</button>
<?php
}
Or you can do this by a second method:
<p id="1">Welcome To My Blog!</p><button id="2">Delete Content</button>
<h1 id="3">You are banned from this website!</h1>
<?php
if($_SESSION['group_id'] == 0) {
echo '<style>
#2 {
display:none;
}
#3 {
display:none;
}
</style>';
} elseif ($_SESSION['group_id'] == 1) {
echo '<style>
#1 {
display:none;
}
#3 {
display:none;</style>';
} elseif ($_SESSION['group_id'] == 2) {
echo '<style>
#1 {
display:none;
}';
}
Hope this helps!

How do I wrap some PHP code with a link?

I'm quite new to PHP and am having issues inserting a link into some of my code in the echo statement.
Thie following is what I have so far...
<div class="cta">
<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
echo 'Create Account |
Login';
} else {
echo 'Welcome, ' . $current_user->display_name;
}
?>
</div>
I want to wrap $current_user->display_name with a link but every time I attempt this, the whole page breaks.
Obviously my syntax is wrong but being new to PHP I am not certain how to fix this issue.
Try this in your else block
echo 'Welcome, ' . $current_user->display_name . '';
You Can just use concatenation as you are already with your example.
You can edit it with the below:
<div class="cta">
<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
echo 'Create Account |
Login';
} else {
echo 'Welcome, ' . $current_user->display_name .'';
}
?>
</div>

Drupal login logout links

I've got the following code to show when a user is logged in and when they're not.
<?php
global $user;
if ($user->uid > 0) {
echo "You are logged in as: " . $user->name . "<br>";
} else {
echo "You are not logged in!<br>";
}
?>
I want to add a logout link (when logged in) & and a login link (when not logged in). Would I just use html < a > links? If so, how would I incorporate them?
In most cases you would just use a regular menu and add the two items.
The visibility of the items is controlled by drupals menu-system, so you won't see the login-link if you're already logged in and you won't see the logout-link if you're not logged in.
If you want to output the links programmatically I would recommend using the "l" function
l($text, $path, array $options = array())
To check if a user is logged in you can simply use the user_is_logged_in function provided by the users module.
So your code would become
<?php
if( user_is_logged_in() ) {
print l('logout', 'user/logout');
}
else {
print l('login', 'user/login');
}
Depends on where you want to show the links. If it is on the same page/piece of code, just go ahead and echo them:
<?php
global $user;
if ($user->uid > 0) {
echo "You are logged in as: " . $user->name . "<br>";
echo "Logout";
} else {
echo "You are not logged in!<br>";
echo "Login";
}
?>
You can simply use the url function of Drupal core as below
<?php
global $user;
if ($user->uid > 0) {
echo 'Logout ';
} else {
echo 'Logout ';
}
?>
Hope this will help you :)

Making a variable of a variable in wordpress

I'm trying to put the ID from code 1 in to code 2. Can anyone help me? The ID is the logged in user ID. I want to re-use this ID in code 2. As you can see code 2 got ID 1 at the moment, but I need to assign the ID given from code 1 in to code 2 in stead of the ID "1"
Code1:
<?php $user_info = get_userdata(1); echo 'User ID: ' . $user_info->ID . "\n"; ?>
Code2:
<?php
$user_id = 1;
$user_blogs = get_blogs_of_user( $user_id );
echo 'User '.$user_id.'\'s blogs:<ul>';
foreach ($user_blogs AS $user_blog) {
echo '<li>'.$user_blog->blogname.'</li>';
}
echo '</ul>';
?>
The code will be placed in the same file. I'm trying to merge these 2 insted of using ID 1 in code 2
You are doing it wrong here. Your code one gives id of the current blog not the user. You need to change your code;
CODE 1:
$user_id = get_current_user_id(); //get the current logged in user id
CODE 2:
$user_blogs = get_blogs_of_user( $user_id ); //get the blogs of logged in user
echo 'User '.$user_id.'\'s blogs:<ul>';
foreach ($user_blogs as $user_blog) {
echo '<li>'.$user_blog->blogname.'</li>';
}
echo '</ul>';
Hope this is what you want :)
EDIT :
This is your current code:
CODE 1:
<?php
$user_info = get_userdata(1);
echo 'User ID: ' . $user_info->ID . "\n";
?>
CODE 2:
<?php $user_id = 1;
$user_blogs = get_blogs_of_user( $user_id );
echo 'User '.$user_id.'\'s blogs:<ul>';
foreach ($user_blogs AS $user_blog) { echo '<li>'.$user_blog->blogname.'</li>'; } echo '</ul>';
?>
Here you are assigning $user_id =1 directly. No need of that. You can do that directly in code 1.
Just change your current code to this:
CODE 1:
<?php
$user_id = get_current_user_id(); //get the current logged in user id
echo 'User ID: ' . $user_id . "\n";
?>
CODE 2:
<?php
$user_blogs = get_blogs_of_user( $user_id );
echo 'User '.$user_id.'\'s blogs:<ul>';
foreach ($user_blogs AS $user_blog) { echo '<li>'.$user_blog->blogname.'</li>'; } echo '</ul>';
?>
This will work provided your both codes are in same file.
This is what you want

Categories