Making a variable of a variable in wordpress - php

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

Related

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>

get a variable number from sql

I'm a beginner in php. I'm trying get a variable number from sql ,
I have this part of code:
function renderNotification()
{
if ($user_id = $this->getDi()->auth->getUserId()) {
$cnt = $this->getDi()->db->selectCell("SELECT COUNT(ticket_id) FROM ?_helpdesk_ticket WHERE status IN (?a) AND user_id=?",
array(HelpdeskTicket::STATUS_AWAITING_USER_RESPONSE), $user_id);
if ($cnt)
return '<div class="am-info">' . ___('You have %s%d ticket(s)%s that require your attention',
sprintf('', REL_ROOT_URL . '/helpdesk/index/p/index/index?&_user_filter_s[]=awaiting_user_response'), $cnt, '') .
'</div>';
}
}
i want get number of ticket only in other place in my program
This is my try:
<?php echo "%s%d" ; ?>
or
<?php echo $cnt ; ?>
but not work
i'm using Zend , sf , pear , all what i need output %s%d ticket(s)%s from above code in other place or call it
i have found correct way it's below
<?php
$cnt = $di->db->selectCell("SELECT COUNT(ticket_id)
FROM ?_helpdesk_ticket
WHERE status IN (?a)
AND user_id=?",
array(HelpdeskTicket::STATUS_AWAITING_USER_RESPONSE),
$di->user->pk());
echo $cnt;
?>

Instagram API pagination in PHP

I am trying to create a small instagram app in PHP only (no database) and without getting an access_token (just my client_id). So far so good, (i.e. input user_id returns photos from last 30 days, with likes-count and created_time, in a table), until I get to pagination. As expected, I want to hit a 'more' button which loads next json file and adds additional photos to the existing table, but it falls apart there... Here is what I've got, working, except for the pagination attempt.
NOTE: this is an internal app, so the sensitivity of my client_id is not an issue, if it is exposed
<?php
if (!empty($_GET['user_id'])){
$user_id = ($_GET['user_id']);
$instagram_url = 'https://api.instagram.com/v1/users/' . $user_id . '/media/recent/?client_id=MY_CLIENT_ID';
$instagram_json = file_get_contents($instagram_url);
$instagram_array = json_decode($instagram_json, true);
}
?>
...
<?php
if(!empty($instagram_array)){
$instagram_array['pagination'] as $page { // Attempt at pagination
echo '<p>' .$page['next_url'].'</p>'; // Attempt at pagination
} // Attempt at pagination
foreach($instagram_array['data'] as $image){
if ($image['created_time'] > strtotime('-30 days')) {
echo '<tr>';
echo '<td>' . date('M d, Y', $image['created_time']) . '</td>';
echo '<td>'.$image['likes']['count'].'</td>';
echo '<td><img src="'.$image['images']['standard_resolution']['url'].'" alt=""/ style="max-height:40px"></td>';
echo '</tr>';
}
}
}
?>
</body>
</html>
Note: this is cobbled together from a few other sources - I am a total noob, so please forgive me if I need a little hand-holding...:)
You may specify min_timestamp to return medias which taken later than this timestamp
https://api.instagram.com/v1/users/{user_id}/media/recent/?access_token={access_token}&min_timestamp={min_timestamp}
$instagram_array['pagination']['next_url'] should be removed, it may include your access token which is a sensible data, that must be always invisible.
list_ig.php
<?
$user_id = "...";
$access_token = "...";
//30 day ago
$min_timestamp = strtotime("-30 day",time());
//pagination feature
$next_max_id = $_GET['next_max_id'];
$instagram_url = "https://api.instagram.com/v1/users/" . $user_id . "/media/recent/?access_token=" .$access_token. "&min_timestamp=" . $min_timestamp;
if($next_max_id != "")
$instagram_url .= "&max_id=" . $next_max_id;
$instagram_json = file_get_contents($instagram_url);
$instagram_array = json_decode($instagram_json ,true);
?>
<? if( $instagram_array['pagination']['next_max_id'] != "" ): ?>
More
<? endif;?>
.... print instagram data....
Instagram AJAX Demo
http://jsfiddle.net/ajhtLgzc/

php echo wordpress user meta data

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

Showing specific information of current user logged in, in PHP

I'm using this 2 plugins
http://www.wpexplorer.com/wordpress-user-contact-fields/
and
WooCommerce
The 1st parameter, in this case the number "1" refers to the id of the user, how con I change it to be dynamic? So, depending on the user I get its own specific information
<h2>Personal</h2>
<?php
echo '<ul>';
echo '<li>Direccion: ' .get_user_meta(1,'address',true) . '</li>';
echo '<li>CompaƱia: ' .get_user_meta(1,'company',true) . '</li>';
echo '<li>Birth dAte: ' .get_user_meta(1,'birth',true) . '</li>';
echo '<li>Gender: ' .get_user_meta(1,'gender',true) . '</li>';
echo '<li>phone: ' .get_user_meta(1,'phone',true) . '</li>';
echo '</ul>';
?>
thanks
Don't try to make a function do something it wasn't intended to do. Write your own. Especially for something this simple.
function getUserStuff($id, $item){
$item = mysql_real_escape_string($item);
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT `".$item."` FROM `users` WHERE `id` = '".$id."'");
$z = mysql_fetch_assoc($q);
return (mysql_num_rows($q) > 0) ? $z[$item] : false;
}
This is just an example. I used a deprecated function for simplicity but you should use a different API.

Categories