Dynamic linking in PHP - php

I am creating a custom CMS for learning. I have the plan to have the following pages,
Login page
All Posts page
Edit Post page
index page
header.php (the website's header)
footer.php (the website's footer)
sidebar.php (the website's sidebar)
I am confused how would the index page link header, footer and sidebar. Please guide me how can I link these php files in index.php.

You can simply add an Array of files you want to include:
$array = ('header.php', 'footer.php', 'sidebar.php');
Then add some HTML Code structure...
and then you can access the Array and load files.
include_once($array[0]);
.. to include the header.php
include_once($array[1]);
.. to include the footer.php
....
you can use the require_once function to make your site not loading other content if the file does not exists.
if you want to add these files automaticly just add a loop.
foreach($array as $file){
if(file_exists($file)){
require_once($file);
}
else{
die($file.' does not exist!');
}
}

You can use either require_once or include.
I personally use require once option cause it only needs to be include once and not again on later stage.
Inside your body tags:
require_once('/path/to/header.php');
require_once('/path/to/breadcrumb.php');
require_once('/path/to/content.php');
require_once('/path/to/footer.php');
require_once('/path/to/copyright.php');

Related

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.

Woocommerce (shop) header in product-archives.php file

I read that it is possible to place alternative header into woocommerce pages using get_header('shop'); in product-archives.php template in my custom theme. In order to make it work I need to make copy of header.php file and rename it to header-shop.php. It should be also located in same folder as header.php file.
Unfortunately I can't get this to work.
Anything I am missing if somebody notices that would be great help.
You can have a work around.
Just make a copy of header.php in your current theme with the same name (i.e. header.php).
Create another copy of the header.php with you desired name(i.e. header-shop.php).
Now you just need to add a custom code in header.php at the starting lines to check that whether its a product archive page(using this function http://codex.wordpress.org/Function_Reference/is_post_type_archive), if yes then include the header-shop.php else show the below default code of the file itself.
An Example Snippet:
if ( is_post_type_archive('product')) {
// Load header-shop.php
}else{
// continue with default code
}
Even you can load header-shop.php with the PHP function include_once()
if ( is_post_type_archive('product')) {
include_once 'header-shop.php';
}

CI - Public Variable for all pages?

I wish to display a variable that is stored in a session at the top of each page throughout my website. At the minute, on every single page, in the controller index() I have;
$data['credits'] = $this->session->userdata('credits');
I havve created a seperate view for the navigation bar (where the variable will be displayed). I have called it vNav.php. In vNav.php I then do echo $credits.
For every new view, I have to include the vNav.php, but that also means in the other view's controllers, I have to set the $data['credits'] variable in the index() function.
Is there a way in CI to do this automatically for me? So I don't have to have the same line of code in all my controllers?
Thanks
Okay here's a better design for your views.
First, create a folder called include/, in this folder create a header.php, footer.php, template.php.
header.php
<html>
<head>JS - CSS - META</head>
<body>
<div>COMMON MESSAGE</div>
footer.php
<footer>FOOTER GOES HERE</footer>
</body>
</html>
template.php
$this->load->view('include/header.php');
$this->load->view($main_page);
$this->load->view('include/footer.php');
any controller:
$data['main_page'] = "hello_world"; // view/hello_world.php
$this->load->view('include/template',$data);
So in that way, you can create a common section that will be displayed on all pages by adding it to the header or the footer. If you want, you can also create another file in the include folder and then include it in the template so that it will be loaded automatically every where by only modified one file

WordPress - Set 2 headers in one website

i have to header like.
header-first.php
header-second.php
i create two template like first and second. when the first template is load then i want to load header-first.php and this is working. then second like first.
i have problem with when i load second template then the header-second.php is load is working but not when i go to any page then i want to load header-second.php but not it load header.php.
when i load a second template then the header-second.php is loaded working. now i going any page and view post then the header.php file is load i want to load header-second.php file. please help how can i set globally.
i try to set globally in function.php like.
global $header;
if(is_page_template('page-second.php')){
header = 'second';
}else if(is_page_template('page-first.php')){
header = 'first';
}
it working it return only template page is load.
when i try to load any other like about page it return '' value. it not return any value.
i want to second whenever the first template is not visit.
thank you.
Each template file such as page.php, single.php, archive.php, etc, will have get_header() at the top of it.
This function loads the header (header.php).
If you pass in a string as the first argument it will try to load header-{argument}.php and if not found it will load header.php.
Anywhere you want to load header-second.php you need to change get_header(); to get_header( 'second' );
There is an action that fires when this function runs but there's no filter so you can't override it globally like you're attempting to do. Instead you need to update the template files individually with the function I showed you above.
To summarise change:
<?php get_header(); ?>
To:
<?php get_header( 'second' ); ?>
Further reading: http://codex.wordpress.org/Function_Reference/get_header

Include wordpress theme in a custom php page

I need to include a custom PHP page in Wordpress.
So what I need to do is just to show this custom php page using the Wordpress theme installed on that Wordpress.
Does not mind which theme is up, the custom php page will have to be shown under any theme is installed in that moment.
How do I do it in Wordpress?
I am new to Wordpress development.
Thanks
Creating a custom php page that will be able to be viewed in any theme (and have the theme applied) would be considerably difficult.
Each wordpress page calls specific theme functions of that particular theme, as well as referencing files of that theme to generate header, footer, css files, javascript files, etc.. Your custom page would need to plan for all of these contingencies, for each possible theme used.
Here's a alternative solution: inject PHP code directly into a standard wordpress page via this plugin http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Meaning: you make a normal wordpress page, but are able to add php to it. When this page is rendered, the proper page template is used, and all the theme references are taken care of for you.
You could do this easily with a page template. WordPress allows you to create page templates which can be assigned to a page via the 'Page Attributes' panel within the page editor. These templates are php files inside your theme directory which begin with some code like (see this page in The Codex for more info):
<?php
/*
Template name: Custom PHP Page
*/
?>
<?php // begin custom PHP page ?>
Typically a template is a variation on the regular theme files (such as page.php) and would call the get_header() and get_footer() functions and have an instance of the loop. However if you simply want to use a custom PHP page, then all you need to do is create the file you want inside the current theme directory and add the above code at the very top of the file.
To output the custom PHP page on your site, you would need to add a new page via the admin area and then assign your new page template to this page.
Alternatively, if you want to include a custom PHP page inside an existing theme file, you use the code:
<?php include(TEMPLATEPATH . '/includes/file.php'); ?>
in this case your custom PHP file would be located inside a directory called 'includes' within your current theme directory.
Tim.
It's not that difficult. Here's what you need:
Once you include the main wordpress blog header, the entire armamentarium of wordpress functions is available to you, which allows you to get the active theme's directory. Once you get that, just include the header and the footer of the theme.
// If title is not displayed before loading the header, Wordpress displays "Page not found" as the title
echo "<head>
<title>Your page title</title>
</head>";
// Include the Main Wordpress blog header
include $_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php";
//Now, you need to get the active theme's folder, and get a relative path to that folder
$homeurl=home_url();
$ddir= get_bloginfo( 'template_directory');
$current_theme_relative_path=substr_replace($ddir, "", 0, strlen($homeurl));
//echo "<br/>The relative path to the currently active theme is ".$current_theme_relative_path;
//Once you have the path, include the header and footer, adding your custom php code in between.
// Include the specific theme header you need
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/header.php";
// Your custom PHP code STARTS here
// Add anything you want to display to the user
echo "
<h2>
Your form has been submitted
</h2>";
// END of custom code
?>
<?php
}
// Now end with the theme's footer
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/footer.php";
?>
Was very helpfull (even if dated of 2011-13)
Also, as a thank you, i'm sharing the version i made
it's usefull if your wordpress folder is not located at ROOT
PasBin Link - wordpress custom php page
just change the value of $wplocalpath in :
// Wordpress path (if wordpress is not located at ROOT
// $wplocalpath="/Wordpress1";

Categories