The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
0
votes
0answers
6 views

API Function to use in place of global $menu?

Just wondering... Googling it I cannot find it. I'd like to not depend on globals at all in any circumstance.
0
votes
0answers
27 views

What is the correct way to obtain access to the WP_oEmbed object?

I know that WP_oEmbed is a class used by WordPress to handle oEmbeds. Is this available as a global or should I create a new instance of it? Update: I have found a function called ...
0
votes
0answers
18 views

Browser detection booleans mess. Caché plugin?

Hello, I have an issue concerning the Browser Detection Booleans. Sometimes, being in Chrome or Firefox, the page is served as being in IE, as in the attached image. Can this be related to the fact ...
1
vote
1answer
37 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 ...
0
votes
2answers
37 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
0answers
173 views

Trying to get property 'post_content' of non-object

I'm creating a wordpress theme and I created a shortcode to show an abstract of a post (the first image, title, and link) from a post (by its ID). To do this, I created a function to get the first ...
0
votes
0answers
13 views

using global variables and performance

I use global variables to retrieve all sort of available data, especially $post and $posts, for example in order to retrieve the url of a user from the post loop instead of using ...
0
votes
0answers
40 views

Simple global, in-memory, persistent object for storing result of stemming a relatively large JSON blob

I'm relatively new to WordPress. I have this 2.6 MB json file: https://raw.githubusercontent.com/codeforamerica/naics-api/master/data/naics-2012.json It's a list of around 2000 entries where every ...
0
votes
0answers
40 views

How to access custom class methods from any include without using global

I am trying to do the following functions.php class Test { public function hello(){ return 'Hello'; } } $test = new Test(); header.php or any other include echo $test->hello();...
0
votes
0answers
14 views

How do I get a reference to the HTML memory buffer while the page is being constructed?

