我无法理解的是如何加载Wordpress函数。当我向这样的Wordpress钩子添加动作时。
<head>
<script src="path/to/file.js" type="text/javscript></script>
<script>
$(window).load(function() {
doFunction(variable);
});
</script>
</head>
此代码适用于我想要做的事情,但这个add_action函数来自何处。我知道Wordpress正在以某种方式处理它,但我不知道如何在不实际包含文件的情况下调用它。我试图将文件包含在另一个文件中,该文件将包含在此文件之前,但之后我会得到一个未定义错误的函数。我真的很想知道这背后的逻辑。
答案 0 :(得分:1)
你会发现这个函数在wp-includes / plugin.php中初始化了:
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
您可以在包含此文件的任何地方使用该功能。
Wordpress有这个文档和更多@ https://developer.wordpress.org/reference/functions/add_action/
答案 1 :(得分:1)
它的工作原理是,在打开的WordPress网站上运行的第一个文件是index.php,它需要wp-blog-header.php
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
然后wp-blog-header.php需要wp-load.php和template-loader.php
// Load the WordPress library.
require_once( dirname(__FILE__) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
这里wp-load.php文件需要wp-config.php
if ( file_exists( ABSPATH . 'wp-config.php') ) {
/** The config file resides in ABSPATH */
require_once( ABSPATH . 'wp-config.php' );
}
和wp-config.php文件需要wp-settings.php
require_once(ABSPATH . 'wp-settings.php');
和wp-settings加载wp-includes / plugin.php文件
define( 'WPINC', 'wp-includes' );
// Include files required for initialization.
require( ABSPATH . WPINC . '/load.php' );
require( ABSPATH . WPINC . '/default-constants.php' );
require_once( ABSPATH . WPINC . '/plugin.php' );
和wp-includes / plugin.php文件中有add_action函数
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
和wp-includes / template-loader.php加载主题模板。
require_once( ABSPATH . WPINC . '/template-loader.php' );