The tag has no wiki summary.

learn more… | top users | synonyms (1)

12
votes
5answers
2k views

How to include code only on specific pages?

I'm learning about plugins and shortcodes. I noticed that when I activate my plugin its code gets loaded on all of my pages-- even pages that don't have my shortcode. (I don't mean content vs. admin ...
10
votes
4answers
6k views

WordPress Multisite - global categories

Setting up a WP multisite instance - the client has an existing ontology / set of categories that they want to classify all content across the set of blogs. Also the desire is that any new categories ...
9
votes
2answers
420 views

What is a good alternative to using $content_width for image optimization?

The $content_width GLOBAL in WordPress effects many themes and even some plugins by limiting the size of images and embeds in posts, it is a requirement for themes submitted to wordpress.org. It is ...
6
votes
1answer
2k 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: ...
5
votes
2answers
183 views

What is the difference between using global $current_screen and get_current_screen()?

I have been looking at get_current_screen();. I have seen there is also a global $current_screen; that I could use. Here are two examples: // Using function function wpse_post_notice() { ...
4
votes
2answers
2k 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 ...
4
votes
4answers
2k 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 ...
4
votes
1answer
239 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 { ...
4
votes
2answers
4k views

query_posts() in function makes global $wp_query out of sync?

This has me stumped for a while already. Either I am missing something very obvious or something very not obvious. I am also not entirely sure if this has something to do with WP or purely PHP ...
2
votes
3answers
14k 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 ...
2
votes
1answer
3k views

get_query_var vs global query variables?

I have query vars defined in functions.php using: add_filter('init', 'add_query_vars'); function add_query_vars() { global $wp; $wp->add_query_var('profession'); } Until now, to ...
2
votes
1answer
390 views

Pass variables from one widget to another widget

I wrote a widget but I need to use the variables from one widget in another...both widgets are in the same dynamic sidebar. This is what I am trying to do: I wrote a widget that adds a footer to the ...
1
vote
2answers
1k views

Using query_posts inside single.php loop

Inside of my loop in single.php, I used a custom query using get_posts to return posts belonging to a certain category. <?php global $post; ...
1
vote
1answer
568 views

Passing variables to templates (alternatives to globalizing variables)

I want to make my WP code simpler (there's some mess within header.php, page.php etc.). So, for example, I want to set something like this global: $foo = get_option('foo'); So $foo will work ...
1
vote
2answers
212 views

Should I global $wpdb outside of any of my plugin's functions scope?

I know that the general advice to global variables is to not use them at all. Still, when doing something with the Wordpress database, it is required to do global $wpdb; to access the methods ...
1
vote
1answer
65 views

Global functions on WPMU

I work in a multisite environment where all of the sites are related. A lot of recent developments have required me to copy and paste certain functions into many different theme folders, which causes ...
1
vote
2answers
175 views

What are the disadvantages of using global variable?

I am a newbie at WordPress. In my "recent posts" shortcode, I used global $post variable, because the_title() function was giving me page's title, not post's title. I am not exactly sure, but I ...
1
vote
2answers
3k views

Defining a global array in functions.php?

I try to create an array of years, which is avaible in my template-files and in at least one other function in the functions.php. function get_the_years() { global $year_arr = range(2001, ...
1
vote
1answer
541 views

Using global $post v/s $GLOBALS['post']

It's probably more of a PHP question, but I would like to know if there are any differences in using global $post; echo $post->ID; when compared to echo $GLOBALS['post']->ID; to access the ...
1
vote
2answers
122 views

How should I store global information such as a phone number so that it is editable through the CMS?

Right now I have a p element in header.php containing the address and phone number my client wishes to use for contact info. How can I set this up so that it is editable through the WP admin area so ...
1
vote
1answer
829 views

How/where is the global variable $wp_registered_widgets filled?

I have a problem of widgets not showing up, trying to investigate why $wp_registered_widgets is empty. Can somebody tell me where it gets filled from the database? How can I find out why the default ...
1
vote
2answers
241 views

Retrieve the template directory URI via global or get_template_directory_uri() every time?

One Wordpress function I happen to use frequently is get_template_directory_uri(). While I tried to minimize or avoid its use where I can, I still happen to have more than one call to this function in ...
1
vote
2answers
763 views

Call External Object in Class Function During Callback

I've a class which has a function that check and creates table in database. In order to do so i need to use WordPress $wpdb object. I need the function to run only on first plugin activation, so i ...
1
vote
1answer
683 views

How to make global post work with custom post type?

I was wondering to know if it's possible to use global posts with custom_types. I was working with this function: global $post; $myposts = get_posts('numberposts=4&offset=1& category=3,4'); ...
1
vote
1answer
743 views

wp_get_current_user always returns 0 continued

I have been busting my head trying to get the user ID with every piece of code possible but it always comes back "0". Not sure what I'm doing wrong. I started out using this: <?php $user_info = ...
1
vote
1answer
396 views

Getting $curauth->ID to work inside a shortcode

Im wanting to show a profile/logo pic on author.php via a simple shortcode: function wpaluploader_showauthorimage() { $wpaluploader_authorlogo = '<img src="' . get_bloginfo('url'). ...
1
vote
1answer
25 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
1answer
120 views

Change global values from templates

I am trying to check for a php variable in each of my template in order to assign it a different styling but the value seems to disappear, can you please help, I already spent hours trying to figure ...
0
votes
2answers
5k views

How to set global variable in functions.php

I want to be able to echo the URL of the featured image of a post and after looking some on the web I found the following which works fine when I put it in a loop in my front page template. <?php ...
0
votes
1answer
88 views

How can I save custom meta to one global value?

I have added a custom meta box to one of my custom post types. It contains a checkbox that will indicate that this is the "featured monthly product" (there can only ever be one). When I check this ...
0
votes
3answers
719 views

How can i use a global variable in a .css file generated with php?

I am including a .css which i parse with php like so <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_childTheme_url(); ?>/css/productgallery.php" /> Now in ...
0
votes
4answers
575 views

Does WP have a global of $id?

I'm trying to attach an object to every post of any type. This will require post ID to populate or create my object. So, I either call global $post, or get_the_ID(), or do get_post(). I hope there is ...
0
votes
2answers
93 views

Will $current_user no longer be global?

I just had a look at Global variables in WP and I discovered that a lot has changed since the last time I looked. $current_user is no longer listed. Does that mean this and many other (old) globals ...
0
votes
2answers
36 views

Test if php document executed by Wordpress or directly

Active plugin PHP files are interpreted automatically when you view a page, post, admin page, etc. I could also run a plugin PHP directly if I knew the fully qualified URL, however it will most ...
0
votes
1answer
2k views

How to call a function only once (global variable scope)

I have been trying to work out the most effecient way to use functions in WordPress. I have a big slow function big_slow_function() that ideally is only run once. But I need to use the boolean that ...
0
votes
2answers
435 views

Is $page a global variable in wordpress?

I am having a problem with a local $page variable, especially when using it in The Loop. Is it a reserved variable of Wordpress? Also where can I find a list of all those global variables?
0
votes
1answer
489 views

get_posts() and global variables

I was reading this post: When should you use WP_Query vs query_posts() vs get_posts()?, which seems to have become the go-to post for understanding the difference between the different functions for ...
0
votes
1answer
734 views

Passing global variable from header.php to functions.php

Situation: Is header.php file where is code: if(is_single()){ global $my_global_var; $my_global_var == true; } Is functions.php file where is code: global $my_global_var; if($my_global_var == true) ...
0
votes
1answer
234 views

Where is this function's callback getting its arguments from?

This might be better suited to Stack Overflow, thought I'd try here first though. I was walking through this tutorial by Justin Tadlock and encountered the function below being used as the callback ...
0
votes
1answer
792 views

What does the Global Variable $s represent?

What does the $GLOBALS['s'] variable contain? It looks like an empty array, but what would it normally hold? I've encountered it in a tutorial: The WordPress Theme: Single Post, Post Attachment, ...
0
votes
1answer
41 views

What's the safest way to switch and restore a post global?

I need to switch WordPress's post context, sort of like switch_to_blog() but for posts. What's the safest way to alter WordPress's global state and switch it back?
0
votes
1answer
99 views

How to avoid repeatedly use the new statement to instantiate a class? [closed]

I have a Class with a lot of variables. To get a variable, I have to write this line in each function: $x = new MY_Class(); I guess there has to be another way? I tried: function my_data(){ ...
0
votes
1answer
52 views

Php file that doesn't recognize wordpress functions

I have a custom contact form that I am trying to process in other file: The form: <form method="post" action="<?php echo get_template_directory_uri() . '/templates/contact.php' ?>" ...
0
votes
1answer
38 views

Understanding WordPress Search

I am trying to create a custom search similar to the one on totaljobs.com but through a WordPress site. Essentially I would be able to search for both search terms and location simultaneously. The ...
0
votes
2answers
271 views

Using global $post; to get featured image for custom post via WP_Query

I'm using a site-wide function to add css to the <head> of the page pull in featured images of various as a responsive page background. It works great across the rest of my site but fails on a ...
0
votes
1answer
26 views

What's the right way to share data between widgets?

I've got a few custom widgets in my site, which I'm targeting to specific tags. There may be a case where there may be an overlap in widget targeting, and the desirable behaviour may be to only show ...
0
votes
1answer
73 views

Display Post Meta in Header

I'm trying to get page post meta in my header so I can set some meta tags. I tried to get the global post variable but it only return true. <?php global $post; global $wp_query; echo ...
0
votes
1answer
153 views

How to get particular data from wp_list_comments outside the loop?

I need some help,very much new to wordpress. I need only comment title and comment date from wp_list_comments function in my post, calling from outside loop, after declared $postid as global ...
0
votes
1answer
39 views

how do i register global query in template

ok i think this is a basic php question but here goes: i have this working code: <!-- show the bands playing --> <?php $child_posts = ...
0
votes
1answer
24 views

How to save a viewer specific option

A minimal example of my problem would be default background colour is white a custom sidebar widget offers to switch background colour to green when switched to green, background shall stay green ...