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
2 answers
112 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 = ...
0 votes
0 answers
31 views

Using global variables

Every page in my blog starts with an include file. When listing "all posts" this header will appear many times on the page. To avoid this I just introduced a global variable that is ...
  • 3
4 votes
2 answers
3k 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: ...
  • 111
1 vote
0 answers
41 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 ...
  • 11
0 votes
0 answers
42 views

The Global Styles missing

I am working on my first block-based theme, and trying to leverage theme.json as much as possible. I would like to allow client to change the the fonts, colors and other variations defined in the ...
  • 186
1 vote
0 answers
39 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 ...
  • 11
0 votes
0 answers
66 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 ...
  • 11
0 votes
1 answer
324 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; ...
0 votes
1 answer
890 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', '...
1 vote
1 answer
35 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 ...
0 votes
0 answers
44 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 ...
  • 109
0 votes
2 answers
2k 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 = ...
  • 129
0 votes
1 answer
100 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, '...
0 votes
0 answers
132 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 ...
1 vote
1 answer
412 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 ...
  • 11
0 votes
1 answer
1k 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 ...
5 votes
1 answer
412 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 ...
2 votes
1 answer
3k 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 ...
0 votes
1 answer
44 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 ...
0 votes
2 answers
447 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 ...
0 votes
1 answer
450 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 ...
  • 1
1 vote
0 answers
253 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 ...
  • 11
0 votes
1 answer
206 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($...
-1 votes
1 answer
24 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 ...
  • 301
0 votes
0 answers
405 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 ...
  • 3
0 votes
0 answers
52 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
0 answers
63 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 ...
  • 327
1 vote
1 answer
749 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
602 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 ...
  • 125
0 votes
0 answers
2k 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 ...
  • 1
0 votes
0 answers
57 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();...
  • 305
0 votes
0 answers
23 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 ...
  • 101
0 votes
2 answers
572 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
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 { ...
0 votes
0 answers
372 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 , '...
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 ...
0 votes
0 answers
541 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
2k 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 /* ...
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 ...
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 = ...
  • 161
0 votes
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 ...
0 votes
1 answer
26 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 ...
  • 718
0 votes
2 answers
4k 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 ...
10 votes
3 answers
11k 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?
  • 431
-1 votes
1 answer
202 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'...
  • 161
0 votes
2 answers
307 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
2 answers
8k 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
1 answer
53 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
0 answers
79 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 ...
  • 73
0 votes
1 answer
74 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'...
user avatar