How to echo a wordpress value inside an 'echo' - php

I'm trying to do this:
<?php
global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) {
echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>'; "\n";
echo 'Logout';
}
else {
echo 'Login';
}
?>
The problem is that href gives me back the empty value: wp_logout_url( home_url() );
When I use this WORDPRESS call outside the echo it works good, like this eg:
LOGOUT
How can i write this ??

echo 'Logout';

None of those was, but I appreciated.
This works well:
'text

echo 'Logout';
Needs to be changed to
echo 'Logout';
String started with single quote needs to be closed with single quote.

I think what you want is this:
<?php global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) { echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>'; "\n";
echo 'Logout'; } else { echo 'Login'; } ?>
The different is the string is closed before the result of wp_logout_url() is concatenated with it

It's because you've not ended you string
You'd want to use the following:
echo "<a href=\"" . wp_login_url( get_permalink() ); . "\"/>;

Here the correct option
<?php
global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) {
echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>\n';
echo 'Logout';
} else {
echo 'Login';
}
?>

Related

Style PHP using echo Issue

Using this code on my WP site and need help styling the echo lines of code;
`<?php
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
echo '' . esc_html( $current_user->display_name );
echo get_avatar( $current_user->ID, 38 );
}
}
?>`
Would like to use CSS classes, but when I try the below code it doesn't work;
echo '<p class="CSS Style">' . ($current_user->display_name ) '</p>';
Thanks to anyone that can help. Probably pretty simple to most developers.
I try run your code, i think you are missing the ' . ' before </p>.
echo '<p class="CSS Style">' . ($current_user->display_name ) . '</p>';
Hey you can also do something like this if you want write a standalone P tag outside the PHP logic.
<?php
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) { ?>
<p class="username"><?php echo $current_user->display_name; ?></p>
<?php
echo get_avatar( $current_user->ID, 38 );
}
}
?>
Otherwise, you can only add a dot before P closing tag.
Use this line of code
echo '<p class="CSS Style">' . ($current_user->display_name ).'</p>';
Your forgot to give period(.);

Wordpress - using variable in menu URL

Is it possible to use php variable in menu URL, that I defined in function.php?
This is actually not working:
For example, in variable $path I store username of every user so they can visit their own profile.
You can use a hook and add the PHP programmatically.
add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
function nav_items( $items, $menu, $args ) {
if( is_admin() )
return $items;
foreach( $items as $item ) {
if( 'profil' == $item->post_title ) {
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$item->url .= '/' . $username;
}
}
return $items;
}
I found this code from another answer on this community.
In Wordpress You Can Do Direct Like this for get the current user details:
<?php
$current_user = wp_get_current_user();
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
Reference: https://codex.wordpress.org/Function_Reference/wp_get_current_user

How can i display only the first image from this JSON array?

OK, I'm trying to integrate an API that lists adoptable pets into a wordpress website. I've done lots of googling and read through tutorials, and so far have managed to put together a super basic plugin that seems to do what I'm trying to accomplish. Currently I'm trying to pull in an image, but just the first image. Each animal may have 5 images associated with it, but I only want to pull in the first (default). Currently my code brings them all. Now I realize the problem is that I'm using "foreach()". But, this is new to me and my googling is not going well, and any other way I've tried to do it is just not getting me ANY pictures. Any advice is appreciated....and if I'm doing anything else wrong, feel free to let me know :) I also need to figure out how to paginate it, but I'm thinking that's a separate question! Thanks!
<?php
add_shortcode('pets', 'petsshortcode');
function petsshortcode() {
$request = wp_remote_get( 'https://petstablished.com/api/v2/public/pets?public_key=UlEK4EWvDAoOjXeQXSCQZAyBywWfqfOg&search[status]=available,foster&pagination[limit]=20&pagination[page]=1' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data ) ) {
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
foreach( $collection->images as $images ) {
echo '<div class="pet-photo"><img src="' . $images->image->url; echo '" width="200"></div>';}
echo '</ul></div>';
}
}
}
You don't need nested foreach loops in this case, simply use just one foreach loop like this:
// your code
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
echo '<div class="pet-photo"><img src="' . $collection->images[0]->image->url; echo '" width="200"></div>';
echo '</ul></div>';
}
// your code

PHP Array Random Variable not switching out on page refresh

I am using Advanced Custom Fields plugin on a WordPress site. I have a repeater field with 25+ quotes that I am displaying on a per page basis. Each page has unique quotes.
The code I currently have to pull one random quote from that pool of 25 works just as it is suppose to when I am logged in. When I log out, it does pull one random quote but will not switch it out to a different one on page refresh like it does when I am logged in.
I am using this random row code from here:
https://gist.github.com/wesrice/1924934
<?php
if (is_singular('post')) {
$repeater2 = get_field( 'quote_info', 11 );
$random_rows2 = array_rand( $repeater2, 1 );
if( is_array( $random_rows2 ) ){
foreach( $random_rows2 as $random_row2 ){
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater2[$random_row2]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater2[$random_row2]['bottom_quote_author'] . '</div></div>';
}
} else {
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater2[$random_rows2]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater2[$random_rows2]['bottom_quote_author'] . '</div></div>';
} wp_reset_query();
} else {
$repeater = get_field( 'quote_info' );
$random_rows = array_rand( $repeater, 1 );
if( is_array( $random_rows ) ){
foreach( $random_rows as $random_row ){
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater[$random_row]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater[$random_row]['bottom_quote_author'] . '</div></div>';
}
} else {
echo '<div class="newquote">
<div class="newinnerwrapperquote">"' . $repeater[$random_rows]['bottom_quote_text'] . '"</div>';
echo '<div class="newquoteby">' . $repeater[$random_rows]['bottom_quote_author'] . '</div></div>';
}
}
?>
The first if statement is if it is on a WordPress post rather than a page. I hope I explained this well.
Internal caching plugin was activated. Turned it off and works now when not logged in.

Custom WordPress Shortcode, can't replace echo with return

This code works great when put directly into the content of a page.
<font color=#ff0000>
<?php global $current_user;
get_currentuserinfo();
echo $current_user->display_name . "\n";
echo 'Level: ' . $current_user->level . "\n <br>";
?>
</font>
Result shows 'Sam level 65'
However, when I convert it to a shortcode and try to replace echo with return, wordpress breaks, all screens go blank.
This is an example of what i've tried:
// Add Shortcode
function custom_shortcode() {
// Code
<font color=#ff0000>
<?php global $current_user;
get_currentuserinfo();
$output = $current_user->display_name . "\n";
$output .= 'Level: ' . $current_user->level . "\n <br>";
return $output;
?>
</font>
}
add_shortcode( 'userlevel', 'custom_shortcode' );
Any idea where I'm going wrong?
Thank you in advance!
You're using HTML where PHP is expected. That's why you have a blank screen.
Here's how I'd move this into a shortcode:
function wpse_shortcode_userlevel() {
global $current_user;
get_currentuserinfo();
$output = '<span class="shortcode-user-level">';
$output .= $current_user->display_name . "\n";
$output .= "Level: {$current_user->level} \n <br>";
$output .= '</span>';
return $output;
}
add_shortcode( 'userlevel', 'wpse_shortcode_userlevel' );
Then in your stylesheet add:
.shortcode-user-level {
color: #ff0000;
}

Categories