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
Related
require_once($_SERVER['DOCUMENT_ROOT']."/Event_Logger.php");
global $Event_Logger;
$Event_Logger = new EventLogger();
Doing this displays a blank page and does not continue loading the page
The file on my server is located at public_html/Event_Logger.php
So I know I am doing this wrong $_SERVER['DOCUMENT_ROOT'] returns something I wasnt expecting so I am asking how do I get the top level of my site basically the folder public_html
You can't (and shouldn't) include a file using URLs. You should use the path to the file locally, on the same server. If the plugin you're building lives on technologyforthefuture.org, then you can require it from the root path. If you're trying to include the file from an external website, that's not possible.
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');
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)
I am developing a website on a local test environment. From time to time I need to import classes or functions into my php pages. At first I used relative paths to import files, but for some (unexplicable) reason the PHP couldn't find those files. So I have thought it would be necessary use the following:
<?php require_once($_SERVER['DOCUMENT_ROOT'] .'/mywebsite/inc/libs/functions.php'); ?>
The problem is when I have to upload all the pages to my remote webserver, the PHP interpreter won't find functions.php in that position because there is no mywebsite subfolder , on the other hand I can't get rid of mywebsite subfolder, because that would leave me with http://localhost/inc/libs/functions.php which leads nowhere.
So that basically means I will have to manually readjust the path to make everything work. My question is: is there a way for PHP to detect the exact folder of my website so that I don't have to change paths everytime I need to upload a php file to my webserver?
What is the reason behind impossibility of use relative path?
<?php require_once(dirname(__FILE__).'/inc/libs/functions.php'); ?>
Set the base folder with all the files you want as an include path in php.ini.
That way, when you use include () or require (), it will automatically look in the included path.
www.php.net/manual/en/ini.core.php#ini.include-path
Most of my website is in my root directory. And In that directory there is "css", "functions", "images" folder. Everything works fine when I include php files within index.php or any other root file. It includes it fine and executes it fine.
But problem occurres when I made folder "blog". So this is totally new and separate root folder with CMS and its own "root" files. And I try to include css from main root directory or some php files from "functions" folder in main root directory, Everything breaks down. I know I have to include it as ../functions/myfile.com. But this files includes some other files so it just wont work properly and won't be able to include other files properly.
Is there any idea how to fix this problem?
You can get to the root from within each site using $_SERVER['DOCUMENT_ROOT']. For testing ONLY you can echo out the path to make sure it's working, if you do it the right way. You NEVER want to show the local server paths for things like includes and requires.
Site 1
echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/';
Includes under site one would be at:
echo $_SERVER['DOCUMENT_ROOT'].'/includes/'; // should be '/main_web_folder/includes/';
Site 2
echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/blog/';
The actual code to access includes from site1 inside of site2 you would say:
include($_SERVER['DOCUMENT_ROOT'].'/../includes/file_from_site_1.php');
It will only use the relative path of the file executing the query if you try to access it by excluding the document root and the root slash:
//(not as fool-proof or non-platform specific)
include('../includes/file_from_site_1.php');
Included paths have no place in code on the front end (live) of the site anywhere, and should be secured and used in production environments only.
Additionally for URLs on the site itself you can make them relative to the domain. Browsers will automatically fill in the rest because they know which page they are looking at. So instead of:
<a href='http://www.__domain__name__here__.com/contact/'>Contact</a>
You should use:
<a href='/contact/'>Contact</a>
For good SEO you'll want to make sure that the URLs for the blog do not exist in the other domain, otherwise it may be marked as a duplicate site. With that being said you might also want to add a line to your robots.txt file for ONLY site1:
User-agent: *
Disallow: /blog/
Other possibilities:
Look up your IP address and include this snippet of code:
function is_dev(){
//use the external IP from Google.
//If you're hosting locally it's 127.0.01 unless you've changed it.
$ip_address='xxx.xxx.xxx.xxx';
if ($_SERVER['REMOTE_ADDR']==$ip_address){
return true;
} else {
return false;
}
}
if(is_dev()){
echo $_SERVER['DOCUMENT_ROOT'];
}
Remember if your ISP changes your IP, as in you have a DCHP Dynamic IP, you'll need to change the IP in that file to see the results. I would put that file in an include, then require it on pages for debugging.
If you're okay with modern methods like using the browser console log you could do this instead and view it in the browser's debugging interface:
if(is_dev()){
echo "<script>".PHP_EOL;
echo "console.log('".$_SERVER['DOCUMENT_ROOT']."');".PHP_EOL;
echo "</script>".PHP_EOL;
}
If I understand you correctly, You have two folders, one houses your php script that you want to include into a file that is in another folder?
If this is the case, you just have to follow the trail the right way.
Let's assume your folders are set up like this:
root
includes
php_scripts
script.php
blog
content
index.php
If this is the proposed folder structure, and you are trying to include the "Script.php" file into your "index.php" folder, you need to include it this way:
include("../../../includes/php_scripts/script.php");
The way I do it is visual. I put my mouse pointer on the index.php (looking at the file structure), then every time I go UP a folder, I type another "../" Then you have to make sure you go UP the folder structure ABOVE the folders that you want to start going DOWN into. After that, it's just normal folder hierarchy.
i had the same issue and found a code on https://css-tricks.com/php-include-from-root/ that fixed it
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/common/header.php";
include_once($path);
?>
None of the above answers fixed this issue for me.
I did it as following (Laravel with Ubuntu server):
<?php
$footerFile = '/var/www/website/main/resources/views/emails/elements/emailfooter.blade.php';
include($footerFile);
?>
Try to never use relative paths. Use a generic include where you assign the DocumentRoot server variable to a global variable, and construct absolute paths from there. Alternatively, for larger projects, consider implementing a PSR-0 SPL autoloader.