How to print current_user name and Logout button in wordpress - wordpress

I want to display this
Current user name - Logout( redirect to home page )
This is my current code:
echo $current_user->display_name . "\n";
echo Logout
But when i click on a link, i get only "" in address bar. The function is not being recognized.
Thanks

Try
echo 'Logout';
you are mixing php code and echo

This will give you the current username of logged in admin.
global $current_user;
echo $current_user->user_login;
Function Refrence

You need to include the global variable and initialize it for it to be usable.
Above where you need to call it, add this
global $current_user;
get_currentuserinfo();
echo $current_user->display_name
It will call in the variable and allow access to its data and functions.
Read more about getting use information HERE
For the URL, try this:
echo '<a href="'. wp_logout_url(home_url()) .'" title="Logout" />Logout</a>';

Related

WordPress: page Template Conditional checking not working in functions.php

I want to add a stylesheet in WordPress only if a user is using a specific template but conditional page template ( is_page_template('templa_name) ) is not working and code is falling into the else part. I have even gotten the right name of the template through code and tried adding that code but it didn't work. I can easily add and continue my development but it is strange and would like to know the solution to this weird problem.
Tried the code inside the template file and stackoverflow. there is an anwer but to no avail. Tried different variets of the code but it goes the same way
if ( is_page_template('page-templates/page-landing-page.php') ) {
function this_echoes() {
$looking_at = is_page_template( 'page-templates/page-landing-page.php' );
echo $looking_at;
echo '<br>';
echo 'if';
echo '<br>';
$page_template22 = get_page_template_slug( get_queried_object_id() );
echo $page_template22;
}
} else {
function this_echoes() {
$looking_at = is_page_template( 'page-templates/page-landing-page.php' );
echo '<br>';
echo $looking_at;
echo '<br>';
echo '$looking_at';
echo '<br>';
$page_template22 = get_page_template_slug( get_queried_object_id() );
echo $page_template22 ;
echo 'else';
}
}
I want the code to fall under the if statement rather then else statement. I do know that I am checking the right way.
Your if condition needs to go in the main this_echoes function.
I would suggest using template name slugs
(https://codex.wordpress.org/Function_Reference/get_page_template_slug) and then using an if check get your job done

Unable to login from another wordpress site

My A site passes POST data to site B thus must log in the user into site B.
I prefer not to verify password in site B, so I'm using below:
$user = $_POST;
$a = get_user_by('login', $user['data']['user_login']);
do_action('wp_login', $a->data->user_login, $a);
wp_set_current_user( $a->data->ID );
wp_set_auth_cookie( $a->data->ID );
I can get current logged in user like this: wp_get_current_user();.
also when I check is_user_logged_in(), it says true as well.
But when I redirect to site B, the user is not logged in.
One more is that,
when i echo site_url() it says site B url which is correct. BUt when I use wp_safe_redirect or wp_redirect() it goes to site A.
so I use a hack like this:
$string = '<script type="text/javascript">';
$string .= 'window.location = "' . site_url() . '"';
$string .= '</script>';
echo $string;
exit;
What am I doing wrongly here?

How to use wordpress settings and session information on a custom page?

I am new in designing applications for wordpress websites. This is my first time when i am creating an application in wordpress environment. I am creating a PHP script getinfo.php that will deliver a json object containing information about currently logged in user in wordpress. And for that I need to know the php code to initialize wordpress backend PHP environment.
e.g including a php file or calling a function which will load the user's session information from database of a wordpress website.
e.g. If any basic PHP website which stores information of currently logged in user in $_SESSION variable. For such website i will write following code in getinfo.php to get information from user's session.
session_start();
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
$email = $_SESSION['email'];
What will be the code for acquiring exact same information in wordpress environment?
How to initialize the session object in wordpress?
And how to access it?
Thanks in advance.
wordpress isn't using $_SESSION - but you can get the user info through the global $current_user object.
the above in wp should look like this:
if (is_user_logged_in()) {
global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";
}

get_home_url() just returns current URL

My problem is simple, I need a "home" link for my WP template, and the function get_home_url() is simply returning the current url. So if I'm on "example.com/?cat=2", that's the url I get for my home button .
Any ideas what I'm doing wrong? I'm pulling my hair out.
It's just home_url(). So:
echo home_url();
Will output the homepage of your Wordpress installation.
Have you tried:
echo get_bloginfo('url')
You need to echo that function, so it should be:
<?php echo get_home_url(); ?>

Delete post from front end of wordpress

Here's the code I have:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$url = get_bloginfo('url');
if (current_user_can('edit_post', $post->ID)){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
?>
I'm trying to delete the post outside the wordpress loop.
When I click the link, I get:
"Your attempt to delete this post: “post-name” has failed.
Please try again."
Does anyone know why that would be?
Looking at the reference, it looks like current_user_can() only takes one argument.
Perhaps the root cause here is a permissions issue for the user. You should be checking if a user has the delete_posts capability as seen below. Note that delete_posts checks if the user can delete their own posts. To check if the user can delete the posts of other users, you can use delete_other_posts instead.
if (current_user_can('delete_posts')){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
(Assuming you are using WP version 2.1 or later)

Resources