get wordpress only footer on external php page - php

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.

Related

loading entire page sections based on scroll position

I am developing a one page website and I would like to load in each section when the window scrolls to that specific section. Now I know you can lazyload images but I want to lazy load the entire section. The only way I think it would be possible is if I put my html code into jQuery then load it in when the scroll position is reached. But I would prefer not to do this.
This is a wordpress website and I am loading each page through into the homepage using
<?php require_once(''); ?>
so my page is layed out something like this
<?php get_header(); ?>
<?php require_once('section_one.php'); ?>
<?php require_once('section_two.php'); ?>
<?php get_footer(); ?>
So could I use php to only load these sections in when the scroll position is reached or is there a better way with jQuery? Also I want web crawlers etc to still be able to see my whole page. So if jQuery is disabled I want the full page to show. Any guidance or tutorials on this would be very helpful thanks
Create a method in controller that will render a sub-view based on the section number and return you the HTML. Or in your case create a file that will accept a GET request with section number, and render the output of needed section file as its done in most PHP frameworks (see below). That way you can make AJAX request when scrolling position is of necessary value, and insert returned HTML into the page.
<?php
$section_number = $_GET['section'];
ob_start();
if(file_exists(__DIR__ . 'section_' . $section_number . '.php')) {
include(__DIR__ . 'section_' . $section_number . '.php');
$var=ob_get_contents();
ob_end_clean();
echo $var;
}
echo '';
Render PHP file into string variable
May I suggest that you load the content and hide it with CSS instead? And then make a scroll spy solution to display the content when the section enters viewport? Why force someone to wait while the contents loads?

How to change many webpages in a website?

I am making website and this website will have more than 20pages.
I am using my template to add a webpage. This template has header and footer, so I just add the body of new page.
But, what if I want to change navigation bar in the header, then I have to change all 20 pages that I already made to correct.
I want to know better way.
I read a book, and it says about "php include" function.
Should I use this function in the header and footer of each webpage to call header and footer file?
If I want to change the navigation bar in the header, all I can do is changing only one header file, then rest of website will be changed.
Is this correct way?
In this case, what do you do?
I am a beginner, so please advise me.
Thank you in advance.
You should build your website as following. The header.html would contain the navigation.
header.html
<!doctype html>
<html>
<head>
...
</head>
<body>
<nav>
...
</nav>
footer.html
<footer>
...
</footer>
</body>
</html>
page.php
<?php
require_once 'header.html';
?>
Your content goes here
<?php
require_once 'footer.html';
?>
You should use require_once so the header and footer will be imported only once per script and if the header or footer cant be found, the script will throw an exception and stop the "application".
"require_once" and "require" are language constructs and not functions. Therefore they should be written without "()" brackets!
Yes this is the correct way or you could just copy your header and footer code to every .html file (if you don't like PHP)
Yes, this is the correct way. Try to think of the DRY principle - don't repeat yourself. Elements of your web page that are common across multiple pages can be coded once, then called in. If you need to update these elements, you update them once and it affects all pages.
You then "include" these elements into your page, and the elements are self contained files. As a basic example you would have header.php
<html>
<head><!-- all of your head meta tags in here--></head>
<body>
<div id="header"><!--your header elements and top menu in here --></div>
and footer.php:
<div id="footer"><!-- your footer elements in here--></div>
</body>
</html>
Don't forget in both header and footer files you can then put dynamic code if you wish. Then for each of your pages you would simply call these files in using include, include_once, require or require_once
<?php
include('header.php');
//this is where your actual page content goes
include('footer.php');
?>
A very basic example, but hopefully that makes sense to you.
Correct.
If you include another php file it will calculate/ask for input/do output (whatever you do in the file) as part of your main file.
If you include a php file that has a function you can later call this function without it showing in your main file. (Saves space in your main file)
So your 20 pages may only need a few lines of body text and the rest is header and footer. This will make changes very easy

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>

hide div in a certain html page

apology for this newbie question.
I have created an html page (dash.html) that uses the same header as the other pages.
it calls this PHP function <?php include 'header.php'; ?>
the dash.html contains a special <div> made specially for that page; and it must be placed inside the header.php
im trying to figure out how to enable/disable a certain div on a certain html page.
will it require a PHP conditional statement?
Yes. The easiest way is to include a conditional line in the header, and pass the checked variable from each page that calls header. So, in your dash.php (it can't be a .html if it calls php, can it?):
<?php
$includediv = true; // set to false, or leave out, if you don't want the div
include('header.php');
?>
and in the header.php:
<?php
...some other code...
if($includediv){
...code to include div...
}
?>
This will continue to work as before for all other pages that call header.php.
You are trying to show the div only if header.php is present, right?
So, just set a variable inside header.php and use a conditional inside the HTML page.
Try this code
<?php
$path=explode('/',$_SERVER['REQUEST_URI']);
$page=end($path);
if($page=='dash.php')
{
?>
<div>your div for dash.php page</div>
<?php } ?>

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