Fatal error: Call to undefined function add_action()
i got some code for twitter updates-
function twitit() {
global $wp_query;
$thePostName = $wp_query->post->post_name;
echo '<div id="twitit">开发者_如何学Go<a href="http://twitter.com/home?status=Currently reading '.$thePostName.' : '.get_permalink($post->ID).'" title="Click to send this page to Twitter!" target="_blank">Share on Twitter</a></div>';
}
function widget_twitit($args) {
extract($args);
echo $before_widget;
echo $before_title;
echo $after_title;
twitit();
echo $after_widget;
}
function twitit_init() {
register_sidebar_widget(__('Twit It'), 'widget_twitit');
}
add_action("plugins_loaded", "twitit_init"); //line 30
?>
Fatal error: Call to undefined function add_action() in C:\xampp\htdocs\shizin\twitter.php on line 30
Just add the line require(dirname(__FILE__) . '/wp-load.php');
before the other functions. This should solve your problem.
Remember dirname(__FILE__)
is meant to point to the root wordpress directory, something like www.yourdomain.com/wordpress/
As the message says, you haven defined the function add_action()
and yet you're trying to use it.
Create it first.
I search your previous questions, and it seems that you copied the code of a wordpress plugin. In that case don't directly invoke the page, use the wordpress plugin system.
If add_action comes up undefined, you're trying to run it outside of your theme's core files - basically, it's running without any of the processing WordPress is supposed to do before hitting your custom functions. This should be in your theme's functions.php file or within a plugin, as Colin pointed out.
The line C:\xampp\htdocs\shizin\twitter.php
definitely suggests this is in the wrong place.
Move your call to add_action()
functions.php script inside your current active theme.(not in the main functions.php).
I faced the same issue. I created a WooCommerce plugin here and I got this issue.
My findings are that you have added add_action()
before WP functions are loaded.
Please make sure that It is loaded in functions.php
or after WP is initialized/loaded.
It's a core function and available if your code is activated trough WordPress and accessed trough WordPress.if you execute the file from the browser or directly then add_action is not available unless you require wp-load.php at the top of your file from the WordPress root.
精彩评论