wordpress add_action 无法在动态生成的文件中工作
wordpress add_action not working in dynamic genereted file
我构建了一个自定义 post 类型,并根据其设置创建了一个 php 文件。
此文件在 wp-content/uploads/example 文件夹中创建。
然后遍历此 CPT 的所有 post 并包括所有生成的文件。
这是包含文件的代码:
if ( ! function_exists('include_hook_files') ) {
function include_hook_files() {
$hooks_folder = check_hook_folder_exists();
if ( ! $hooks_folder ) return;
$args = array(
'post_type' => 'customhook',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
echo ( $loop->post_count );
while ( $loop->have_posts() ) {
$loop->the_post();
// echo get_the_title();
require_once trailingslashit($hooks_folder) . 'custom-hook-' . get_the_ID() . '.php';
}
wp_reset_query();
}
}
}
if ( !is_admin() ) {
add_action( 'template_redirect', 'include_hook_files',2 );
}
动态生成的文件内容示例为:
<?php
add_action('wp_login', function($user_id) {
//action codes
});
?>
当我把它放在主插件文件中时,这个动作运行良好,但在这个外部文件中不起作用。
有什么想法吗?
问题是
if ( !is_admin() )
我构建了一个自定义 post 类型,并根据其设置创建了一个 php 文件。
此文件在 wp-content/uploads/example 文件夹中创建。
然后遍历此 CPT 的所有 post 并包括所有生成的文件。
这是包含文件的代码:
if ( ! function_exists('include_hook_files') ) {
function include_hook_files() {
$hooks_folder = check_hook_folder_exists();
if ( ! $hooks_folder ) return;
$args = array(
'post_type' => 'customhook',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) {
echo ( $loop->post_count );
while ( $loop->have_posts() ) {
$loop->the_post();
// echo get_the_title();
require_once trailingslashit($hooks_folder) . 'custom-hook-' . get_the_ID() . '.php';
}
wp_reset_query();
}
}
}
if ( !is_admin() ) {
add_action( 'template_redirect', 'include_hook_files',2 );
}
动态生成的文件内容示例为:
<?php
add_action('wp_login', function($user_id) {
//action codes
});
?>
当我把它放在主插件文件中时,这个动作运行良好,但在这个外部文件中不起作用。
有什么想法吗?
问题是
if ( !is_admin() )