Home > front end >  wordpress add_action not working in dynamic genereted file
wordpress add_action not working in dynamic genereted file

Time:03-27

I built a custom post type and i create a php file based on its settings.
this file created in wp-content/uploads/example folder.

then do loop over all post of this CPT and include all generated files.

this is code for include files:

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

and example of dynamic generated files content is :

<?php
    add_action('wp_login', function($user_id) {
        //action codes
    });
?>

this action working well when i put it inside main plugin file, but not working in this external file.
any idea?

CodePudding user response:

the problem was for

if ( !is_admin() )
  • Related