Skip to main content

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
0 answers
85 views

Global variable not showing in shortcode

I am trying to show some variables when I hover over products image. For that I am using a shortcode to show the variable I need and the function do_shortcode(). However the needed variable is not ...
Mehdi's user avatar
  • 103
-1 votes
1 answer
51 views

Pass global variable data to localize_script

In my template file: product-list-section.php I loop through my products and add data to a global variable: global $product_filter_data; $product_filter_data = array(); // Loop products, grab ...
wharfdale's user avatar
  • 348
0 votes
1 answer
13 views

Is there a way to natively return the full filesystem path when using $wp_styles?

Looping through global $wp_styles, $wp_styles->registered[$style]->src returns the URL of the resource e.g. https://domain.com/path/to/file/. I'd like to source the local filesystem path instead ...
Motivated's user avatar
  • 235
1 vote
1 answer
105 views

'global' not working when passing variable from plugin file to template

I have a custom plugin file that contains a function to generate a string upon the firing of a 3rd-party plugin hook. I want to echo this string in a custom template file — how do I give the template ...
tikis.tacos's user avatar
0 votes
1 answer
758 views

How can one use variables in a template or template part without polluting the global scope?

I'm a developer who is relatively new to developing in PHP for WordPress, and I am working on a WordPress site that has a considerable amount of custom code in the site's theme. I have been enjoying ...
NReilingh's user avatar
  • 103
0 votes
2 answers
708 views

How do i set global variable in a function in wordpress functions.php

