How to Use Global Variables Inside Header and Footer

In functions.php you can create a constant with the user id, like this. // if somehow this constant exists this will prevent a php error if (!defined(‘CURRENT_USER_ID’)) { // if user is not logged it this constant will contain 0 define(‘CURRENT_USER_ID’, get_current_user_id()); } Now you have a constant variable available anywhere in your theme. To … Read more

Get current page id, title, url, etc

<?php $link = “https://wordpress.stackexchange.com/questions/25552/mailto:?subject=”. get_bloginfo() .” ‘. the_title_attribute(array(‘echo’=>0)); $link .= ‘&amp;body=some text ‘. get_permalink(); ?> <a href=”https://wordpress.stackexchange.com/questions/25552/<?php echo $link ?>”>mail to a friend</a>

What does the Global Variable $s represent?

It’s not a global variable; in fact it’s not a variable at all. It’s just placeholder within the sprintf() function. Take a look at the sprintf PHP function documentation. In the example you cite, the author is using the ‘argument swapping’ placeholder syntax: %n$t where %n is the placeholder number that corresponds to the argument … Read more

Where is this function’s callback getting its arguments from?

If you have a look at function do_meta_boxes() in wp-admin/includes/template.php then you’ll see this line close to the end of the function: call_user_func($box[‘callback’], $object, $box); That calls the callback function and provides the two arguments. The $box argument holds all the information about the metabox, like ID, title, callback function. In wp-admin/edit-form-advanced.php, which displays your … Read more

get_posts() and global variables

get_posts doesn’t modify global variables & is safe to use anywhere but setup_postdata does modify the global variables. Use the function wp_reset_postdata() just after the foreach loop. It reverses the changes made by setup_postdata. The thing it does is basically same as you did in your example, you just won’t need to worry about the … Read more

Use a variable created in get_header to calculate stuff in wp_footer

You need to redeclare the variable in your end_time() and also you don’t need the user to be logged in.. You can try the below answer. // LOAD TIME CHECKER function start_timer() { global $time_start; $time_start = microtime(true); } add_action(‘wp_head’, ‘start_timer’, 1); function end_time() { //try redeclaring the var global $time_start; echo ‘ <div class=”clearfix”></div> … Read more

Global Variable vs Local Variable

As @Milo pointed out, when you use code in OP $post_types is undefined inside the function, so WordPress will act just like it was not there at all. However, that means you are using an undefined variable, and if turn WP_DEBUG on you’ll see a warning, and so you can notice you are doing something … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)