The tag has no wiki summary.

learn more… | top users | synonyms (1)

12
votes
5answers
3k 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 ...
11
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
1answer
106 views

Should we trust the post globals

@toscho left a comment to this answer which had me thinking again. How much trust should he have in the global scope, specially regarding the post globals like $post. So what? The global variable ...
9
votes
2answers
493 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
218 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
345 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 ...
3
votes
3answers
22k 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 ...
3
votes
2answers
47 views

When to use global $post and other global variables?

I am trying to understand when and why I should use the global $post variable. I have tried to do the following on a post page, outside the loop, and as expected it works. <?php ...
2
votes
2answers
554 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 ...
2
votes
1answer
4k 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
435 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
2k 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
733 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
1answer
102 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
1answer
38 views

Add classname comment template from functions.php

I want to add an extra classname to my comment template (the div wrapper before the post itself) which is stated in functions.php. The classname in functions.php is an extra field in my users profile ...
1
vote
2answers
260 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
5k 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
718 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
140 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
946 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
43 views

When and Where is `global $post` Set and Available?

Usually when people think of the global $post object it's assumed that it's actually set whenever you enter The Loop. I started to play around with how early I could actually call global $post which ...
1
vote
2answers
291 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
813 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
752 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
853 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
429 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
125 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
134 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
7k 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
3answers
794 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
1answer
125 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
4answers
678 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
1answer
3k 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
101 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
48 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
27 views

Use a variable created in get_header to calculate stuff in wp_footer

i am trying to create a small time tester that loads at get_header hook and the using the value does something in wp_footer... i have tried using global $var i tried putting the global var inside ...
0
votes
2answers
671 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
556 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
825 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
302 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
867 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
42 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
100 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
68 views

how to get the post id in plugin

I have a plugin that I'm working on and I'm having trouble getting the post id so jquery can use it. I'm able to get it using the admin_head hook but I'm not sure how I can return it to jquery. I'm ...
0
votes
1answer
83 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
49 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 ...