Solved

wordpress, trying to pass a php variable to sidebar.php (called through a function)

Posted on 2010-11-22
8
2,063 Views
Last Modified: 2012-05-10
On my single.php in my theme, I run a php script that ends up with two variables, like this:

$test1='test1';
$test2='test2';

I need to pass that information to the sidebar.php. sidebar.php is called and positioned into the single.php page like this:

<?php get_sidebar(); ?>

and apparently the php variables are not passed to it. If I try to echo them, I get no results.

Thanks for any help,  Chris
0
Comment
Question by:St_Aug_Beach_Bum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 34193550
On the page where the variable is declared use a hidden field to hold the value:

<input type="hidden" name="test1" id="test1" value="<?php echo $_POST["test1"]; ?>" />
<input type="hidden" name="test2" id="test2" value="<?php echo $_POST["test2"]; ?>" />

Then on the page where you want to use them:
$test1 = $_POST["test1"];
$test2 = $_POST["test2"];

Check out the php manual for details.

http://www.php.net/manual/en/language.variables.external.php
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34193626
Hi jmiller1979, just tested and that's still not passing it to the sidebar.
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 34197483
hi,

can you better explain how your single.php is running...

when you say you run a script, do you mean inline php as part of single.php or do you mean you run a separate script with something like curl?

As long as you set $test1 and $test2 before including the sidebar then those variables should be available to the sidebar. From wordpress site,

get_sidebar: Includes the sidebar.php template file from your current theme's directory. If a name ($name) is specified then a specialized sidebar sidebar-{name}.php will be included.

So you need to set $test1 and $test2 before/above where the sidebar is included.

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:St_Aug_Beach_Bum
ID: 34199763
It' just inline.

So if I do this in single.php:

 <? $test="test"; ?>
 <?php get_sidebar(); ?>

Then put in the sidebar.php:

<? echo $test; ?>

the text 'test' should display on the post page, but it doesn't... or do I need to do something else to make it work?
0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 34200198
yes, maybe I am reading this wrong, I have tried both with get_sidebar() and with include_once(TEMPLATEPATH . '/sidebar.php');

and neither way is able to see variables set in page.php even though page.php is the page including sidebar.php and the variable is available at the point in the code.

According to php manual

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

inside sidebar.php if i declare my variables as global it can see them


so in sidebar.php

change to

<?php
global $test;
echo $test;
?>

0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 34200243
Ah, yes, that works.

Interesting... I don't understand the global setting very much, other than it's supposed to make the variable globally accessable I guess... but I would have thought I would have had to declare that it was global in the single.php.  It seems like if it was passed to sidebar.php (which apparently it is since it can be accessed that way), it would show up without declaring it as global at that point.
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 34200307
yeah, global in php works kind of oppsosite than that. You tell your variable when you declare it, that you actually want access to the variable of the same name in the global scope, not that you are declaring a new global variable.
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34200472
It works though!  Thank you very much!
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Now that you've installed WordPress 2.9 (http://www.experts-exchange.com/articles/Web_Development/Blogs/WordPress/WordPress-2-9-What-to-Expect-When-Upgrading-to-WordPress-2-9.html?) on your site, you need to install some plugins to get the most out …
How to install BuddyPress on your self-hosted WordPress site It’s been called everything from “social networking in a box” to “Facebook without the terms of service,” but until Feb. 16, BuddyPress was a relatively unknown outside the WordPress MU…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question