Please vet this code and help me with how to declare '.($a['link']).' variable such that i can echo its content in single.php function mp3_download_link_att($atts, $content = null) { $default = ...
John Ovye Godwin's user avatar
6 votes
2 answers
5k views

Dequeue classic-themes.min.css

since version 6.1, wordpress loads classic-themes.min.css on our websites and crashes all of my buttons styles. I only want to see my own styles in the frontend, so i'm dequeueing WP Styles like this: ...
emot1onz's user avatar
  • 141
1 vote
0 answers
60 views

Wordpress Loop and $post

I decided to take a closer look at how WP works and ran into this question. I see that when WP is loaded, the wp() function is triggered, which will parse the request and get data from the database in ...
Alex's user avatar
  • 11
1 vote
0 answers
46 views

Result of API Request to global variable?

I have an API Request for weather and the account of this weather stuff has a limitation of requests per minute. In my theme I have the weather information twice on the same page. So if I would make a ...
vriic's user avatar
  • 11
0 votes
0 answers
151 views

Global $post returns 2 objects for my shortcode

I made a shortcode, it does most of what I need it to do. Except for a filter I need the post id of the page the shortcode is placed. (to get some linked custom field values) And I can't expect the ...
marty90's user avatar
  • 11
0 votes
1 answer
1k views

How to Use Global Variables Inside Header and Footer

I set global variables for some custom user data in my functions.php to use them in navigation inside header and footer. if (is_user_logged_in()) { # users wordpress id global $user_id_wp; ...
Robin Alexander's user avatar
0 votes
2 answers
2k views

global $product is empty string when passed into function

I'm trying to optimize my functions.php file as I have a Woocommerce site with a bunch of customizations to the theme. Currently, my functions look like the following: add_action('wp_footer', '...
sackadelic's user avatar
1 vote
1 answer
47 views

How to prevent XSS alter custom global javascript object & methods in Wordpress

Im working on a project in Wordpress that enqueues multiple .js files where each file adds a new method to a global javascript object, this in order to make the desired methods available only if ...
Pappageorgio's user avatar
0 votes
0 answers
45 views

Define global custom post

I have create a custom post object with custom post metas. Like this: $post->custom_meta = get_post_meta(); $custom_post = $post; Am I how to define the $custom_post object to global in master ...
km onur's user avatar
  • 159
0 votes
2 answers
3k views

What is the alternative code to if (isset ($_POST) && !empty ($_POST) to avoid warnings?

I am trying to insert some php code to my WordPress website but it gives security warnings perhaps due to directly accessing $_POST variable. Instead of $name = $_POST['name'];, I can use $name = ...
Asmat Ali's user avatar
  • 139
0 votes
1 answer
1k views

Custom global variable not working in function

I've been stumped on this for a while and cannot for the life of me figure out why my function isn't able to reference my global variable. $query_obj = get_queried_object(); if ( is_a($query_obj, '...
tylorreimer's user avatar
0 votes
0 answers
225 views

Hide payment method based on user role AND minimum cart value

Around April this year, we have to move our business 100% online due to the pandemic. Physical store hardly get one customer per day. after 5 months, we recognizes pattern; almost all failed COD ...
Zul Keith Lee's user avatar
1 vote
1 answer
760 views

Getting List of all registered Dashboard Widgets

I am trying to get a list of all registered meta boxes in the wordpress admin, specifically the Dashboard widgets. Now this is easily achieved with the global variable $wp_meta_boxes and does exactly ...
Mark's user avatar
  • 11
0 votes
1 answer
2k views

keep a variable for all pages in memory from the url

I downloaded a referrer plugin and it works fine I create my custom link and everything is fine: https://testsite.com/?r=ty5x I have a conversion system that tells me how many users have come to my ...
user14055335's user avatar
5 votes
1 answer
586 views

How to deal with global information without creating a Singleton class

I am trying to walk away from the Singleton pattern when it comes to develop plugins for WordPress. Why? Because I have been reading in some sources (here, here, here, here ...) and came up with the ...
Álvaro Franz's user avatar
2 votes
1 answer
5k views

What is the global $wp object used for?

In the wordpress documentation for Global Variables, I did not find information about the $wp global variable. I found a plugin which declares the global $wp variable as follows: public function ...
Álvaro Franz's user avatar
0 votes
1 answer
85 views

What is the use of $content_width?

In the requirements of Envato I saw that they require a $content_width to be set. I searched for it on the internet but can't really understand why? I don't use it and the theme is responsive. If ...
ralphjsmit's user avatar
0 votes
2 answers
554 views

Woocommerce Multisite global search how to mod ajax function?

I created an installation of my multisite ecommerce to differentiate the men's and women's departments. Often, however, it happens that in the men's department they do research related to women's ...
Vito Santimone's user avatar
0 votes
1 answer
724 views

External api call and make global variable for any page visitor enters , page-home, page, single etc

I am totally new to the wordpress world so excuse me if my question is not totally clear to you... I wanted to implement the following scenario for my website, The case was to call an external api ...
Kap40's user avatar
  • 1
1 vote
0 answers
329 views

Create a new query in function.php to filter blog posts

My goal is to filter blog posts (those which are requested in the blog page - Reading settings) depending on an expiration date, and then to change their category depending on a choosen future ...
Sioban's user avatar
  • 11
0 votes
1 answer
295 views

Global $wp_admin_bar always returns null

Developing a plugin and looking to access the $wp_admin_bar global variable and it always returns null? I can output $wp_version fine? function test() { global $wp_admin_bar; var_dump($...
Jimmy Howe's user avatar
-1 votes
1 answer
31 views

Suppress errors when using global function

I'm using the NinjaForms plugin which defines a Ninja_Forms() global function. Although I can call it without problem, I still get a warning/error Undefined function 'Ninja_Forms'. If this were a ...
lonix's user avatar
  • 301
0 votes
0 answers
487 views

Insert variables into wpdb in custom post template

I'm rewriting this to try to make it easier. I am inserting into a database table information which is sent when I click on a button in the page. When I insert the data it works, unless I try to ...
RichyR's user avatar
  • 3
0 votes
0 answers
83 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 ...
Matthew Brown aka Lord Matt's user avatar
0 votes
0 answers
67 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 ...
Marc's user avatar
  • 357
1 vote
1 answer
1k 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 ...
user avatar
0 votes
2 answers
1k 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 ...
i.brod's user avatar
  • 125
0 votes
0 answers
3k 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 ...
Max's user avatar
  • 1
0 votes
0 answers
61 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();...
Alex's user avatar
  • 305
0 votes
0 answers
27 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 ...
GaryL's user avatar
  • 101
0 votes
2 answers
610 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' =>...
Mohammad Asif's user avatar
1 vote
1 answer
2k 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 { ...
Shoaib Saleem's user avatar
0 votes
0 answers
464 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 , '...
middlelady's user avatar
1 vote
1 answer
2k 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 ...
Kristián Filo's user avatar
0 votes
0 answers
697 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 ...
user avatar
0 votes
2 answers
3k 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 /* ...
sridharnetha's user avatar
2 votes
1 answer
2k 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 ...
celebrian's user avatar
0 votes
1 answer
1k 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 = ...
richardevcom's user avatar
1 vote
1 answer
1k 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 ...
mage_ento's user avatar
0 votes
1 answer
30 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 ...
Zeth's user avatar
  • 918
0 votes
2 answers
5k 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 ...
Simo Patrek's user avatar
10 votes
3 answers
14k 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?
Nimesh's user avatar
  • 441
-1 votes
1 answer
245 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'...
Geeocode's user avatar
  • 161
0 votes
2 answers
376 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: ...
Michele Fortunato's user avatar
4 votes
2 answers
10k 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 ...
Juan David's user avatar