How to include a Muse file in a Wordpress theme - php

I've designed header in Adobe Muse and I want to include it on my wordpress theme. I tried to use a php include statement like so within the header.php file:
<?php
chdir('pages');
set_include_path('pages');
include 'index.html';
?>
The code calls the correct file but none of the images or css is loading. When I look at the debugging console it shows that the code is not calling the correct path for these images and css. It's looking in the root directory instead of the pages directory. How can I get the index.html file to run with the correct working directory?

Related

How to include assest using PHP

Let say, i have a file product/vps.php and i want to include it in root folder index.php. i can simply use include "product/vps.php"; in index.php to include content from product/vps.php.
But the problem is when i load index.php the page is broken because product/vps.php have lot of CSS, Image and Javascript file from product/assest/xx.
How can i write a include in PHP which call content and and also include the sub-content of content file.
Edited :
It possible with defining the root folder with help of dirname(__FILE__) or __DIR__ or DIR or include_once(ROOT .'product/vps.php');

Accessing navigation file in parent folder only displays without css styles

I am trying to access my navigation.php file so I can include it in my login.php file. Like so:
<?php
include "../navigation.php";
?>
but the navigation only displays without css styles. For some reason this ONLY happens with I add a folder within the root folder of the project I am working on with php files. How do I access the navigation php file from a child folder?
Here is a screenshot of my file folder:

Issues with defining a path for PHP include across multiple folders

cant figure out how to do what im trying.
I want a way to include PHP files so that i can call the same file from within any file in any level folder and it always points to the same files.
So i have a website setup e.g.
root
init.php
databaseconn.php
index.php
/css/
main.css
/settings/
user_profile.php
/template/
sidebar.php
/includes/
get_user.php
For example, on user_profile.php i am including the sidebar.php file.
For now i would use:
../template/sidebar.php
And then ../includes/get_user.php inside that sidebar file.
Which works fine. The problem however is that i also include the same sidebar.php file on my index.php page. This then causes the issues.
To counter this, i have tried this:
<?php
$isDevelopment = true;
$baseInclude = ($isDevelopment ? '//localhost/website/' : '//www.site.com/');
?>
And then when including any files across the site i have used:
include $baseInclude. 'template/sidebar.php';
When doing this however i am getting an error.
The error shows:
Warning: include(//localhost/website/template/sidebar.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/website/settings/user_profile.php on line 46
I dont understand why its looking for the file inside the user_profile.php file when it shows and i have defined that its coing from the localhost path...
Any ideas?
in your init.php file add this at the top
define('ROOT', __DIR__);
now in your other files use
include (ROOT . '/full/path/to/file.php');
in your code it would look like this
include (ROOT . '/template/sidebar.php');
One possible solution:
$baseInclude = getcwd();

Proper way to include PHP files in Wordpress theme

I am trying to develop a WordPress theme which requires a lot of PHP functions and custom classes.
I am wondering what the best practice is for including additional PHP files within WordPress. Example: Do I list every file to include using:
include();
require_once()
etc
When I review other developers themes I never stumble across a batch of "include() statements, so I believe I must be missing some standard method of performing these includes.
You can follow any method. But I suggest you to use require to include any file.
_s ( aka underscores ) which is theme starter kit and default themes of WordPress such as TwentyFourteen, TwentyThirteen, etc are using require to include files.
See sample code used in TweentyFourteen theme.
require get_template_directory() . '/inc/template-tags.php';
If you are using child theme and need to load php file from child theme folder, you should use get_stylesheet_directory() function. get_stylesheet_directory()
Include will include the file content
Require will throw an error if file not found.
Include_once .. check if included if no then include
Require_once same as include_once but with error if file not found
So if you are the one who wrote the file you should include .. not include_once.
We usually, use include for unnecessary files. Require for important files. For example footer.php will not make script stop .. but core.php is important.
Why not put all the included files into a single file (library.php) and include that in your theme? By doing this if you need to change something you can change just one file. Also you can use conditional tags like if..else within the library.php to include specific files for certain pages.
I would also suggest that you use require_once() for important files so you get an error if the file is missing. If there is no error when a file is missing then functions that use that file will through multiple errors.
Also as #Othman has suggested you can use include for less important files.
Another method would be assigning a
file and include it into your functions.php to avoid redundancy
For Example my functions.php in my root folder
where i get my files from the /inc dir and the functions dir/
include('inc/assets.php'); //Where i register all css and js
include('functions/index.php'); // Where are all functions are registered
There is also another method on the wordpress codex
https://codex.wordpress.org/Theme_Development#Including_Template_Files
<?php get_template_part('template-parts/blog', 'content'); ?>
Also checkout the wordpress organizing theme files docs
https://developer.wordpress.org/themes/basics/organizing-theme-files/
assets (dir)
- css (dir)
- images (dir)
- js (dir)
inc (dir)
template-parts (dir)
- footer (dir)
- header (dir)
- navigation (dir)
- page (dir)
- post (dir)
404.php
archive.php
comments.php
footer.php
front-page.php
functions.php
header.php
index.php
page.php
README.txt
rtl.css
screenshot.png
search.php
searchform.php
sidebar.php
single.php
style.css

How to include php file to wordpress theme(index.php)

I would like to include cars.php file to index.php theme; the cars.php file is script, which loads data from the server and works as booking system, when I am trying to connect as usual way include('/.cars.php');, and check the source code in browser, its there, but no forms appear.
Could be the problem with wordpress engine itself?
I think you have a typo include('/.cars.php'); is likely include('./cars.php');
What you have now would look in the root directory for a file called .cars.php

Categories