I want to view & update the buffer with html "<head>" data section in it, after the head has been generated. Problem statement: I have created some shortcodes in a plug-in which, if ...
0
votes
2answers
103 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' =>...
1
vote
1answer
287 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 { ...
0
votes
0answers
90 views

global $wp_meta_boxes returns NULL

When I call global $wp_meta_boxes; it returns null. I'm trying to calling like: class my_class() { public function __construct(){ add_action( 'wp_dashboard_setup' , array( $this , '...
0
votes
1answer
178 views

Relative redirects for posts, categories, products and product categories

I imported all content from an old site to a new one. The old site was basically a catalog of products and blog posts. However, it was all built as standard WP posts - not custom post types. The new ...
0
votes
0answers
107 views

Ajax global variable is not getting saved (returns null)

I am trying to modify a global variable $lath , It is a shortcode. This is where i have defined it. function listing_order(){ global $lath; $lath = array('email','phone'); foreach ($lath ...
0
votes
2answers
173 views

Current page id returns the incorrect value

I need to capture the current page id, execute some conditional script in footer, in my own plugin function hooks to wp_footer. Here is my plugin code, but page id returning wrong value? <?php /* ...
1
vote
1answer
494 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 ...
0
votes
1answer
377 views

Global page ID variable empty error

I am creating theme and I have a need to create global variables within theme functions.php file. Currently I am doing it like this: /** * Setup globals */ global $nb_id, $nb_page; $nb_id = ...
0
votes
1answer
113 views

$GLOBALS['value1'] is not working

My custom PHP script works on my PC over localhost. But, when I upload my code to the 'live' WordPress site, The $GLOBALS values are just null even they work on my localhost. (Nothing wrong with the ...
0
votes
1answer
18 views

Creating a (global)-mapping

I'm making a mapping between page-id's and category-id's, that's being used several places. However I read from this post, that one should never use global variables if one could avoid it. But I ...
0
votes
2answers
746 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 ...
4
votes
2answers
2k views

What is $post_id? is it a global variable in wordpress?

How can $post_id be used while echoing posts in single.php? Is it a global variable?
-1
votes
1answer
67 views

$GLOBALS & global doesn't work [closed]

I have the following configuration: $GLOBALS['x'] = 1; add_filter( 'filter_hook', 'my_function', 10, 2 ); function my_function() { $GLOBALS['x'] = 2; } add_action( 'action_hook', 'my_function2'...
0
votes
2answers
144 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: ...
4
votes
2answers
2k views

Create a global variable for use in all templates

I used following code function userinfo_global() { global $users_info; wp_get_current_user(); } add_action( 'init', 'userinfo_global' ); in a file users.php , this file are call in inside ...
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
0answers
30 views

wp localize script makes variable global, how to solve that?

I don't want my variable "infinite" to be global variable. but it's like wp_localize_script would make it global automatically. How to solve that? This is my code. Wordpress php file: public ...
0
votes
1answer
48 views

using globals from wp_link_pages function

I'm following this reference page for wp_link_pages:https://developer.wordpress.org/reference/functions/wp_link_pages/ In it, 4 global variables are declared: $page, $numpages, $multipage, $more. I'...
0
votes
1answer
355 views

I need some explanation on global $post [duplicate]

Sorry if this sounds like a dumb question but it's something that has bugged me for a while. I need some explanation on function custom_function () { global $post; } I understand from PHP that ...
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
1answer
43 views

global variables in templates

I have defined a global variable in one of my template files page.php. I can access the global variables in my loop. But when the loop displays an individual post in single.php, the variable has no ...
-1
votes
1answer
51 views

Global Handle to Class unavailable in Plugin?

I am writing a custom plugin and calling the plugin functions using short codes on my wordpress page. global $db_handle; below declares the handle to the class 'DBController' global. if (isset($...
0
votes
2answers
88 views

Get variable from previous blog while using switch_to_blog

I'm writing a query that switches to the main blog of a MU site using switch_to_blog(). All is well except i'm attempting to write a wp_query that includes a variable needed from the previous blog. ...
1
vote
1answer
2k views

How does the $post_type global variable work?

I need to output the name of the custom post type within archive.php and single.php. I would use something like this: if ( 'staff' == get_post_type() ) { $post_type = 'staff'; } echo '<div ...
0
votes
1answer
187 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, ...
1
vote
1answer
81 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 ...
2
votes
1answer
57 views

Adding a section to admin menu for global settings

Bear with me a moment as I am still relatively new to WP dev. I am currently building a custom site that loads certain blocks on pages using get_template_part(); . So for example, the home page ...
-1
votes
1answer
21 views

Wordpress Multisite and site speed and scaleability

We have a multisite wordpress setup like this: domain.com/nz domain.com/au domain.com/us etc. I would like to know the best approach for the best setup so we can give each country the best site speed ...
0
votes
1answer
1k views

Ajax Pagination on Ajax filter

Following this great tutorial I've implemented Ajax pagination with scrolling: https://www.billerickson.net/infinite-scroll-in-wordpress/. Now, I've added a second Ajax call to filter the posts by ...
0
votes
1answer
145 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. ...
4
votes
1answer
571 views

Filtered query_vars becomes global. Why does this work?

For some reason that's not quite clear, when I add custom query_vars, they become available everywhere without the need of an accessor like global or get_query_var() // if your url contains the var ...
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
votes
1answer
211 views

$do_not_duplicate is not working in genesis theme

I'm using Genesis child theme and I am using this function to display feature posts on home page. function modern_motoroids_extra_feature_posts(){ ?> <?php $do_not_duplicate = array(); ?...
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 ...
0
votes
1answer
70 views

Need data from two different actions

I have two functions on my functions.php Both functions are from a plugin and I'm trying to use this variable $answerIndex in the other action to do the if condition. The issue is I'm trying with ...
0
votes
1answer
441 views

global categories - Share specific categories in wordpress multisite

I have a wordpress multisite network. So I want to share just some categories between subsites.
2
votes
1answer
3k views

Passing variables to template parts

I have one template file videos.php which has the following line of code in it (aswell as a load of HTML): <?php get_template_part('loop', 'feed-videos' ); ?> inside that template part, I have ...
0
votes
2answers
164 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
1answer
53 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 ...
2
votes
2answers
203 views

Filter the blog title displayed in the header

I wanted to filter the blog title displayed in the header to apply different CSS styles to different title parts/words, so I added the below function to my (WP 4.7) Twenty Seventeen child theme's ...