Missing includes in Wordpress plugin (wp_insert_post function) - wordpress

I created a code which generates content and I want to populate Wordpress's database with it. I am using wp_insert_post and add_post_meta functions but every time I load which triggers the script I get this error:
Fatal error: Call to undefined function add_action() in \www\scriptmafia\wp-includes\post.php on line 153
To my mind it due to some missing includes. I have included post.php which it mentioned as required include for wp_insert_post function.
I know this is pretty common among newbies in WordPress. However, any solution I have tried have not worked for me.

require_once( ABSPATH . '/wp-load.php'); should solve it :)

Related

Console Error for woocommerce_shared_settings deprecation

I am working with WooCommerce for the first time and I am currently implementing WC filters on the shop page. The filters show up but are not functional, and the console is throwing the following errors:
ERROR 1: woocommerce_shared_settings filter in Blocks is deprecated. See https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/trunk/docs/contributors/block-assets.md
ERROR 2: deprecated.min.js?ver=932d8bb37da8bbb396a7a3f754345e08:2 select control in #wordpress/data-controls is deprecated since version 5.7. Please use built-in resolveSelect control in #wordpress/data instead.
The errors disappear when I remove the filters.
I have located the file where the deprecated code exists. I also read the WC docs about how to fix the issue and it presented this code:
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
Package::container()->get( AssetDataRegistry::class )->add( $key, $value )
It doesn't say where to put this code, though. Where should I put it to solve this issue?
I have the same problem.
Which is the file you located?
Here also says you have to add, on the "client side", this code:
wc.wcSettings.getSetting( 'key' );

WordPress function undefined when adding a shortcode

