Without repeating too much that has been covered in the answers above; this is just a best practices approach. I often use this in plugin development where I need to have separate classes like in the wppb.me plugin starter template.
It makes sense to register all your admin hooks in the loader. In the case of the shortcode, you can add the add_action shortcode hook in the hooks loader method :
/**
* Register all of the hooks related to the admin area functionality
* of the plugin
*/
private function define_admin_hooks() {
$plugin_admin = new instance( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'init', $plugin_admin, 'register_shortcodes');
}
The main benefit I have with this approach is for easy code maintenance since the code is modular. You can add several other hooks in the same method which makes it easy to track and change things, especially for a large plugin.
It, therefore, means in the class where you want to use add_shortcode
you do not have to run the action hook in the constructor method, you can now use it in a custom method like :
public function register_shortcodes(){
add_shortcode('recent-posts', array($this, 'recent_posts_function'));
}
add_shortcode
in a function and calling that function with a hook gives you more control of when the shortcodes are added. For example, you may want to add those shortcodes when a specific plugin is activated, but not otherwise. – admcfajn Feb 20 '19 at 6:26