I am facing a strange issue with WordPress. When I put this line as a code snippet in a blog post:
$app = require_once __DIR__.'/../bootstrap/app.php';
I get a 404 error when I try to preview the page, and I wont be able to save the post at all but if I make the line of code incorrect for example, remove the last 'e' from require, this way:
$app = requir_once __DIR__.'/../bootstrap/app.php';
everything works fine. Is the code somehow executing and causing problems?
Your web host probably has a paranoid web application firewall (most likely mod_security) enabled, with rules to block requests which contain data which looks like PHP code.
Contact your web host and request that they turn this feature off.
Wordpress is a little funny that way, try using get_stylesheet_directory() function. This will return the path to your theme directory.
require_once( get_stylesheet_directory() . '/path/to/bootstrap.css );
Also the require_once function doesn't return anything so there is no point trying to assign it to a variable.
Related
I have got a WP website running version 4.8.2.
I have created, in the root folder of my theme, a custom php page that includes Wordpress functions with the following code:
<?php
echo 'ralph';
clearstatcache();
echo file_exists('../../../wp-load.php');
require_once('../../../wp-load.php');
echo 'joe';
?>
The end result should be displaying on the page the string 'ralph1joe' (1 stands for the fact that wp-load.php actually exists).
Problem is: when I launch the custom page only the string 'ralph1' is visualized. Any instruction after the require_once line, is ignored. I cannot see any error both on the page and in the log files. The process of the page is simply interrupted.
Is there anything I can do for having the require_once line to work properly ?
I think the problem is with wp_load.php or maybe you haven't read the wp_load.php file correctly.
Wp_load always requires wp_config.php If it doesn't get it it will die with an error. See the below code in wp_load.php file
$die = __( "There doesn't seem to be a <code>wp-config.php</code> file. I need this before we can get started." ) . '</p>';
With some more identical code like this and error associated with it.
so when you require_once 'wp_load.php' and It doesn't get wp_config.php It will die and stop the execution and you won't get any further print statement
Hope it clarifies you.
I solved the problem. A plugin I had installed caused evidently a conflict.
The plugin is called All 404 Redirect to Homepage
Deactivating it caused my custom page to work properly again.
In my code this error was caused by the required line inside a function. I removed and placed this line at the main code.
I have the following issue. I want to load the Wordpress environment into an external php file. All my work is currently on a localhost and my php code is as follows:
<?php
require("../../wp-blog-header.php");
$admin = do_shortcode("[su_user field='user_email']");
if ($admin === "admin#email.com") {
// Do stuff here
}
else {
echo "<h1>Forbidden.</h1>";
}
This works perfectly fine in my localhost, so I figured that when I upload the file to the server it must work too, given that the file is stored in the same directory as it is locally, hence ../../wp-blog-header.php. Sadly, this does not work. When I echo $admin; the following error appears:
User: user ID is incorrect
I tried using the native Wordpress functions without using the shortcode like this:
<?php global $user_email;
get_currentuserinfo();
echo $user_email;
?>
This does not work either. Is there a more efficient way to load the Wordpress environment into an external php file? Any suggestions on what is happening here? It does not necessarily have to be an e-mail, it could be the user id as well, which I tried and gives me 0.
I forgot to mention, the php file loads and echoes Forbidden together with the error. This makes me believe that the file require("../../wp-blog-header.php") is actually being loaded because when I change the directory I directly get an error that the file is now working/available.
The shortcode works as I have tested it inside a new Page returning the e-mail address of the logged-in user.
UPDATE
My root is:
/var/www/vhosts/test-website.com/httpdocs/
Which I tested with require("/var/www/vhosts/test-website.com/httpdocs/wp-blog-header.php") as well. Still same result. The weird thing is that if I add wp_head() below this line, the header gets loaded. I reformulate my question:
How can I get the current e-mail address of a logged-in user in an external php file?
If your file is same folder root with Wordpress instance. just use code bellow:
<?php
define( 'ROOT_PATH', dirname(__FILE__) );
require(ROOT_PATH . "/wp-blog-header.php");
//your stuff
I have IPB and I have an PHP application which is outside of forum ROOT and on another subdomain as well.
In application I need to have access to member data, eg. posts count, email etc.
I have the latest IPB version and PHP 5.6
I googled a lot for solution, and finally I found this one:
Accessing IPB Classes Externally From Main Website
However, it didn't worked at all. There is no errors, just redirection to main forum URL.
Does anybody have experience wit IPB classes and or it's API?
Can somebody help me to reach the goal.
Solution is probably just one line of code.
EDIT:
I found work around, check out my answer below.
However I'm still interested for "nicer" solution.
After some research, I configured out that IPB redirects because board url doesn't match with current URL/subdomain. (my case)
So... There is workaround, not so nice but works at least, and is one-line solution:
<?PHP
$_SERVER['HTTP_HOST_R'] = $_SERVER['HTTP_HOST']; // Keep original info in another index.
$_SERVER['HTTP_HOST'] = "www.your-ipb-forum.com"; // Work-around
$forumPath = '../forum'; //FORUM FOLDER
define( 'IPS_ENFORCE_ACCESS', TRUE ); // Important so it does not redirect to forums
define( 'IPB_THIS_SCRIPT', 'public' );
require_once( $forumPath.'/initdata.php' );
require_once( IPS_ROOT_PATH . 'sources/base/ipsRegistry.php' );
require_once( IPS_ROOT_PATH . 'sources/base/ipsController.php' );
$ipbRegistry = ipsRegistry::instance();
$ipbRegistry->init();
// Init done
$member = IPSMember::load($memberName, 'all', 'username');
print_r($member); // For demo purposes only
The third line is work-around with which we cheat on IPB.
The second line is "moving" $_SERVER['HTTP_HOST'] data to $_SERVER['HTTP_HOST_R'] so if you need current(real) URL in your application, you may use this variable instead because HTTP_HOST one is changed. (needed for workaround).
I am currently working on a plugin, and I am experiencing a strange problem.
In one of my plugin files, I need to include the wp-admin/admin.php to get the $menu and $submenu variables.
However, if no headers are sent, before requireing the admin.php file, I am redirected to: edit.php?post_type=page.
The code looks like this:
<?php
// Get root dir
$root = explode('wp-content', dirname( __FILE__ ));
$root = $root[0];
// Require
echo 'test'; // if this echo is here (headers sent), it will not redirect
require_once($root . 'wp-admin/admin.php');
Am I doing this wrong? Is there a better way, to include the wordpress root, than what I have done above?
Any feedback greatly appreciated.
By the way; the exact same plugin works fine on two other wordpress installations.
Update!
I have found out that something is redirecting away from the plugin-file using wp_redirect. I have made a backtrace on wp_redirect, which you can access here: http://justpaste.it/dkqw
Any help is greatly appreciated
So I was messing around with the twitter api, and I want to include this file in the footer for every page. The footer is loaded via phps require_once function. The problem is I can't use a full url because of url file acsess being turned off for obvious reasons. So I tried to use
require_once $_SERVER['DOCUMENT_ROOT']('/lib/twitter/base.php');
But that failed. What am I doing wrong or how can I do this better?
Try this:
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/twitter/base.php');