Fatal error: Call to undefined function add_action() in - wordpress

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!

Related

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.

Replacing a core WordPress function

I am trying to replace the core WP function wp_delete attachment. In my functions.php file I am adding the following code:
add_action( 'init', 'remove_my_action' );
function remove_my_action(){
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_filter('delete_attachment','wp_delete_attachment',10,2);
}
And then the copy of the replaced function goes here with edited code.
This does not work. I get the error: Cannot redeclare wp_delete_attachment(). I've tried a number of other methods, but can't get it to work.
The bottom line is that I need to add some code to the middle of wp_delete_attachment function. If I can replace that function with my version somehow or add the code to the existing function without editing the actual code in the wp-includes/post.php file (so that version updates don't override my code), I would be satisfied with either. How can this be done? All the options I found through questions have not solved the issue. Thanks!!
You'll need to name your copy of wp_delete_attachment to a unique name. Perhaps namespace it with your site name like function my_site_wp_delete_attachment().
Also, I believe you'll need to use add_action instead of add_filter.
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_action( 'delete_attachment', 'my_site_wp_delete_attachment');

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);
}

fatal error: call to undefined function wp_headers()

I get error like call to undefined function wp_headers() in wp_content in my theme folder.
how to solve this error?
You should be more specific with your error message, but likely you mean to use wp_head() and not wp_headers() in your header.php file.
A good place to start with learning theme development is here: http://codex.wordpress.org/Theme_Development

Missing includes in Wordpress plugin (wp_insert_post function)

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 :)

Resources