Would appreciate it, if anyone can let me know how we can set the global $user variable, so that we don't have to keep re-declaring it in each function, to access its contents. How can we declare it so that all the functions in a module can use it?
The type of global you're looking for (available always, in every scope) is called a superglobal in PHP. You cannot declare new superglobals, but you can access all globals directly because they are part of the $GLOBAL superglobal. In other words, you can use $GLOBALS['user'] to access the $user global directly.
See also create superglobal variables in php? for more info and alternative methods.
You can't...that's how globals work in PHP. If you want to import the global variable into your local function then you have to use the global keyword, there's no way round it. This is a 'feature' of the PHP language, it has nothing to do with Drupal.
An alternative method might be to implement a helper function:
function get_current_user() {
global $user;
return $user;
}
And call it like this:
$user = &get_current_user();
In your function if you want to use the $user variable, you just required to use/instantiate 'global' keyword before $user it. So that you can access all the data for that current user of the website.
For example
function myGenericFunc(){
global $user;
$user_id = $user->uid;
}
Note that you cannot redeclare it.
I hope it helps.
Related
I'm trying to "Import" the Wordpress core into an own script to use the functionality such as wp_query etc. I've created an script in a subdirectory (own framework) and want to extend it by wordpress, but everytime the script throws an error:
Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/taxonomy.php on line 333
such as (when I remove the add_action( 'init', 'create_initial_taxonomies', 0 )):
Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/post.php on line 1006
The non-object is the $wp_rewrite-object. I've echo'ed something and figured out that first $wp_rewrite is valid and at the next call not. I've changed nothing at the WP core files.
I try to include the core by calling:
require_once(BASE_PATH . 'wp-load.php');
Has anybody some ideas for me?
thanks
Short answer, do this:
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');
Long answer, it's a subtle gotcha around importing scripts with PHP.
If you define a local variable, outside of all functions, then it can be retrieved inside a function using 'global'. If you have a local variable inside a function, it cannot be retrieved later using global, unless it is defined as being global there and then.
The script 'wp-settings.php' is where the issue lies. It is included via your call to include 'wp-load.php'.
The variables defined there are not stated as being global; instead this is presumed because the script is always run outside of any functions, and so are automatically global. i.e.
$wordpress = 'foo';
function wordpressFunction() {
global $wordpress;
}
Because you are importing the script within a function, they now become local variables. You are essentially doing:
function myFramework() {
$wordpress = 'foo';
function wordpressFunction() {
global $wordpress;
}
}
So the fix is to define them as global yourself before importing the script. Now $wp_query, and the others defined as global, are correctly found.
The easiest way to access everything wordpress has programmed in is to use the following:
require_once('/../../../wp-blog-header.php'); // Use actual root path to wp-blog-header.php
header("HTTP/1.0 200 OK");
Using the above code you'll get all functions you would normally get using a template with in WordPress. I've tried all the other methods listed above and this one is by far the best.
I had the same error. I wanted to get some articles along with permalinks. This helped:
global $wpdb, $wp_rewrite;
require '/(...)/wp-config.php';
$result = $wpdb->get_results( $wpdb->prepare( ... ) );
foreach( $result as &$item )
$item->link = get_permalink( $item->ID );
I also found this useful in another case:
http://www.stormyfrog.com/using-wpdb-outside-wordpress/
I'm doing this to render a node and returning the output to an AJAX call and displaying a node inline on a page:
$node = node_load($nid);
$node_view = node_view($node);
echo drupal_render($node_view);
I need to be able to do the same things for a user profile...is it similar?
It's pretty similar, you can use the following functions:
$account = user_load($uid);
$account_view = user_view($account);
echo drupal_render($account_view);
EDIT
I changed the variable name to use $account instead of $user, just to eliminate the possibility of overwriting the global $user variable.
The Entity API module provide a generic function to render any entity: entity_view() that can also be used. For a user entity this should produce the same results than user_view().
$account = user_load($uid);
$account_view = entity_view('user', $account);
echo drupal_render($account_view);
I'm writing a plugin and trying to request some data from a custom table in my database using:
$current_text = $wpdb->get_results("SELECT text FROM addtext ORDER BY id DESC LIMIT 1");
but just get the error Undefined variable: wpdb
Any idea why this isn't working? I've followed the docs and scoured Google, with no luck. Still pretty new to WP plugins so probably something obvious.
Thanks!
I needed to use global $wpdb; in my function.
One note to add: You cannot use global inside a class, and of course you have to use global in order to get your objects to work using $wpdb.
While you can't use global immediately inside a class, you must declare $wpdb as global inside a function inside the class, and this does work.
e.g. This gives you an error:
class wpdb_test {
global $wpdb; // can't use global as a direct 'child' of a class
public function __construct () {
...
}
}
Because global can't be used directly inside a class. Likewise, simply referencing $wpdb inside the class also gives you an error because the object doesn't know what $wpdb is. You have to declare $wpdb as global from inside a function that is inside your class.
e.g. This works just fine:
class wpdb_test {
public $variable_name;
public function __construct () {
global $wpdb; // safe to use because it's inside a function
...
}
}
...and because $wpdb has been declared global inside a function inside a class you are able to use it.
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);
?>
This must be an easy one but I can't find documentation for it online.
I'm trying to use the l() function in Drupal to create a dynamic link. What's the syntax?
At the moment I have:
l('Destination',"path/$user->uid/category")
which points to:
path/%2Fcategory
first of all, if you're working within a function, you'll need to get access to the global user object.
Secondly, if the user is anonymous/not logged in, the $user->uid might not be set or be 0.
lastly to prevent errors, it is common to concatenate variables together with strings
global $user;
if ($user->uid)
{
l('Destination', 'path/'.$user->uid.'/category')
}
l() is correcting your URL to path/%2Fcategory because it's trying to make a workable link from the string path//category.
Your string is path//category because $user->uid has no value. It has no value because either you haven't pulled up a user object from global $user or user_load(), or your user is anonymous.
I would suggest putting checking the value of $user before calling l(), for example:
global $user; // or $user = user_load($foo);
if ($user) {
l('Destination', 'path/'.$user->uid.'/category');
} else {
l('Destination', 'path/you-are-not-logged-in');
}
Try concatenating the strings instead.
l('Destination',"path/".$user->uid."/category")
as for the documentation, here it is: http://api.drupal.org/api/function/l/4.7
l($text,
$path,
$attributes = array(),
$query = NULL,
$fragment = NULL,
$absolute = FALSE,
$html = FALSE)
The documentation for the l() function is located at:
http://api.drupal.org/api/function/l/6
Other stuff has been said yet by others :)