Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Questions tagged [globals]

In PHP globals are variables that are available for every function. WordPress core defines several handfulls of global variables, such as `$post` and `wp_query`.

Filter by
Sorted by
Tagged with
1
vote
1answer
432 views

Add classname comment template from functions.php

I want to add an extra classname to my comment template (the div wrapper before the post itself) which is stated in functions.php. The classname in functions.php is an extra field in my users profile ...
1
vote
1answer
3k views

Cannot update custom database table row

I am trying to update a row in a custom database table by $wpdb->update();. In my opinion my code is right but for some reason my database row doesn't update. Can anyone help me with this? I want ...
1
vote
2answers
1k views

What are the disadvantages of using global variable?

I am a newbie at WordPress. In my "recent posts" shortcode, I used global $post variable, because the_title() function was giving me page's title, not post's title. I am not exactly sure, but I ...
1
vote
2answers
20k views

Defining a global array in functions.php?

I try to create an array of years, which is avaible in my template-files and in at least one other function in the functions.php. function get_the_years() { global $year_arr = range(2001, date('...
1
vote
2answers
4k views

Using global $post; to get featured image for custom post via WP_Query

I'm using a site-wide function to add css to the <head> of the page pull in featured images of various as a responsive page background. It works great across the rest of my site but fails on a ...
1
vote
1answer
283 views

How to check if this is the nth instance of a given shortcode in a post

Suppose I have a shortcode called [item], which I have used 20 times in a particular post. Is it possible, from within the shortcode handler function, to know what the numerical index of the given [...
1
vote
1answer
108 views

How to avoid repeatedly use the new statement to instantiate a class? [closed]

I have a Class with a lot of variables. To get a variable, I have to write this line in each function: $x = new MY_Class(); I guess there has to be another way? I tried: function my_data(){ global ...
1
vote
2answers
1k views

Call External Object in Class Function During Callback

I've a class which has a function that check and creates table in database. In order to do so i need to use WordPress $wpdb object. I need the function to run only on first plugin activation, so i ...
1
vote
1answer
40 views

What is the proper method of using global $post?

I am using global $post multiple times in a class. These are my doubts Is that a bad practice? Can it result in slow websites? What's the proper way to use it inside a class class Test{ public ...
1
vote
1answer
570 views

Multisite Installation: how do I setup global search?

I run a multisite installation and my sites are bilingual. I would like to find out how I can setup global search. Are there decent plugins available that do this? Cos I'm a wordpress noob and am ...
1
vote
2answers
4k views

Call global variable array() in woocommerce child/template

So I have this in my functions file - it defines the products that are not eligible for free shipping. It works everything is fine. //functions.php function my_free_shipping( $is_available ) { ...
1
vote
2answers
726 views

Override global query results without hooks

I have a custom php page and i need to override the global posts variables, but i cant get it to work. When going through the main loop im able to override the global posts, but i cant override ...
1
vote
1answer
461 views

store custom WP table names in a global variable

I was being creative and put this in one of the files loaded with my plugin. Are there implications with storing the table names this way? global $wpdb; if(!defined('DB_ARTISTS')) define('...
1
vote
2answers
4k views

Is $page a global variable in wordpress?

I am having a problem with a local $page variable, especially when using it in The Loop. Is it a reserved variable of Wordpress? Also where can I find a list of all those global variables?
1
vote
1answer
2k views

How to make global post work with custom post type?

I was wondering to know if it's possible to use global posts with custom_types. I was working with this function: global $post; $myposts = get_posts('numberposts=4&offset=1& category=3,4'); ...
1
vote
1answer
2k views

wp_get_current_user always returns 0 continued

I have been busting my head trying to get the user ID with every piece of code possible but it always comes back "0". Not sure what I'm doing wrong. I started out using this: <?php $user_info = ...
1
vote
1answer
697 views

Getting $curauth->ID to work inside a shortcode

Im wanting to show a profile/logo pic on author.php via a simple shortcode: function wpaluploader_showauthorimage() { $wpaluploader_authorlogo = '<img src="' . get_bloginfo('url'). '/wp-content/...
1
vote
1answer
324 views

Global $post not working in OOP function WordPress

I added a custom field with comment form. Below is my code.First function return post id when using var_dump function.But the last function return NULL always. class Post_Rating_Public_Helper { ...
1
vote
1answer
82 views

How to access plugin variables from theme templates without using globals?

Is there a way to access plugin variables from the theme template files without using globals? I have the following situation: One plugin function validates POST data submitted in a form. If ...
1
vote
2answers
1k views

How to modify a global variable in a function and use it on another function?

Final state: As Nathan Johnson stated, I change my plug in to a class, still can't use post id in increment_like function... <?php .... //irrelevant parts skipped class wpse_263293 { protected ...
1
vote
0answers
42 views

Assign External Database Queries to Global Variables and Make Them Accessible

I've got a bit of a tough question involving a website and its web app counterpart. The Background The website relies on two different databases: its default, named wp_website, and its counterpart's ...
1
vote
0answers
19 views

Pass updated local variable between functions? [duplicate]

I creating functions in my function.php and I have a question regarding passing local variables between functions. Let say I have the functions below, A and B, and I have the variable $amount. Now ...
1
vote
3answers
2k views

Global $post shows null in some of my custom post types archive pages

My english is not very good looking. I have several custom post types, 'noticias' (news) 'articulos' (articles), etc. In my functions.php i enqueue scripts and styles by post_id or post_type using a ...
0
votes
2answers
846 views

Declare Global Variable In OOP PHP

I'm working on my Plugin & i wanted to declare one of the global wordpress variables which is $current_screen To Check whether The user is on the target page or some other page, my Approach was ...
0
votes
3answers
2k views

How can i use a global variable in a .css file generated with php?

I am including a .css which i parse with php like so <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_childTheme_url(); ?>/css/productgallery.php" /> Now in ...
0
votes
1answer
657 views

How can I save custom meta to one global value?

I have added a custom meta box to one of my custom post types. It contains a checkbox that will indicate that this is the "featured monthly product" (there can only ever be one). When I check this ...
0
votes
4answers
1k views

Does WP have a global of $id?

I'm trying to attach an object to every post of any type. This will require post ID to populate or create my object. So, I either call global $post, or get_the_ID(), or do get_post(). I hope there is ...
0
votes
1answer
148 views

Best way to pass variables around a WordPress site?

I'm building a membership site with a membership plugin right now. The different types of memberships are associated with IDs and I have to constantly do things like "show this if the ID is 4122 (i.e. ...
0
votes
2answers
171 views

Test if php document executed by Wordpress or directly

Active plugin PHP files are interpreted automatically when you view a page, post, admin page, etc. I could also run a plugin PHP directly if I knew the fully qualified URL, however it will most ...
0
votes
2answers
179 views

Use global variables or function that returns said variables for site-wide private-ish WP settings?

I have a lot of functions extending various plugins and providing logic for a site, functions which use different bits of info that I would like to keep in one place and access it, but feel ...
0
votes
2answers
446 views

after_setup_theme, Global Variable and Theme Customizer

I am using a global variable that is defining in a function runs in after_setup_theme. This variable is not getting update changes in Theme Customizer. Let me explain this with an example: ...
0
votes
2answers
123 views

WordPress Image Slider Looping Title Not Working

In my index page there are two slider one is working fine but the second one is giving some problems. FIRST ONE <?php global $post; $i=0; $args = array('post_per_page' => -1, 'post_type' =>...
0
votes
1answer
10k views

Echo get_the_category() outside of loop (global?)

Trying to grab get_the_category() within the loop and post to the byline within the header. // file1.php <div> <h1><?php the_title(); ?></h2> <?php echo $...
0
votes
1answer
50 views

Declare global var from Template File and use it in Functions.php

I am using the wpseo_title filter to rewrite the authors page titles from my site. I have the following code in author.php template: <?php global $author_page_titles; $author_page_titles = ...
0
votes
2answers
256 views

Global Variable vs Local Variable

// MOVE REVISIONS METABOX TO BOTTOM OF EDIT-FORM.PHP $post_types = get_post_types(); add_action('do_meta_boxes', 'batteryboys_reorder_meta_boxes'); function batteryboys_reorder_meta_boxes(){ ...
0
votes
1answer
125 views

Use a variable created in get_header to calculate stuff in wp_footer

i am trying to create a small time tester that loads at get_header hook and the using the value does something in wp_footer... i have tried using global $var i tried putting the global var inside ...
0
votes
1answer
1k views

how to get the post id in plugin

I have a plugin that I'm working on and I'm having trouble getting the post id so jquery can use it. I'm able to get it using the admin_head hook but I'm not sure how I can return it to jquery. I'm ...
0
votes
1answer
1k views

get_posts() and global variables

I was reading this post: When should you use WP_Query vs query_posts() vs get_posts()?, which seems to have become the go-to post for understanding the difference between the different functions for ...
0
votes
1answer
1k views

Passing global variable from header.php to functions.php

Situation: Is header.php file where is code: if(is_single()){ global $my_global_var; $my_global_var == true; } Is functions.php file where is code: global $my_global_var; if($my_global_var == true) {...
0
votes
1answer
881 views

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

This might be better suited to Stack Overflow, thought I'd try here first though. I was walking through this tutorial by Justin Tadlock and encountered the function below being used as the callback ...
0
votes
1answer
1k views

What does the Global Variable $s represent?

What does the $GLOBALS['s'] variable contain? It looks like an empty array, but what would it normally hold? I've encountered it in a tutorial: The WordPress Theme: Single Post, Post Attachment, &...
0
votes
1answer
51 views

What's the safest way to switch and restore a post global?

I need to switch WordPress's post context, sort of like switch_to_blog() but for posts. What's the safest way to alter WordPress's global state and switch it back?
0
votes
2answers
38 views

How does WordPress make its functions globally available?

I'm trying to understand, how WP makes its countless functions globally available in any theme or plugin file(without the need to manually require any file). For instance, i've used my IDE to trace ...
0
votes
2answers
147 views

Translation issue with global variables

I'm creating a plugin that supports internationalization. Everything is working fine, except for global variables. Strings in global variables will not get the appropriate translation. Explanation: ...
0
votes
1answer
38 views

Why is overwriting $GLOBALS killing the_content()?

I'm in the single.php and now need to use the_content() instead of just $post->post_content; for youtube embeds etc.– in other projects the_content() and apply_filters("the_content", ...) does ...
0
votes
1answer
199 views

Getting the teaser text without overriding global variables

I am trying to make a WordPress theme, following the Wordpress Coding Standards - WPCS. In functions.php, I have a function to get only the teaser (text above the read more-tag). The function works, ...
0
votes
1answer
54 views

Filter the blog's title without using global variables

The below code uses the bloginfo filter to add some CSS rules to my blog’s title and outputs it to the site header, without affecting the HTML head’s <title> tag. This solution was taken from ...
0
votes
1answer
133 views

Recommendations on accessing current user data in WordPress

While reading the following WordPress Codex page on Global variable usage, there is a bold line that states ** Accessing other globals besides the ones listed below is not recommended. ** My ...
0
votes
1answer
161 views

WP user agent returns random variables

I am using a PHP function in order to return the user browser, using the WP global variables. It used to work perfectly, but recently it started doing random things on Safari. I use chrome and ...
0
votes
2answers
41 views

Detecting current page from class method

I've tried to look for a specific answer/tutorial on this subject but I'm coming up short and wanted to see what the SE community could suggest. At this point, any guidance whatsoever would be most ...