use get_option in external php file - php

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');

Related

Is it possible to implement init.php in my wordpress plugin.php file?

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.

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 do I call external PHP files inside of a Wordpress theme?

I'm working with an existing PHP web application. It's site structure is similar to:
public_html
include
header.php
style.css
footer.php
blog
I've installed Wordpress in /blog. I am trying to create a Wordpress theme using the dynamic elements of the external PHP app.
Here is an example of /blog/wp-content/themes/custom-theme/index.php:
<?php
include_once("../../../../include/header.php");
?>
The theme is not reproducing the header code. I've tried variations of the relative path, just in case, with no success. Are there other considerations I haven't taken into account?
If WordPress is in /blog, there's a convenient constant called ABSPATH that holds the path to that folder. So:
$inc_dir = dirname(ABSPATH) . '/include'; # /path/to/public_html/include
include_once "$inc_dir/header.php";
Or directly:
include_once dirname(ABSPATH) . '/include/header.php';
Seeing that you're already using the correct relative path, though, be sure to include that file where relevant. If you're including it in an html comment or something like that, you'll get unexpected results.
I believe the "file_get_contents" could work in this scenario too.
It could also be handy if the external php file is part of a different program.
"file_get_contents" is a php method which loads entire files into a string.
See us3.php.net/file_get_contents
A handy call in various situations.
I used this to embed an ASP menu inside Wordpress. See stackoverflow thread with an example here:
How to Include an .asp menu file inside a php file? (Wordpress Blog folder within ASP Site)

wordpress include local root php file

I have a child theme functions.php that I am trying to have include a few local domain files , through the use of short codes, that live outside of the Wordpress file structure.
I cant seem to find whats going wrong. Anyone have any pointers?
child theme functions.php
<?php
// localhost path
define('WEB_ROOT', 'http://www.example.com/');
// includes
function filePost() {
include WEB_ROOT.'/include/file.php';
}
add_shortcode('fileShortCode', 'filePost');
?>
With default PHP settings you can't include files from external sources, which http://example.com is.
To be able to do that, you'd need to change allow-url-include setting. But perhaps the only thing you have to do, is not referring to a URL, but rather to a local path? This you can retrieve by using Wordpress function plugin_dir_path

wordpress: default location of header.php and footer.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/

Categories