Wordpress Bug?

Has anyone bumped into this problem… ?

If i was to put
header.php

<?
$Testing = "Pass This String"; 
?>

and in page.php

<? 
header();   // is called and right after that i put 
echo "Grab String: " . $Testing; 
?> 

It doesn’t pass it through the wordpress function.

Does anyone know what i can do about this problem?


include 'header.php';
echo "Grab String: " . $Testing; 

header() is an important PHP function, you seemingly want to include a file and access a variable contained in it.

yeah i just tried include header.php and it worked. Any difference as to using header(); and include header.php?

header() has nothing to do with header.php.
header() is used to send header output to the browser and not execute code. Include (and replace, and their _once additionals) imports code from an external file.

Yes, there is a huge difference. As Cups explained, header() is a very important core PHP function to modify the HTTP header of a page. It has nothing to do with Wordpress itself nor it’s header page.

well if you want me to be extremely specific then “get_header();”
Cups knew what i was talking about.

get_header(); grabs the header.php

If i removed that function then it ignores the header.php file on the website.

Well then, the solution to your problem is not to misspell your function name.

Looking at the WP Function Codex, there is no difference between get_header() and include(‘header.php’), in the same way that there is no difference between get_header(‘john’) and include(‘header-john.php’). That’s just my reading though.

ok forget the typo in the posting here… header() is get_header() there’s no such thing as a header(); in wordpress codex… just to put that out of the way…

there’s a get_header() and there’s a wp_head();

So you’re saying get_header() didnt work at all? Or it worked but didnt parse certain functions, or…?

there’s no such thing as a header(); in wordpress codex

Just to clarify, header() may not be part of the wp code, but it is a fundamental PHP function, and as WP is built with PHP ergo it is therefore part of the code.

WP are free to create a function get_header() which might well call header() for all I know. That I helped you seems to have been a bit of an accident, oh well, as long as you got your code working - that’s the thing.

In essence, the get_header() function looks like the following (in reality it’s slightly more complicated but lets not distract from the point of the post):


function get_header() {
    require TEMPLATEPATH . "/header.php";
}

The important point is that the require() happens inside of the function. If you haven’t already, have a read through the include() docs (since require() behaves the same way with regard to variables). The important points to understand are: [i]When a file is included, the code it contains inherits the [url=http://php.net/variables.scope]variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available [in that scope] within the called file, from that point forward .[/i]

Since the require() happens within the body of the get_header() function, then the variables within the header.php file will be local to the function. They will be available within get_header() but not outside.

Of course, if you really, really wanted to make the $Testing variable available from within page.php then there are things that you can do. The options are many and varied, so giving one or two might do you a disservice. What are the motives for declaring the variable in the header only to use it in the page.php? Will it get used elsewhere, did it just seem convenient, are there many other variables you want to do the same with?

yeah I found the function; which makes sense. So in that case. How would someone grab a string from inside the header.php through the get_header function?

i could add extract($GLOBALS);


function get_header( $name = null ) {
	do_action( 'get_header', $name );

	$templates = array();
	if ( isset($name) )
		$templates[] = "header-{$name}.php";

	$templates[] = "header.php";

	// Backward compat code will be removed in a future release
	if ('' == locate_template($templates, true))
		load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}

Well ideally you wouldn’t. But, if you -really- feel you HAVE to, then…

Salathe might correct me on this, but if you declare a [FPHP]global[/FPHP] inside your header.php, it should still be in scope after the function.


<?php
function moo() {
 global $a;
 $a = 2;
}

moo();
echo $a; //2
?>

Though again, if you’re wanting variables, why not just declare them before you get_header()?