how do you get current userid in wordpress - wordpress

Hi have a simple php file in my wordpress site, where I need the current userid
I have tried the following, but keep getting
Call to undefined function get_current_user_id()
<?php
include '../../mydbfile.php';
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);

You need to include wp-load.php to call the global $wpdb
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
This has been tested and works.

Related

How to access Wordpress' global $wpdb from PHP?

I'm trying to execute a query in PHP against a MySQL database. My code is below:
<?php
require_once ("wp-includes/wp-db.php");
global $wpdb;
$myrows = $wpdb->get_results( "SELECT id, name FROM wp_db_posts" );
echo $myrows;
?>
After executing this code, I get a Fatal error: Call to a member function get_results() on null in C:\Apache24\htdocs\wordpress\me.php on line 4
I'm actually new to WordPress and want to know exactly how can I access the $wpdb global variable from PHP. My databses are pre-fixed with wp_db_.
What am I doing wrong? Can anyone help me here?
assuming you script placed in the root directory of WordPress you should load wordpress not access the wp-db.php directly so your code should be like this:
<?php
require_once "wp-load.php";
global $wpdb;
$myrows = $wpdb->get_results( "SELECT ID, post_name FROM {$wpdb->prefix}posts" );
and you can't echo myrows as you will get object not string you should use instead
var_dump() or print_r()

How to get shipping class name not slug woocommerce

global $product;
$shippingClass = $product->get_shipping_class();
this returns the slug but I need the name how do I do this?
After $shippingClass add below lines it will get the name.
$shipp_classname= get_term_by('slug',$shippingClass ,'product_shipping_class');
echo $shipp_classname->name;
So complete code is following.
global $product;
$shippingClass = $product->get_shipping_class();
$shipp_classname= get_term_by('slug',$shippingClass ,'product_shipping_class');
echo $shipp_classname->name;

Will get_current_user_id() in wordpress fire a database request?

I am using get_current_user_id() and wp_get_current_user() multiple times in my plugin. Are these functions going to send a database request everytime I use them or is the user object of the current user always available (cause wordpress requests it anyway on initializing)?
Would it be better to declare a global var at the start of my script with the current user info?
<?php
global $currentUser;
$currentUser = wp_get_current_user();
function function1() {
global $currentUser;
echo $currentUser->ID;
}
?>
No it will not. It will read the wordpress global variable $current_user

wordpress page id in friendly url

How to get the page id in wordpress in the case of friendly url?
Inside the Loop: http://codex.wordpress.org/Function_Reference/the_ID
Outside the Loop: global $wp_query; $id = $wp_query->post->ID;
I'm guessing you're talking about doing it via the Admin interface. Can't you just add it into the custom format textbox using the %post_id% string:
/archives/%post_id%
If you are within the Loop (most of the time meaning within the while(have_posts()) you should be able to use: global $post; $id = $post->ID; to get the ID

drupal-----where are these variable declared?

i saw some module using eg:
function statistics_exit() {
global $user, $recent_activity;
.......
}
where are these variables($user, $recent_activity) declared? thank you. how should i know the values of there variables.
$user is a global var which represents the current logged in user.
for more info about the Drupal core globals visit http://api.drupal.org/api/drupal/globals/6
The global $user object is first set in the sess_read() function in session.inc. I don't know where $recent_activity is set. If you want to know the value, you can simply print the variable like this:
<?php
global $user, $recent_activity;
var_dump($user, $recent_activity);
?>
Or if you have devel module installed:
<?php
global $user, $recent_activity;
dsm($user);
dsm($recent_activity);
?>

Resources