Dynamic footer integration - php

I'ld like to insert the footer of my website dynamically so I can add it to multiply pages without copy and paste it every time.
I know an iframe can do it but I heard that's not a good solution and not working for seo.
Can it be done with PHP or JavaScript?

footer.php code as follows
<?php
//Write your custom footer here
?>
home.php code as follows
<?php
//All contents of Home page
include('footer.php');
?>

<?php include('path/to/footer.php'); ?>

PHP is the right tool for this job:
<?php include('footer.php'); ?>

from php use include function
e.g: <?php include('inc/footer.php');?>

Related

get wordpress only footer on external php page

I am working on a application where i only need to get wordpress footer
on outside php page i am using below code
<?php require('../wp-blog-header.php'); ?>
<?php wp_footer(); ?>
but it is loading NULL,when i useing below code
<?php require('../wp-blog-header.php'); ?>
<?php get_header(); ?>
<?php wp_footer(); ?>
it loads both header and footer but i want to load only footer please let me know what should i do
Thanks
Refer from this question.
You can use get_footer() function.
<?php
require __DIR__ . '/wp-blog-header.php';
// create false $wp_styles to prevent errors.
$wp_styles = new \stdClass();
$wp_styles->queue = [];
get_footer();
In case that your file is the same location with wp-blog-header.php then use the same path as shown above.
Warning! The result maybe invalid HTML as the elements are closed without open. Example </div> without matched <div>.
Alternative solutions.
Use cURL to fetch your home page and then use PHP Dom to grab only specific HTML part. Ideas: 1, 2
Use AJAX to fetch your home page and then use JS Dom to grab only specific HTML part. (See reference).
These 2 choices, I can't show the code because each WordPress theme use different HTML elements and styles.

how to modularize html page elements using php

I want to modularize html page elements using PHP. This is the code I am currently using:
<?php include("template.php");
echo "Hello";?>
The template.php actually also includes the body of original html page, inside of which I wish to write "Hello". So will I have to create 3 different PHP templates: 1 for header, 1 for navigation, and 1 for footer and then include them at appropriate locations in a new PHP file or is there any other way out?
You could include the 3 files on each page?
<?php include("header.php"); ?>
<?php include("navigation.php"); ?>
Hello
<?php include("footer.php"); ?>
You could also include the navigation.php inside the header.php
How about using a template engine?
If you want to have same complex layout for multiple pages with some variations on the datasources a template engine would be of real help.
Some good template engines for php are:
Smarty http://smarty.incutio.com/?page=SmartyFrequentlyAskedQuestions
Mustache PHP https://github.com/bobthecow/mustache.php
PHP TAL http://phptal.org/
Of course if you just want some simple header and footer - use include_once as suggested by the other answers.
I suggest you use the include_once() function to include the scripts you want to insert and place the code into html <div></div> tags so you can display wherever you need them to display in the web page.
<div>
<?php include_once("header.php")?>
</div>
<div>
<?php include_once("body.php")?>
</div>
<div>
<?php include_once("footer.php")?>
</div>

php include breaking

I have a php page that generates all the html and echo's it. Now I want to write a php script that I can use include to handle the footer code. that way if I need to update the footer on all the pages I can just edit the code in the included page.
But when I use include("footer.php"); and the footer page contains the footer code that works if it exists on the page it breaks and prints the code. Im very confused as too why?
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0)
Starts writing the code on the page if I include from 0)
Please help?
EDIT: The code in the footer is wrapped in <?php ?>
You need to wrap that code in footer.php with php tags.
<?php
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0) ...
?>
Dont your short hand notation of php tags <? ?>, use <?php ?>instead...
It sound like you didn't start php file with
<?php
Please make sure that your footer file has <?php in the beginning.
I'm not sure if you stated your question very clearly.
If you are echoing code out it will appear as soon as you include the file. Your file should look like this:
<?php require_once 'header.php';
// content goes here
require_once 'footer.php'; ?>

php website one page for design

I'm done creating a php website that has 6 pages, and the structure of the pages is the same for each one of them, the only thing that changes is the content, so is the same header, same design and same footer, the only thing that changes like I said before is the content itself.
so i was thinking instead of having many pages, I could have only one design page, and change only the content, what do you recommend?,and how do I do that?, also im not planning installing anything like Typo3, wordpress, joomla or whatever in my server, so I want something i could do using php idk. thank you!
Simplest solution is to create separate files.
header.php
footer.php
menu.php
In header.php put your code from header
<?php ?>
<HTML>
<HEAD>
</HEAD>
<BODY>
...
<? ?>
Same goes for footer and menu files.
Then you can use it by including them.
Your index.php could look like following.
<?php
include("header.php");
include("menu.php");
?>
<h1> This is my content </h1>
<?php
include("footer.php");
?>
This is the easiest option I think for someone who doesn't want to spend using templates, CMS etc. Also you can create function called header that takes $title and changes title of your window. Up to you.
Sounds like you want AJAX. Use prototype. You can make one page, and then use prototype to swap out the content (which could include a PHP page) based on user clicks.
Simple and easy solution:
create footer.php and header.php
and in the header.php you can have something like this:
<?php function top_header($title) { ?>
<html>
<head>
<title> <?php echo $title ?> </title>
</head>
<body>
<?php } ?>
footer.php
<?php function footer() { ?>
</body>
</html>
<?php } ?>
Your index.php could look this:
<?php
include("header.php");
include("footer.php");
top_header("Title of the page");
?>
Hello World!
<?php footer(); ?>

Dynamic header with PHP?

I know this is a basic PHP question, and I'm trying to learn the stuff. I very familiar with HTML, CSS and familiar with the CONCEPT of PHP, but not with specifics.
I have always partnered with a back end developer to accomplish this stuff and to set up wordpress sites, etc.
I'm building a very basic four or five page website (a showcase for the client's custom fishing rods: http://www.tuscaroratackle.com/index2.php). I want to call the page header (as in logo, navigation, etc., not as in the head element) dynamically from a php file, and same thing with the footer, so I don't have to rewrite all the markup on every page for these bits.
I don't intend to use a database for this site, I was just thinking I could call those two bits from another file, as you would in a wordpress setup with the header.php in the wp-content directory.
Is there an easy explanation on how to do this? (I get the basics, just looking for help on the more specific PHP calls that need to be made)
Or, if this is not an answer somebody could easy give, can you point me to a good resource to research it further?
Thx
You betcha - include and require -
using include
in your page:
<body>
<?php include 'header.php'; ?>
in your header.php
<div id="header">
<!-- content -->
<?php echo "run php stuff too"; ?>
</div>
would result in:
<body>
<div id="header">
<!-- content -->
run php stuff too
</div>
You should put the header html code in some file such as header.php and then include it with php like:
include ('header.php');
You should specify the correct path there, for example, if you put the header.php file in includes folder, you can include it like:
include ('inclues/header.php');
More Info:
http://php.net/include
Put in a separate file and use include, require or require_once.
Eg
require_once("path/to/myfile.php");
Look into PHP includes.
The way I normally do it is to create a file called includes.php with two functions header() and footer(), then call them on each page like such:
includes.php:
<?php
function header(){
echo '<div id="header">Welcome</div>';
}
function footer(){
echo '<div id="footer">Goodbye</div>';
}
?>
index.php:
<?php
include_once('includes.php');
header();
echo '<div id="content">Main page body</div>';
footer();
?>

Categories