We would like to add a custom function of a shortcode in wp_include/functions.php.
This is the function:
function url_id() {
$apiUrl = 'https:exampleurl.api.com/exemple123';
$response = wp_remote_get($apiUrl);
return wp_remote_retrieve_body($response);
}
add_shortcode('session_id', 'url_id');
But we were getting this error:
There has been a critical error on this website.
Learn more about troubleshooting WordPress.
After reading some more troubleshooting documentation we added these two lines in wp-config:
define( 'WP_DEBUG', true );
define( 'WP_CACHE', true );
And after that we are getting this error:
Fatal error: Uncaught Error: Call to undefined function add_shortcode() in /home/c1624672c/public_html/woo/wp-includes/functions.php:18 Stack trace: #0 /home/c1624672c/public_html/woo/wp-settings.php(111): require() #1 /home/c1624672c/public_html/woo/wp-config.php(104): require_once('/home/c1624672c...') #2 /home/c1624672c/public_html/woo/wp-load.php(50): require_once('/home/c1624672c...') #3 /home/c1624672c/public_html/woo/wp-blog-header.php(13): require_once('/home/c1624672c...') #4 /home/c1624672c/public_html/woo/index.php(17): require('/home/c1624672c...') #5 {main} thrown in /home/c1624672c/public_html/woo/wp-includes/functions.php on line 18
There has been a critical error on this website. Learn more about troubleshooting WordPress.
If you want to add custom shortcodes to your website, that can be done via a custom plugin, or in your theme (as I suspect you're trying to do).
Instead of editing wp-includes/functions.php, add your code to wp-content/themes/<name-of-your-theme>/functions.php.
You should never edit the core files of WordPress, which includes all files in /wp-admin and /wp-includes. When you upgrade WordPress in the future, these files will be overwritten, and you'll lose any changes you've made.
The error message occurs because you're attempting to use the add_shortcode() function before WordPress has fully loaded, and defined all available functions.
Please change the shortcode parameter session_id because session_id predefined function in wordpress see the blow or your code
add_shortcode('session_id', 'url_id');
But right code will be work when you change the session_id.

Fatal error: Call to undefined function add_action() in

I am trying to add action in a plugin in a file which is included to the base php plugin file:
add_action( 'wp_ajax_my_action', 'my_action' );
Then I am requiring the file in the main plugin file:
require_once plugin_dir_url(__FILE__) . 'api/index.php';
, but I am getting this error:
Fatal error: Call to undefined function add_action() in
This code works fine, if it is in the main plugin file, but if I put it in another file it doesn't.
I have followed some answers to similar questions to include wp-load, but I am getting the same error.
Following this code, I think you might solve your issue.
// You must define the wp_ajax_my_action callback
function my_action( $my_action_callback ) {
// Write your action
};
// add the action
add_action( 'wp_ajax_my_action', 'my_action');
If you want to get more info, please refer to http://hookr.io/actions/wp_ajax_my_action/
Good luck!

Call to undefined function add_options_page()

I developed the plugin for my wordpress project. I successfully tested it on my local xampp server with 5.3 php. Then I uploaded my project to the web hosting with php 5.2. First trouble which with I faced off was unsupporting anonymous functions in php 5.2. No issue, I redeclared all functions with names. But then I got error Call to undefined function add_options_page(), which I counldn't explain. Plz help me guys with your advices
My part of code:
function mainPage(){
///some code
}
function mainPage2(){
add_options_page('Submissions of MAIN page contact form', 'Submissions of MAIN page contact form', 'manage_options','ea_submissions2', mainPage());
}
add_action('admin_menu',mainPage2());
I think something wrong with my funcitons, look through it please.
There is no issue with php 5.2 as I thought, this part of code also doesn't work with php 5.3! Something wrong with my code
I had a similar problem, turns out I was running a function too early:
Use admin_init hook instead of init
Hopefully that helps someone out :D
This doesn't work because you have normal function not wrapped in a class, and because add_options_page does not work yet by that time that is why you get the error.
A fix would be to use an anonymous function in the add action call, but hence that does not work on php 5.2.
So long story short, this is fixable though, but you shouldn't run php 5.2 anymore in the first place. PHP 5.5 is already in development and 5.3 is facto standard these days. A solution is to ask your hosting company to upgrade php to at least 5.3 so that you can use anonymous functions and you can hook it to the add action call.
Or, wrap it all in a class and on the admin init function create the new class.
You have to call your code inside admin_menu like this
add_action( 'admin_menu', array(&$this, 'addWidgetSettingsMenu' ));
Aware that this is an old question, this is your problem:
add_action('admin_menu',mainPage2());
Here you are invoking the mainPage2() function and adding the return value of that function as an argument to the add_action method.
You should do
add_action('admin_menu', 'mainPage2');
This way, the mainPage2 function will be called when admin_menu happens.
Try it without giving space in title :-
SubmissionsOfMainPageContactForm

How to re-use Drupal module code using module_load_include

I am using the node_clone module with great effect but there is a need in my project to use node_clone functions from my custom module. So I put in the following code:
module_load_include('inc', 'node_clone', 'clone.pages');
function mymodule_init(){
clone_node_save(118);
}
That code returns Fatal error: Call to undefined function clone_node_save().
My modules are categorized by source into directories labelled mine and contrib. Node_save is in contrib while myModule is in mine.
So, I amended the code acordingly as follows:
module_load_include('inc', '../../contrib/node_clone', 'clone.pages');
but I get the same error.
What am I doing wrong?
Use:
require_once DRUPAL_ROOT . '/sites/all/modules/contrib/node_clone/clone.pages.inc';
From the module_load_include API:
Do not use this function in a global context since it requires Drupal
to be fully bootstrapped, use require_once DRUPAL_ROOT .
'/path/file' instead.
It's a bit misleading, the folder is named 'node_clone' but the module is actually called 'clone', so you want:
module_load_include('inc', 'clone', 'clone.pages');
hook_init() runs pretty early on so if you don't need the clone module's functions before hand, you'd be better off moving the code into the hook:
function mymodule_init(){
module_load_include('inc', 'clone', 'clone.pages');
clone_node_save(118);
}

Resources