Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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`.

0
votes
2answers
582 views

How do I pass custom shortcode-extracted variables (taxonomy) into a query function for WordPress RoyalSlider?

Plugin in question: http://dimsemenov.com/plugins/royal-slider/wordpress/ Background: This is a great plugin, and it features a 'post' version where the slider pulls content from posts. However, the ...
3
votes
2answers
758 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
vote
3answers
1k 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
0answers
40 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
11 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
37 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 ...
4
votes
2answers
789 views

Accessing $post global on a custom post type archive page

I have a custom post type called projects, and its archive is at /projects, on the archive page I get notices Notice: Trying to get property of non-object every time I try to access the $post i.e. $...
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
1answer
946 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
408 views

Save something to global var in add_filter

I am here again with a strange question. First, I'm using ACF plugin for adding cistom fields. My code looks somethink like that: global $testMe; $testMe = 0; function my_acf_update_value( $value, $...
2
votes
2answers
742 views

Custom plugin: Trying to show saved data on frontend

I'm working on my first plugin and I have made a nice settings page which saves and works. But I have split it to few files and now I can't determine if field is checked.. I have also tried ...
6
votes
5answers
7k views

Where to get information about array fields in $_REQUEST?

I just started to learn PHP and doing things with WordPress and I am a bit confused about information in Codex. Was told that Codex got all info I required but I got stuck with it. What array fields ...
0
votes
0answers
12 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
24 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
143 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
70 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 ...
13
votes
1answer
11k views

How to pass arguments from add_settings_field() to the callback function?

I have a function like this: add_settings_field( 'contact_phone', 'Contact Phone', 'settings_callback', 'general'); That works. It calls settings_callback. Cool. The problem I have with this is: ...
0
votes
0answers
59 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 , '...
8
votes
4answers
1k views

WordPress Theme variables scope

I need to create a variable that can be accessed throughout my WordPress Theme template files (index.php, header.php etc..). I know function definitions go inside the functions.php template file (in ...
0
votes
1answer
59 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
2answers
84 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
257 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
0answers
15 views

Have global footer that isn't applying globally?

I have a global footer set up but it's not applying across the site. There are 2 footers showing and they are quite similar, but they aren't the same. I used Beaver Builder to create the footer as a ...
27
votes
4answers
161k views

How to set and use global variables? Or why not to use them at all

UPDATE: My original question has been solved, but this is turning into a valid discussion about why not to use global variables, so I am updating the question to reflect that. The solution was <?...
0
votes
1answer
277 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
72 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
17 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
475 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 ...
2
votes
1answer
826 views

WP 4.4.1 allow empty comments via add_action 'pre_comment_on_post'

got a question about allowing empty comments with worpdress v.4.4.1 to achieve this i use the action 'pre_comment_on_post'. in WP 4.3.1 i could do this easily: if (isset($_POST['comment']) &&...
0
votes
2answers
418 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: ...
-1
votes
1answer
58 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
130 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
36 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
27 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
44 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
280 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 ...
0
votes
1answer
42 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 ...
0
votes
0answers
255 views

How to load post_meta for custom post type via Ajax

I'm trying to load a YouTube video via ajax outside the loop. I have it working on regular pages, but not anything loaded via ajax. A user would click a button, then a YouTube video will load via ajax,...
0
votes
0answers
59 views

Add notice to add image popup

I have been using the global $pagenow variable to add instructions for users, but I need to somehow hook into the add image popup as I would like to instruct the users on optimal resolution for ...
-1
votes
1answer
48 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
1answer
142 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
2answers
80 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
1k 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 ...
1
vote
1answer
68 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
51 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
138 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
479 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
832 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 ...