top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find the current user in Wordpress ?

+1 vote
1,334 views

I am trying to get the current user in WordPress. My code is in below:

<?php
    include('/wp-admin/includes/user.php');
    $current_user = wp_get_current_user(); 
    echo 'Username: ' . $current_user->user_login;
?>

but I get the following error:

Fatal error: Call to undefined function wp_get_current_user() in /membri/mysite/wordpress/getUser.php on line 3

How can I resolve it?

posted May 26, 2017 by Ram Jana

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In your functions.php, you can do this:

function my_theme_get_user_id() {
    global $current_user;

    echo 'The current logged in user ID is: ' . $current_user->ID;
}

then, use an action hook or a filter to use it were needed.

In your theme, under the wp-content/themes/ directory, you can simply do this directly in any of your files for view:

global $current_user;
echo 'The current logged in user ID is: ' . $current_user->ID;
answer Jun 1, 2017 by Sumanta Hazra
Similar Questions
0 votes

I have a button in my wordpress website that popup a gallery, my problem is that the gallery is loading the image from the code, so when my clients need to upload a new image I need to add it for him in the code. How can I make it?
Here is the code:

<!DOCTYPE html>
<html>
<body>

<img class="alignnone size-full wp-image-133 aligncenter" style="border: 5px solid white; border-radius: 50%; background: white; box-shadow: 1px 1px 5px gray;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/2000px-F_icon.svg.png" alt="" width="72" height="72" onclick="openModal();currentSlide(1)" class="hover-shadow cursor"><strong>try</strong>
  </div>
</div>

<div id="myModal" class="modal">
  <span class="close cursor" onclick="closeModal()">×</span>
  <div class="modal-content">

    <div class="mySlides">
      <div class="numbertext">1 / 4</div>
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/2000px-F_icon.svg.png" style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">2 / 4</div>
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/2000px-F_icon.svg.png" style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">3 / 4</div>
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/2000px-F_icon.svg.png" style="width:100%">
    </div>

    <div class="mySlides">
      <div class="numbertext">4 / 4</div>
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/F_icon.svg/2000px-F_icon.svg.png" alt="Nature and sunrise" style="width:100%">
    </div>

    <a class="prev" onclick="plusSlides(-1)">❮</a>
    <a class="next" onclick="plusSlides(1)">❯</a>

    <div class="caption-container">
      <p id="caption"></p>
    </div>

<script>
function openModal() {
  document.getElementById('myModal').style.display = "block";
}

function closeModal() {
  document.getElementById('myModal').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  var captionText = document.getElementById("caption");
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " active";
  captionText.innerHTML = dots[slideIndex-1].alt;
}
</script>
</body>
</html>
0 votes

I want to display all the existing categories in my Wordpress page in the bottom of the content and the selected category will be in bold style, as follows:

For example, for an existing post if I selected category2 of 3 existing, then it show like this

category1 category2 category3

<div class="entry-meta">
<span class="term-links">
<?php foreach ( get_the_terms( $post->ID, 'category') as $term ) : 
?>
<a href="<?php echo esc_url( get_term_link( $term->term_id ) ) 
?>"><span class="<?php echo $term->slug ?>"><?php echo $term->name ?>
</span></a>
<?php endforeach; ?>
</span>

<style>
.term-links .category2 {
display: inline-block;
font-weight:bold;
</style>

In the above code only display the selected category, not showing the all category. How can I do this?

0 votes

I want to disable the message "No result found" during page loading in my Wordpress site.

$search_code = $_POST['search_code'];

global $wpdb;

$helloworld_id = $wpdb->get_var("SELECT s_image FROM search_code WHERE s_code = $search_code");

if (empty($helloworld_id)) { 
echo '<div class="no_results">No results found</div>'; 
}
else 
{ 
?>

<img src="http://igtlaboratories.com/wp-content/uploads/images/<?php echo $helloworld_id; ?>"style="width:200px;height: auto;">

<?php
}
}

I used this code but when page load by default "No result found" visible. How I disable this massage during page load. Any help?

...