I have a standalone script in my WordPress installation and I have used the following to load the wpdb object:
define( 'SHORTINIT', true );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
However, my error log outputs the following when I call the script:
PHP Fatal error: Call to undefined function __() in /wp-includes/wp-db.php on line 1297
When I go to line 1297 in wp-db.php, I see
_doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' );
If I comment out that line, the script doesn't crash, but being a core WordPress file, I don't think that's the best solution.
I do use the prepare method in a separate class, however it is only at the standalone script file that I get this error. When I re-use that same separate class by creating an object in a file that WordPress "recognizes", I don't get the error.
According to WP docs, __ is located in wp-includes/l10n.php - so you could try and include that as well (and hope that that then does not itself refer to stuff that in turn needs other files ;-)
Apparently it does work, so I'll just add it as an answer.
Related
I am trying to follow a WordPress tutorial by Imran Sayed - Codeytek Academy (https://www.youtube.com/watch?v=lNtw4yxEydM&list=PLD8nQCAhR3tT3ehpyOpoYeUj3KHDEVK9h) to allow wordpress to build a menu and then inject into the html. I have followed the turorials 22, 23 and 24 (from the playlist) trying to use this in my own project.
I have copied the code and folder/file structure and added in the class, helpers, singletons and autoloaders. BUT everytime I try and run the code i get
Fatal error: Uncaught Error: Class 'INFOCUS_THEME\Inc\Menus' not found in /home/will/Local Sites/karenkeyinfocus/app/public/wp-content/themes/infocus/template-parts/header/nav.php on line 13
Error: Class 'INFOCUS_THEME\Inc\Menus' not found in /home/will/Local Sites/karenkeyinfocus/app/public/wp-content/themes/infocus/template-parts/header/nav.php on line 13
I have changed the text domain from aquila to infocus as thats whats in my project in all the locations. BUT i am completly stuck and can't work out why my code is not running.
Think this is the code thats causing the problems as it can't find the class 'INFOCUS_THEME\Inc\Menus'
<?php
$menu_class = \INFOCUS_THEME\Inc\Menus::get_instance();
$header_menu_id = $menu_class->get_menu_id( 'infocus-header-menu' );
$header_menus = wp_get_nav_menu_items( $header_menu_id );
?>
I have uploaded it to my github account and post the link here, as I thought that is the best way.
https://github.com/wkey1980/infocus
When declaring a variable as a class you must use 'new' to initiate the class.
$menu_class = new \INFOCUS_THEME\Inc\Menus();
//and then you can do
$menu_instance = $menu_class->get_instance();
I'm a beginner to WordPress and PHP, and I'm trying to add a custom settings options page to my WordPress theme by defining a class that is used to generate the page. When I attempt to create an object in the functions.php file to generate the page, I get an error message stating that the class cannot be found.
I've spent a while searching for solutions and messing with the code, but I couldn't find anything that works. The file definitely exists (I can find it in the specified location in file explorer and open/edit it in my IDE). If I just paste the code from my class file directly into functions.php with the class declaration and constructor removed, everything works as expected.
I'm running XAMPP on Windows.
Error message:
Fatal error: Uncaught Error: Class 'My_Class' not found in C:\xampp\my-path-to-site\my-theme\functions.php
in \my-site\functions.php:
include('/folder/class.my-class.php');
$my_options = new My_Class;
$my_options->__construct();
in \my-site\folder\class.my-class.php:
class My_Class
{
private $options;
function __construct() {
add_action( 'admin_menu', array($this, 'option_add_admin_menu'));
add_action( 'admin_init', array($this, 'option_settings_init'));
}
function option_add_admin_menu( ) {
add_options_page('My Options', 'Options', 'manage_options',
'options', array($this, 'option_options_page');
}
// rest of code that registers settings & fields
}
EDIT: I changed "include():" to "require()" as suggested, but now I am getting two different error messages:
Warning: require(/setup/class.my-class.php): failed to open stream: No such file or directory in C:\xampp\htdocs\my-site\wordpress\wp-content\themes\my-theme\functions.php on line 29
Fatal error: require(): Failed opening required '/setup/class.my-class.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\my-site\wordpress\wp-content\themes\my-theme\functions.php on line 29
Effectively, you don't have the right path and include will allow you to continue if the file doesn't exist.
When including or requiring a file, if the path you supply starts with a / or \ then PHP will treat it as a path from the root of the current filesystem. When you supply a path that doesn't start with one of those, PHP thinks it is a relative path it will try to guess which file to include based on where the current file is and other directories it knows about.
To fix you will likely want to do the following:
require_once __DIR__.'/folder/class.my-class.php';
See the docs on include, include_once, and as well as __DIR__.
Recommendation:
Whenever including a file you should try to use require_once whenever possible. If it is a file that you know can be included multiple times then you may use require. If it is a file that is OK to be omitted if it for whatever reason doesn't exist, then you may use include_once. If the file can be both, only then should you use include.
However, as an experienced programmer I can also tell you that if you are using either include_once or include you are doing something wrong and should be checking if a files exists before trying to blindly include it.
Also, I highly recommend having the below code active at all times. This will help you catch breaking errors before they have a chance to actually break. Or at least grant you a better understanding of why something broke.
ini_set('display_errors', '1');
error_reporting(-1);
Please check my comments inside the code
in \my-site\folder\class.my-class.php:
<?php
class My_Class
{
private $options; //if you want receive a option
function __construct($options) { //You need receive this option here
$this->options = $options; //and atribut it here
//add_action( 'admin_menu', array($this, 'option_add_admin_menu'));
//add_action( 'admin_init', array($this, 'option_settings_init'));
}
function option_add_admin_menu() {
//add_options_page('My Options', 'Options', 'manage_options',
//'options', array($this, 'option_options_page');
}
// rest of code that registers settings & fields
}
in \my-site\functions.php:
<?php
include_once('folder/class.my-class.php'); //removed the root bar
//You are waiting for a option in the class, so pass this option
$my_options = new My_Class('some option');
//$my_options->__construct(); //You don't need this here, the constructor is used inside the class.
I want sign up with google plus in codeigniter. I have include files which are required for the php script but I'm getting error
(( ! ) Fatal error: Cannot redeclare class Google_OAuth2 in
D:\wamp\www\Surecash_back\application\libraries\google-plus-api-client-master\src\auth\Google_OAuth2.php
on line 453)
The error message tells you a very simple thing:
You are trying to load\include the same class file more then once
(which results in re-declaring a class name of an already declared class).
The fastest way to check this, will be:
If you are using an autoloader - check your autoloader folder tree for the existance of a file named Google_OAuth2.php
Run a project-wide (all files in your project) search for the string "Google_OAuth2.php" and another search for "class Google_OAuth2", look for any duplicate include(), include_once() or require_once().
(in phpStorm you click Cmd+Shift+F to open the path search, and in the options tab select "Whole project" in Scope.)
See where and if you are including\requiring\autoloading the Google_OAuth2 class file more then one time.
Finally, remove the duplicate.
Hope it helps a bit!
I am getting an error when I try to access my site (site on WordPress)
Fatal error: Call to undefined function themify_build_write_panels() in /home/ash/public_html/wp-content/themes/metro/theme- functions.php on line 931
My line 931 is =>
themify_build_write_panels( apply_filters(
'themify_theme_meta_boxes' ,
please help me!!
Thanks :)
This error is occurring because you did not define this function in functions.php
Check if you properly defined this function or not.
Find this function in your functions.php
function themify_build_write_panels(){
//statements
//statements
}
You are right you are new to WordPress but as a developer you should know programming and how functions are used in programming and also how to define them.
Error is transparent that you did not define your function.
I have been writing a function using the Shortcode Exec PHP plugin and the function works great when I run it inside the editor.
When I move it to a plugin I begin to see errors in the log such as this:
PHP Fatal error: Call to undefined function wp_create_category()
I realize that this is because of lack of includes, etc.
What is the correct way to include the built-in wordpress functions for a plugin?
My plugin uses the following wordpress functions
wp_create_category
username_exists
wp_generate_password
wp_create_user
wp_insert_post
update_post_meta
add_post_meta
Use below code, working fine , i have tested
require_once(ABSPATH . 'wp-config.php');
require_once(ABSPATH . 'wp-includes/wp-db.php');
require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
try including this to your file and let me know then --
require_once(WORDPRESS_HOME. 'wp-config.php');
require_once(WORDPRESS_HOME. 'wp-includes/wp-db.php');
require_once(WORDPRESS_HOME. 'wp-admin/includes/taxonomy.php');
I know this is very old question, but somehow noone mentioned solution I have in mind.
I think the correct way of doing this is by using correct init hook in your plugin. Adding require_once() seems like hack to me.
So some functions are being loaded only for admin area and some are only loaded for frontend. So depending on those use correct hook in your plugin.
add_action('init', function () {}
or
add_action('admin_init', function () {}
You can have both of them in one plugin of course.