We’re rewarding the question askers & reputations are being recalculated! Read more.

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
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 ...
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
63 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
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. ...
4
votes
1answer
588 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
217 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
75 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 ...
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 ...
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 ...
0
votes
2answers
174 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
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 ...
6
votes
1answer
132 views

Global Objects and Public Methods

I'm developing a plugin and am just thinking over best practices. How common is it to have a global object? Currently on plugins_loaded I am creating a global object of my class: add_action( '...
0
votes
1answer
132 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
1answer
123 views

Use a select box to change a php variable

I am currently stuck on a little problem. I'm using a localStored value to differentiate the content that user sees depending on their choice. <div class="form-group form-inline hidden-print"> ...
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 ...
0
votes
1answer
9k 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 $...
22
votes
3answers
6k views

How to: inspect global variables in WordPress

People are often confused about how to get data from global objects/variables Question: In which ways can you inspect global variables? This Q was written because it's needed pretty often over here ...
0
votes
1answer
728 views

How to get submenu of admin menu?

I user global $menu in one of my plugins to get the list of menu items. But i would like to get the list of of items from a submenu for example from Posts i would like to get Add new, All, Edit ...
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 ...
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
1answer
232 views

global $post inside plugin query messes up every new post page in wp-admin

I have a very strange problem that I am trying to troubleshoot: I have a query (to build an array) that I run on a custom post type inside my plugin. It looks like this: function makePointerQuery(){ ...
0
votes
1answer
63 views

Is it advisable to use $post->comment_count instead of get_comments_number( $post_id )

I was learning from the WordPress codex about global variables and after var_dump($post) I found out that $post has a property called comment_count I was wondering whether I can directly use $post-&...
2
votes
1answer
1k views

How to access global $wp_meta_boxes variable on front-end?

Is there a way to take the global $wp_meta_boxes variable to be used in the front-end? Currently the $wp_meta_boxes is accessible on specific admin pages only. I've found a similar question about ...
1
vote
2answers
491 views

How to fix a “globals” issue to avoid a rejected theme?

I submit a WordPress theme for Themeforest and one of the reasons for rejection was as follows: Globals should always be within a function or a class and should be used restrictively & only if ...
6
votes
1answer
3k views

get_post() vs global $post or $GLOBAL['post']

I'm writing a widget plugin and need access to post information. I've seen people using 3 different approaches: global $post; if ( isset( $GLOBALS['post'] ) ) $post = $GLOBALS['post']; $post = ...
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
712 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 ...
7
votes
3answers
1k views

Any advantage of using wp_scripts and is_IE when enqueuing scripts

I read on the WP docs, which pointed to this gist that the correct way to enqueue styles for IE is by using the $wp_styles. I'm guessing that this would be true then for scripts as well. Take these ...
0
votes
1answer
1k views

Retrieve post in AJAX Callback

I have a JS file that triggers a PHP callback file. The PHP file is supposed to work on the Post that is currently opened (wp-admin/post.php). I do not want the JS file to pass the post id though ...
2
votes
1answer
1k views

Multisite Global Custom Posts

I have a museum client with a multisite setup with pages that detail the exhibits in their facility. They recently added tablets throughout the museum and want their tablets to display only their ...
0
votes
1answer
227 views

Declare global variable that can be used on other function

I have AJAX image upload function where I can get attachment_id value. I want to save that as global variable and use it in different function that sets post thumbnail when it's exist. This is what I ...
0
votes
1answer
69 views

Array is not working in Filter?

This doest not works: Warning: in_array() expects parameter 2 to be array, null given $non_area_page = array(160951,160955); // This does not work add_filter( 'term_link',change_term_name , 10, 3 ...
5
votes
3answers
6k views

Does an activated plugin automatically mean its methods are available to other WP functions?

I made a WordPress plugin like this: Class MY_CLASS { //codes } Global $myclass; $myclass = New MY_CLASS (); After installed and activated the plugin, can I use this class in other plugins without ...
21
votes
1answer
957 views

Should we trust the post globals?

@toscho left a comment to this answer which had me thinking again. How much trust should we have in the global scope, specially regarding the post globals like $post? So what? The global variable ...
2
votes
1answer
2k views

JavaScript, best way to use data from the loop

I am trying to learn WP theme development. And at the moment I am trying to figure out how to use data from the loop in JavaScript. Lets say I got some JavaScripts functions in a external script-file ...
0
votes
2answers
88 views

Save last login date in global before change it?

I use this function to get the last time a user logged in. function jkn_last_login($user_login) { $user = get_user_by( 'login', $user_login ); global $last_login; $last_login = array(); ...
0
votes
1answer
52 views

apply_filters to $GLOBALS

I'm building a onepage website, using the Pootle Page Building plugin. With get_page, I show every separate page on 1 page. To target the Pootle Page Builder content, I'm using the following code: $...
9
votes
1answer
16k views

Passing variables from header.php to template and vice verca

I've defined a variable - let's call it $header_var, which is placed in the header.php file. I would like this variable to be passed onto my template file (in this case taxonomy.php). Also I would ...
0
votes
0answers
688 views

Setting a cookie in functions.php and it gets deleted when working on an other function

My first function gets the parameters from the URL and sets the cookies: /** Get variables from the URL and set them into the coockie **/ add_action( 'init', 'set_utm_cookie'); function ...
0
votes
0answers
114 views

Popup panel is only displaying 1 entry ignoring all others

I have a popup panel being used for when someone clicks on view bio it will display their bio along with their name and headshot. As of right now I have four members being displayed M1 M2 M3 and M4. ...
0
votes
0answers
66 views

Best way to access variables in template markup

I am making my own mini OOP wrapper for working with wordpress themes. I have a main $settings variable that contains all of the information like definitions, scripts, menus etc.. I then pass this ...
0
votes
1answer
2k views

'global' not working in wordpress?

My wordpress theme consists of the file loop.php which just does this: <?php include("AAAA.php"); ?>. Now AAAA.php looks like this: <?php $test = 1; function f() { global $test; ...
0
votes
1answer
152 views

Editing Global Variables from Inside Functions

I'm working on my first plugin and I have two separate functions inside to accomplish two different task. Each function is added to a different hook, one using actions and the other using filters. ...
4
votes
2answers
2k views

Registering AJAX callback function that is part of a class without instantiating the class in function.php

I'm not even sure if this is possible. But nevertheless I thought of asking. I'm in habit of wrapping/grouping AJAX calls for a particular need within a class. See below. class my_class { ...
7
votes
3answers
856 views

Reducing the use of global $post

When creating meta boxes, in each meta box function it seems a reference to the global $post is passed as a parameter ($event). I prefer this as it seems consistent and less likely to fudge the $post ...
4
votes
2answers
437 views

Why declare $post globally?

I'm new to Wordpress development and am seeing examples everywhere that have a line to declare the $post variable explicitly via: global $post; But I've tested my uses without that line and it makes ...