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
Related
So I'm developing a Wordpress plugin, and I'd like to include the code from the init.php file to my plugin.php file.
My plugin does contain a working hook:
require_once plugin_dir_path( FILE ) . '/src/init.php';
To be exact this is a wordpress gutenberg block plugin. I have used this project to start:
https://github.com/ahmadawais/create-guten-block
So as I have asked above, is there a way to implement the code inside my plugin.php file? And if so, how?
I'd like to achieve this, so my src folder would only contain files that have either .js or .css extension and to store anything plugin related php code in one file.
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?
I have an WordPress page that include the javascript file generated by php script (<script src="myScript.js.php" />).
Inside of that php script I would like to use WordPress functions like get_category_by_slug(), get_posts(), get_option() and so on.
But my php file does not include the WordPress "framework".
Which file(s) should I include in my php script to make use of functions WordPress provides to e.g. archive.php in /theme folder?
I've already tried to include each file from wp-include but it wasn't an good idea.
To bootsrap the application you only need to do two things:
Include the loader file require_once( 'wp-load.php' )
Start the application wp()
Now you have access to all the framework functions!
i create a simple wordpress plugin and wanna use get_option() in my external php file
i know that i can use this to codes to include wp-load.php and use get_option()
require_once('../../../wp-load.php');
but i wanna include wp-load.php in correct way
also i read http://ottopress.com/2010/dont-include-wp-load-please/ and coulnt find a solution
"Options" is a WordPress database table. You can use get_option() from your plugin, assuming it's installed, without manually loading wp-load.php.
<?php echo get_option( 'option_name' ); ?>
wordpres has the following structure with 3 directories and few files:
- /wp-admin
- /wp-content
- /wp-includes
- /files...
All your plugin and themes are located in wp-content directory. For all your plugin or themes files you don't need to include any extra files to use there default functions or hooks.
But in case you have extenal directory or files that is not part of the wordpress structure and you want to access the wordpress functionality then in this case you have to include the wp-load.php file. Take a look on this structure:
// Directory example
- /external_dir (Not part of WordPress Structure)
or
// File example
- /my_custom_file.php (Not part of WordPress Structure)
- /wp-admin
- /wp-content
- /wp-includes
- /files...
Below file does not need to include when you are coding in your theme folder or developing any plugin because all wordpress function and libraries are called by default.
You need to include when you are coding outside the wordpress structure i.e "wp-content" folder.
If the page you are trying to execute is reside in the same directory, use
require('./wp-blog-header.php');
If the page is not in the same directory, use
require('/the/path/to/this/wp-blog-header.php');
Here's a really simple question. I have wordpress 3.4.2 installed. I'm trying to build a theme from scratch. So I've added a style.css file and an index.php file. Both only containing the bare minimum. Now I'm wondering when I call the get_header() and get_footer() functions from index.php, where are the header.php and footer.php files located that wordpress is using to generate the header and footer of my site?
In the root folder of your wordpress theme e.g. wp-content/themes/mytheme so you will have to create these also ...
Found the answer here: http://codex.wordpress.org/Function_Reference/get_header. It seems the default header.php and some other template files are in wp-includes/theme-compat/