WordPress global $post variable is empty - php

I'm working on a wordpress plugin(managing background image), when I try to use
global $post;
print_r($post);
the object is empty.
Probably there is some required data (I dont know what.)
Please help me if you know.

The WordPress global variable $post contains the data of the current Post within the The Loop.
This means that WordPress will assign a value to this variable in each loop iteration. Consecuently, if you're trying to access $post from anywhere in your code outside the WP loop, it will be of course empty...

Related

Wordpress PHP using global constants to avoid hard-coded values

I am developing on a Wordpress website with my own php coding. So far I am using the Snippets plugin which I like most for adding PHP code to existing wordpress sites.
The only thing I would like to know is how i can use something like global constants to avoid hard-coded values. Because I am using the same values again and again. What is the best way?
Thank you for any help.
best,
It is not recommended to create your own global variables on Wordpress (read this). But you you can achieve this by defining global variables.
function my_globals() {
global $myglobals;
// We define it as an array so you can store multiple values. If only needed one value you can directly set it
$myglobals = array();
$myglobals['first'] = 'This is first content';
$myglobals['second'] = 'This is second content';
}
add_action( 'after_setup_theme', 'my_globals' );
Now you can call your global using:
global $myglobals;
echo $myglobal['first'];
echo $myglobal['last'];

Passing MULTIPLE vars with add_rewrite_endpoint fails

I have read many tutorials about passing multiple variables to a post/page in wordpress, using add_rewrite_endpoint. I am currently passing 2 variables, calendar_year / calendar_month . It works good for both but separately each one. Using both like (http://myserver/mysite/availability/calendar_year/2016/calendar_month/10/) only gets the year and if I pass them on reverse, only gets the month. Availability page shows correct (No 404 error), but it fails taking the last parameter when there are the two of them. Here is my code for add_rewrite_endpoint
function booking_calendar_add_endpoints() {
add_rewrite_endpoint('calendar_year', EP_ALL);
add_rewrite_endpoint('calendar_month', EP_ALL);
}
add_action('init','booking_calendar_add_endpoints');
And this is how I get it and use it:
global $wp_query;
$year_var = $wp_query->get( 'calendar_year' );
$month_var = $wp_query->get( 'calendar_month' );
I am sure I am missing something. Does anyone have a solution for it? Thanks.

Passing a php variable into a gravity forms hook

I think I'm getting really confused with return'ing and echo'ing variables.
I've got this gravity forms hook from their support...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return 'boom!';
}
This works and returns 'boom!' as my form field default variable.
This is pretty straight forward for a general text string, but I am trying to return a PHP variable instead.
I am loading the facebook PHP SDK in my functions.php at a higher scope than the gravity form hook. The facebook SDK definitely works, for example I am currently echoing this in my wordpress theme files...
echo $userData['name']
But my question is, why does it not work if I try and return the above variable inside the gravity for hook?
Please see what I have tried below, but it returns nothing...
add_filter('gform_field_value_facebook_name', 'my_custom_population_function');
function my_custom_population_function($value){
return $userData['name'];
}
I've also tried something similar in my wordpress functions.php, when trying to echo a variable in a filter...
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
echo $fb_app_id;
}
But this returns nothing and the scope is the same.
Can anyone please enlighten me to why I can't pass these variables around. I think thats the technical term. Thank you very much.
This is because in PHP, functions don't read global variables without the global keyword.
$fb_app_id = '12345678910';
// APP ID FILTER
add_action( 'fb_app_id', 'echo_fb_app_id' );
function echo_fb_app_id() {
global $fb_app_id; // tells PHP to use the global variable
echo $fb_app_id;
}
Try to add global $userData; to your my_custom_population_function.

How to get id of post inside of plugin

I want to retrive post id inside plugin.
I tried
global $post;
$a_Id=$post->ID;
and
global $wp_query;
$thePostID = $wp_query->post->ID;
and
var_dump(get_the_ID()); //shows just null
How i can retrieve it?
The idea is to get language of post from Custom Fields
and feed it into Global Translator plugin as a BASE LANG
EDIT:
I can retrive id from $_GET['p'] on development server but on production i
have pretty urls so i dont have it.
Assuming that you know what the ID is for an particular post you could use
var_dump(get_defined_vars());
to show a listing of anything that you can get your hands on.
Look through the output for the ID that you know and then use the path that it shows to get there.
What are you doing with the post ID? When exactly do you need it? I'm guessing you are using this very early in your plugin file, when the query is not yet parsed. The query is parsed after the hook parse_query is fired.

Getting Post Information Outside the Wordpress Loop

I know there are functions to like is_single() that will return data about the page, but I'm looking for a way to get the following information outside of the loop:
The category of the single post.
AND
The title of the single post.
All I would really need is the post ID in question and I could get all the other information. I've looked through the functions reference in the codex, but I haven't found anything. Is this impossible because the script doesn't even get that information 'til the Loop runs?
(I would need this information in both the header and footer, so before and after the PHP script for the loop, if that is a problem.)
Hopefully someone can offer some insight.
EDIT: To clarify: I want the information to be from the post that is loaded in the loop on the "single" page. (AKA the post they are viewing.) So how would I get this ID in the first place? Basically, when viewing a post, I want to get its category or title, but not while the loop is going.
This is actually quite simple.
// Gets an array of post objects.
// If this is the single post page (single.php template), this should be an
// array of length 1.
$posts = get_posts();
// The post object contains most/all of the data that is accessible through the
// normal functions like `the_ID()`, `the_author()`, etc
var_dump($posts[0]->ID);
This can be performed outside of and it doesn't affect the normal loop. For example, my single.php template looks like this (line numbers included):
1. <?php
2. get_header();
3. $posts = get_posts();
4. var_dump($posts[0]->ID);
5. ?>
6. <div id="post">
You could execute your own query and return a $post object and then access it's attributes through something like
echo $post->title;
Read about $wpdb and Custom Queries on the Codex. Basically you could run a SQL query using the ID as a where filter through its $wpdb object.
Another option, more WP-like, would be to use a Custom Query Post, where you could define another loop, returning only your post using its id:
query_posts('p=2010'); // 2010 being the post's ID
Then you could run the alternative loop and use the similar $post object to display some information.
This is what I use inside the loop to run an new query; I just tried it outside the loop and it works, on a single.php template page. This will give the title of the latest post in mycategory. I don't know how to pull the category ID, though.
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while($my_query->have_posts()):
$my_query->the_post();
the_title();
endwhile;
After the query has run, try running:
global $wp_query;
var_dump( $wp_query );
The information you are looking for is probably in $wp_query->query_vars or $wp_query->post
You can use the $wp_query global object, and address all returned posts like this
$wp_query->posts ; // contains an array of posts.
Then, if for instance you need to know if the first post is of a certain post_type, you can do this:
$first_post_type = $wp_query->posts[0]->post_type;
Couldn't you store the $post information in a separate variable on the page, then just echo the data you need out later when you need it (ie, outside the loop)?
This is predicated on needing the data after the initial loop has run.
The usual thing is to create "second loop" which would extract needed post, hence your needed data.

Categories