WordPress include file at header not working - php

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".

Related

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.

Get page.php using php code

I am working with wordpress.
I see that in my index.php there is a code called <?php get_footer(); ?> ..and I get it, it's simple. This code will pull footer.php.
It seems to me that the get_footer() is built in to wordpress that pulls anything that is named footer.php.
I have created my own page called page.php.
I want to be able to 'get' this page and show in my php code enabled 'sidebar widget'.
I have tried to paste this code, and I am more that certain that its wrong:
<?php
echo file_get_contents("side.php");
?>
What code would I need if I want my custom page called page.php to be shown?
The WordPress way to load a template file is get_template_part():
get_template_part( 'page' );
// loads page.php from the theme directory.
Note that page.php is a special filename in WordPress themes, this file is loaded as a template if a page is displayed. You should give your file a different name if you want to prevent this.
Details are in the already mentioned template-hierarchy.png
Edit:
After rereading your question: If your page.php is not from a template, but in a folder of a plugin you are developing as a sidebar widget, the also already mentioned include('page.php'); would be the way to go.
page.php is a special page of wordpress. See this diagram.
https://developer.wordpress.org/files/2014/10/template-hierarchy.png.
The wordpress way is to create a own template.
https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
try the following
include ('side.php');
OR
require ('side.php');
you may also use include_once / require_once
Please use require_once or include in php file like below.
require_once('side.php');
or
include ('side.php');
try this,
require( dirname( __FILE__ ) . '/page.php' );
if your page is in same level where is your parent page.
Add this code in your php page
<?php include_once('filename.php'); ?>
There are so many options that you can chose:
You can use get_template_part():
From the Docs: WordPress now offers a function, get_template_part(),
that is part of the native API and is used specifically for reusing
sections - or templates - of code (except for the header, footer, and
sidebar) through your theme.
Other solution is require_once() or include_once().
But if i compare both function include_once() is faster than to require_once() for smaller application.
For better understanding about the require and include you can read this Question.

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.

Saving the Wordpress header to a php file accessible anywhere?

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.

Categories