php 致命错误:调用未定义的函数 add_action()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3947979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:31:46  来源:igfitidea点击:

Fatal error: Call to undefined function add_action()

phpwordpresspluginstwitter

提问by ppp

i got some code for twitter updates-

我有一些推特更新的代码-

function twitit() {
global $wp_query;
$thePostName = $wp_query->post->post_name;
echo '<div id="twitit"><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

致命错误:在第 30 行调用 C:\xampp\htdocs\shizin\twitter.php 中未定义的函数 add_action()

回答by hiprakhar

Just add the line require(dirname(__FILE__) . '/wp-load.php');before the other functions. This should solve your problem.

只需require(dirname(__FILE__) . '/wp-load.php');在其他功能之前添加该行。这应该可以解决您的问题。

Remember dirname(__FILE__)is meant to point to the root wordpress directory, something like www.yourdomain.com/wordpress/

记住dirname(__FILE__)是指指向 wordpress 根目录,例如 www.yourdomain.com/wordpress/

回答by Colin Hebert

As the message says, you haven defined the function add_action()and yet you're trying to use it.

正如消息所说,您已经定义了该函数add_action(),但您正在尝试使用它。

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.

我搜索了您之前的问题,您似乎复制了一个wordpress插件的代码。在这种情况下不要直接调用页面,使用 wordpress 插件系统。

回答by Gavin

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.

如果 add_action 出现未定义,则您正在尝试在主题的核心文件之外运行它 - 基本上,它在运行时没有任何 WordPress 在访问自定义函数之前应该执行的处理。正如 Colin 指出的那样,这应该在您主题的 functions.php 文件或插件中。

The line C:\xampp\htdocs\shizin\twitter.phpdefinitely suggests this is in the wrong place.

该行C:\xampp\htdocs\shizin\twitter.php肯定表明这是在错误的地方。

回答by Brane

Move your call to add_action()functions.php script inside your current active theme.(not in the main functions.php).

将您的调用移动到add_action()当前活动主题中的 functions.php 脚本。(不在主要的functions.php 中)。

回答by Alex

I faced the same issue. I created a WooCommerce plugin hereand I got this issue.

我遇到了同样的问题。我在这里创建了一个 WooCommerce 插件,但我遇到了这个问题。

My findings are that you have added add_action()before WP functions are loaded.

我的发现是您add_action()在加载 WP 功能之前添加了。

Please make sure that It is loaded in functions.phpor after WP is initialized/loaded.

请确保它是functions.php在 WP 初始化/加载之后或之后加载的。