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.
Related
I'm using prestashop 1.7.2.0 to write an admin module that adds nested categories and products from a csv file. I understood that addJQuery() is deprecated and I'm trying to get jquery code to work. I get Uncaught ReferenceError: $ is not defined.
I have created a displayBackOfficeHeader hook with the following:
public function hookDisplayBackOfficeHeader(){
$this->context->controller->addJS($this->_path.'/js/jqShim.min.js');
$this->context->controller->addCSS($this->_path.'/css/getcontent.css');
$this->context->controller->addJS($this->_path.'/js/getcontent.js');
}
as you can see I have tried including jqShim.min.js and I still got the same error. what am I missing?
Thanks
You can include $this->context->controller->addJquery(); in first line of your hookDisplayBackOfficeHeader() function.
I have been getting this error since I updated my woocommerce plugin:
Fatal error: Call to undefined function array_replace_recursive()
The errror is on line 586:
enter image description here
I have found some solutions in this quote here:
Fatal error: Call to undefined function array_replace_recursive() for CI Controller solution
But the problem is that i really do not know what to change in my code or where, in order fot the error to stop popping up. Any help would be greatly appreciated.
Check your PHP version. array_replace_recursive only available in PHP >= 5.3.
Reference:
array_replace_recursive
since I updated my WooCommerce plugin
I suggest you check once by deactivating all plugins except WooCommerce and by activating default WordPress theme. It may be possible that any of your plugin is not compatible with the latest WooCommerce plugin.
Let me know if that doesn't work...
I am using Joomla 2.5 and DirectPHP. I can run PHP code in an article just fine. I would like to include a library of PHP functions to be available for use in all articles. What is the best way to include the function library?
I tried including (with require) from the template's index.php and even putting the functions directly in index.php, as suggested here. None of the functions are defined in the article. A test with variables and global variables also finds them undefined in the article.
I then created a custom HTML module with the PHP functions that I then used in my template, but they are still undefined in the article. Oddly enough, if I try and declare the function again in the article I get "Cannot redeclare...". How can the function be both undefined, not re-defineable?
I also tried using a namespace to define and use my functions. Same result.
Code in my custom html module:
<?php
namespace c6;
function testit5()
{
echo "hello world 5";
}
?>
Code in my article:
<?php
namespace c6;
testit5();
?>
Result:
Fatal error: Call to undefined function c6\testit5() in /home/testsite/www/www/c6test/plugins/content/DirectPHP/DirectPHP.php(58) : eval()'d code on line 5
go to,
Login to admin -> Extensions Menu -> Module Manager -> Create New Module -> Choose Custom HTML
and create you own module and add php functions.
I figured it out! My problem was the order Joomla processes things. Apparently the article is processed first, then the template. So my code using the function executed before the module that defined the function was executed. (When I got the "cannot redeclare" error, it was coming from the template, not the article.) I moved my loadposition from the template to the article and it works.
I have the following setup in a wordpress theme:
in functions.php i have defined a function called function listing_pins()
in functions.php i also have
add_action('wp_enqueue_scripts', 'load_scripts');
function load_scripts() {
wp_enqueue_script('googlecode_regular', get_template_directory_uri().'/js/google_js/google_map_code.php',array('jquery'), '1.0', true);
}
and finally the google_map_code.php file
header("Content-Type: text/javascript");
include_once("{$_SERVER['DOCUMENT_ROOT']}/wp-load.php");
$general_latitude = get_option('wp_estate_general_latitude','');
$general_longitude= get_option('wp_estate_general_longitude','');
$place_markers=listing_pins();
....
I'm not gonna list the whole functions since i don't think they are not relevant for my question.
The setup above works perfectly on my server but on a client setup i receive
Fatal error: Call to undefined function listing_pins() in /home/content/64/45654654/html/blue/wp-content/themes/wpestax/js/google_js/google_map_code_home.php on line 6
Is anything i;m doing wrong ? Wordpress is not aware of the things defined in functions.php when use wp_enqueue_script ? But in this case why this works on my server and not on client one.
I am trying to integrate WordPress and MyBB forums. Specifically, I just want to add WordPress's navbar (the new menu) to my MyBB website. Following the CODEX example, I've added the following to my header.php of my MyBB installation:
require('/home/linuxdis/public_html/wp-blog-header.php');
However, I get 500 error when navigating to the forum. Examining error_log revealed this:
PHP Fatal error: Cannot redeclare is_super_admin() (previously declared in /home/linuxdis/public_html/forum/inc/functions.php:5484) in /home/linuxdis/public_html/wp-includes/capabilities.php on line 1213
Bummer, the functions are named the same. Other than renaming one of the functions and probably breaking absolutely everything, is there a way to go around this? :/
User require_once() instead of require() and it will make sure it will only include that file once per page.
Although not ideal in this case, you could wrap each function in a function_exists() check:
if(!function_exists('myfunc') {
function myfunc() {....}
}
This might be your only option if there are direct clashes with WP/MyBB, you're stuck if they both need is_super_admin() though.