wordpress prevent error reporting - php

i'm making a custom theme and noticed one thing. when i try to go directly to my theme index.php file through url (or anypage) for example:
localhost/wordpress/wp-content/themes/my-theme/index.php
this error will appear.
Fatal error: Call to undefined function get_header() in ... on line...
i've tried to download and activate other themes but the result is the same.
how can i prevent this from happening?
i've tried adding the following lines to the wp-config.php
error_reporting(0);
#ini_set('display_errors', 0);
but with no results.
i've also tested it "live" but with no results.
thank's in advance.

Correct me if I'm wrong but that error occurs when someone tries to directly access the theme's index.php.
For example when a bot tries to access it.
You should never access it directly like this:
<?php get_header(); ?>
But rather try it like this:
<?php
if (function_exists('get_header')) {
get_header();
} else {
// Here goes whatever you want to do when the file is accessed directly.
}
?>

Related

Including wp-load.php in custom .php file causes page processing to stop. Why ?

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.

Undefined index page if get page

I cant find specific answer for my problem. So what I'm trying to do is to define index page by using only index.php. This is my code so far:
<?php
if(isset($_GET['page'])){
if(!$_GET['page']){
include "template/home.php";
}
if($_GET['page']=="gallery"){
include "template/gallery.php";
}
}
?>
When I'm trying to open it on local host there is nothing only white screen without any errors. Only thing that makes me problem is this string, but still without any errors, can't find what problem is...
if(!$_GET['page']){
include "template/home.php";
}
I was trying to find on internet solution, but there was nothing helpful, hope u can help me :)
The issue is that you're contradicting yourself in your code. Your example reads like this: "If page is set, proceed. Then, if page is not set, include the home page, if page is gallery, then include the gallery page." Do you see the issue? The isset() check will ensure that the code with the negation operator ! is not run.
The solution is to move that code outside of the primary if block.
try this code
<?php
if(isset($_GET['page'])){
if($_GET['page']=="gallery"){
include "template/gallery.php";
}
}
else{
include "template/home.php";
}
?>

How can I get the current Wordpress e-mail address of a logged-in user in an external php file?

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

Fatal error: Call to undefined function

I am trying to call a function which is defined in functions.php. But I can’t figure out why this error is coming.
Let me tell you in detail what I am trying to accomplish: there are two header files — client and admin. The client header will be accessed when the user enters into a page through index.php, but admin header will be accessed after an admin successfuly logged in.
In the functions.php this is the function I have defined:
function get_my_header(){
if(is_home()){
get_header('client');
}
elseif(is_page('dashboard')){
get_header('own');
}
}
from index.php get_my_header() is called with no error but when I try to access the same function from the admin page I am getting this error:
Fatal error: Call to undefined function get_my_header()
The page where this error is coming has only this one line:
<?php get_my_header(); ?>
The most common causes of this are:
The file you think is being included, isn't being included.
Your calling the function prior to including the file
Based on your description as a header a file - I think the first is most likely. As you also mention that it's in an admin page that it doesn't work on - does the admin page live in a sub directory e.g:
httpdocs/
index.php
admin/
admin_index.php
If it does - the file path to the include file may be wrong, depending on your error_reporting() settings you may or may not be alerted to this.
One way to debug this would be to use require() instead of include() on the functions file - as that creates a fatal error if PHP can't find the script.
http://php.net/manual/en/function.require.php

Fatal error: Call to undefined function redirect_to()

very new to this, so I apologize in advance for rookie mistakes.
I am currently working through one of the PHP/MySQL introductory series from lynda.com. I am loving it thus far, and have been having success, but this one's got me confused.
I have created a form for user input, called new_subject.php. The following is called create_subject.php, and it meant to process the form data from new_subject.php. Please keep in mind that I have not yet added any code to do this, I am just testing a redirect function:
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
?>
<?php
if (isset($connection)) {mysqli_close($connection);}
?>
The point of the redirect_to function is to redirect the user to the form at new_subject.php if they were to type in create_subject.php manually in the browser. Here is what redirect_to function looks like in functions.php:
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
I am getting the following error when trying to get to create_subject.php manually:
Fatal error: Call to undefined function redirect_to()
The tutorial video says that I can either turn on output buffering, which I have tried to do in my php.ini file, but the error remains the same. The video says I can do that, or "fix the white space issue." I have no idea what this means.
I would appreciate any info on what this "white space issue" is, and if there is anything else I can do here. As far as I can see, I should be able to call this function without issues.
Thanks
for the whitespace issue, there must be no output prior to calling location header, in your code.. there must be no whitespace outside the php tags..
just rewrite so that all those are enclose in a single php tag and make sure there is no space outside of that, even a single one.
<?php
require_once("../includes/db_connection.php");
require_once("../includes/functions.php");
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
if (isset($connection)) {mysqli_close($connection);}
?>
read here.. How to fix "Headers already sent" error in PHP
however, your main issue is the function is not found..
check your functions.php and make sure redirect_to is define properly. if that is the case, then most likely issue is in your "require_once" must be pointing to a non existent file. can you tell us your folder structure?
your functions.php must be located in a folder called includes located outside the folder where your create_subject.php is located.
if the 'includes' folder is located inside the folder where create_subject.php is, then change your require_once to
require_once("includes/db_connection.php");
require_once("includes/functions.php");
if both are located in the same folder, then change your require_once to
require_once("db_connection.php");
require_once("functions.php");

Categories