$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - add_action not calling back to function|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - add_action not calling back to function

matteradmin11PV0评论

I am working on a plugin, but add_action doesn't call the callback. The code is as follows:

require plugin_dir_path( __FILE__ ) . 'includes/class-network.php';

$distro = new Classnetwork();

includes/class-network.php:

class Classnetwork {
    public function __construct() {
        add_action( 'publish_post', array ($this, 'cdn_capture_data') );
    }

    public function cdn_capture_data( $post_id, $post ) {
        print_r ($post);            
}
}

Nothing is printed, not error, it just doesn't do anything every time I post a new post. Any ideas where is the error? The __construction is called but not the callback from add_action.

I am working on a plugin, but add_action doesn't call the callback. The code is as follows:

require plugin_dir_path( __FILE__ ) . 'includes/class-network.php';

$distro = new Classnetwork();

includes/class-network.php:

class Classnetwork {
    public function __construct() {
        add_action( 'publish_post', array ($this, 'cdn_capture_data') );
    }

    public function cdn_capture_data( $post_id, $post ) {
        print_r ($post);            
}
}

Nothing is printed, not error, it just doesn't do anything every time I post a new post. Any ideas where is the error? The __construction is called but not the callback from add_action.

Share Improve this question asked Dec 10, 2018 at 9:27 GonzaloGonzalo 1672 silver badges10 bronze badges 2
  • set define('WP_DEBUG', false); to define('WP_DEBUG', true); in wp-config.php file @Greg Winiarski, answer should do the job. You didn't specify the execution order and default number of arguments If you didn't specify them, they default to 10 and 1 ( in your case you passed two arguments to function attached to hook ) – maverick Commented Dec 10, 2018 at 11:03
  • @maverick you were right, the issue was that I didn't indicate the how many parameters. Thanks – Gonzalo Commented Dec 10, 2018 at 15:14
Add a comment  | 

1 Answer 1

Reset to default 1

I understand that you are adding the post from wp-admin / Posts / Add New panel? If so then note that the publish_post action is run and the WP is doing redirect so you cannot see any printed data.

Also, your add_action call is missing 4th argument which is number of arguments passed to cdn_capture_data(), by default there is only 1 argument passed so in your case the $post is always null.

The correct code (to actually print the result) should be

class Classnetwork {
    public function __construct() {
        add_action( 'publish_post', array ($this, 'cdn_capture_data'), 10, 2 );
    }

    public function cdn_capture_data( $post_id, $post ) {
        print_r ($post);  
        exit;          
    }
}
Post a comment

comment list (0)

  1. No comments so far