Saving the Wordpress header to a php file accessible anywhere? - php

I have been searching all day to figure this out and still cant find an answer. I want to take the header of my wordpress site and have its own file (headerstandalone.php). I want it to do all the functions and calling it needs to do without being in the same directory as the standard header.php. Is this possible?
I am basically trying to implement it into some custom pages.

Would this help you with your problem?
<?php include_once("headerstandalone.php"); ?>

I would put the headerstandalone.php in a seperate directory, be sure to include wp-load.php in your standalone file so you can use all your functions and then include that file in your main header using require_once(../another_directory/headerstandalone.php).
If you don't want to include the file in you header then you could add the standaloneheader.php code to a function and add it to either your own plugin's function file or the themes function file and call that function in the header.

Related

How do I get my current wordpress theme's header & footer in a PHP file?

The file is manually created by me & not a part of the theme.
I've tried by placing get_header( ) inside my file but it shows a error that get_header( ) is not defined.
Maybe you don't have default header and footer names. Then you need to set additional name.
Includes the header.php template file from your current theme's directory. If a name is specified then a specialised header header-{name}.php will be included.
If the theme contains no header.php file then the header from the
default theme wp-includes/theme-compat/header.php will be included.
Source: https://codex.wordpress.org/get_header
I highly discourage it but you need to include the wp-load.php file that resides in the root of your wordpress installation, in your lone php file. That is how you have access to WP functions and capabilities.
But again I HIGHLY DISCOURAGE IT. Find another way, create a simple plugin and activate it, in which you put that php file's code, or if part of a theme, just create a new page template with a different header, that you can call with get_header('name_of_header_without_php_extension') or a new footer called the same way but with get_footer().
There are alternatives, maybe if you share what you are trying to achieve, we can guide you to a better, safe solution.
Based on theme it's defer.
Same time define as get_header(),get_footer()
But now latest themse assign particular theme name based header & footer function
Ex: Theme Name is "Demo"
So the header function assign as Demo_get_header() or get_header_Demo()
Dashboard ->Appearance ->Editor -> in your Template file or in page.php write
<?php get_header()?>,<?php get_footer()?>
if you want to use different headers on different pages
1. create new file , name it header-example.php
2. you can call this header in any page using code

Include external php inside Wordpress Header

I am trying to do something simple but it won't work.
I have a custom Wordpress header. I need to include external php classes and other stuff. The problem is that if I do that the page breaks, it doesn't work anymore.
This doesn't occur when I try to "require" external php scripts inside page templates. The problem occurs when I include it in the header.
To make it shorter, I have:
header-home.php (standard wordpress theme header file, which has to include the following file)
snippets.php (a php class)
header.home.php is located at /wp-content/themes/twentyseventeen-child/templates...
snippets.php is located at /resources/scripts/snippets.php
Perhaps the header loads something that is not compatible with custom inclusions?
I was able to include an external php file which contains pure html elements. If I try to load custom classes, the page simply breaks. The include filepath is correct, so that's not the problem.
All the files that you are going to require, you must do in the file "functions.php" that is in "/wp-content/themes/twentyseventeen-child/functions.php", then in the file "header.php" you can instantiate your objects or call functions. For example:
Functions.php
<?php
require("./YourClass.php");
function getYourClass() {
$yourClass = new YourClass();
var_dump($yourClass); //Verify if it returns data and then delete this line
return $yourClass;
}
Header.php
getYourClass();//Here it brings the object and shows the output.

WordPress include file at header not working

I tried to include some php files in header.php. I use the script below :-
get_template_part( 'custom/tos_functions');
However get_template_part doesn't work for me, so I use include which works fine.
include (TEMPLATEPATH . "custom/tos_functions.php");
The issue is, if I put this line in header.php, I want to use some of the functions in custom template , say profile.php. In profile.php the file is as though not being called at all. I can't get the data I need in profile.php.
Then I tried to take out the include script from header, and place it in profile.php and the data I need can be called perfectly fine.
This will be an issue as there will be many custom php pages I need to create, thus every page will call the include script.
Question, why is it that the file cannot be called from header.php ? The data can only be retrieved within header.php and any custom page that calls the header, couldn't get the data from the include file.
Anyway I can fix this so that I can place the include file in the header.php?
Thank you!
Rather than including files in the theme template files (such as header.php), you should include them in the theme's functions.php file.
Including them in the template files is almost always too late in the WP run for it to be useful, and it may cause unexpected results and unexpected output in your templates.
Instead, in your functions.php file, do something like so:
require_once 'custom/tos_functions.php';
Then, your API code is available where you need it, and it is in the "right" place and consistent with the "WordPress way".

Why doesn't wordpress need to include anything before using a function

When I use get_header(); in index.php, it automatically load the header.php file and and need not to include anything in the page.
Generally index.php is the main page. everything should be include there before it was called. Am I wrong?
It seems you are checking index.php inside the theme. But that is not the entry point to the site, but the index.php file in the wordpress root. There you can see it has included some files and inside those they have included some other files. Like wise file including functions are written in the wordpress core codes. So, you don't want to worry about including files in your theme. if you call get_header() function, the function itself has coded so that the relevant file to be included. If you are going to use a separate file to code functionality other than standard wordpress theme files, you'll have to include it at the top of functions.php

wp_head hook including file not working wordpress

I am having some issue with site I am developing. Let me know you some of the pages are static php pages which are working fine. But the issue is with wordpress pages, I have all css and js files are included from header-required.php file. I am calling this header-required.php file from function file by wp_head hook. here is my code of function.php file:
function hook_javascript()
{
include('http://'.$_SERVER['HTTP_HOST'].'/inc/header-required.php');
}
add_action('wp_head','hook_javascript');
And I am calling this function from all wordpress file code wp_head();
here is the code:
while loading, This function is calling properly, but it does not including file header-required.php. If i am including this file from specific wordpress file, Its working.
Please help. Thanks..
your specified path is correct. Actully I am calling this file from wordpress and header-required.php file is php file which is outside wordpress directory. example.com contain all static php pages, and inside this directory,I have created one another directory called wrpas, which contains wordpress. This some what confusing because i am using static php pages and wordpress together on one site. Another thing header and footer files are static php pages, which i am using in wordpres.
Thanks!!!
Put that file in theme folder
Rather than using php include use get_template_part for wordpress
Use
<?php get_template_part('header-required'); ?>
For more details
http://codex.wordpress.org/Function_Reference/get_template_part
Don't include through hook because this is php file, you can directly include through include and require function in your header.php file inside the theme.

Categories