h5p / h5p-wordpress-plugin

Adds support for H5P Content in WordPress.
https://wordpress.org/plugins/h5p/
71 stars 74 forks source link

feature(h5p-content-hooks) : Add the ability to extend h5p plugin upon content crud #115

Open HaidarZ opened 4 years ago

HaidarZ commented 4 years ago

Hello guys,

Is it possible to add hooks for h5p content create/update/delete , just like what you've done with h5p_mods.
In that way we can easily integrate the h5p content within wordpress. For example we can create a custom post upon content creation that includes the h5p_content shortcode in it's body.
All of this process is now done manually and not easy for non-technical content editors.

Psuedo code

function handle_h5p_content_creation($content){
   wp_insert_post( array(
    'post_type' => 'lesson' ,
    'post_status' => 'publish',
    'post_title' => $content->title,
    'post_content' => '[h5p id="'. $content->id . '"]' ,
    ));
}

add_action('h5p_content_created', 'handle_h5p_content_creation' );

Just add the do_action hooks after the creation/update/deletion of the content.

Regards,

kraftner commented 1 year ago

Although hacky and messy I needed to get this to work and did it like this:

  1. Create a new WordPress plugin
  2. Copy public/class-h5p-event.php into that plugin.
  3. Add an autoloader to this plugin that loads this class. Make sure that this autoloader runs before the one from H5P. The easiest way to do this setting prepend to true on spl_autoload_register or make it a mu-plugin.
  4. Edit the Constructor of the copied H5P_Event and add do_action() calls for the intended event. Something like if($type === 'content'){ do_action('my_h5p_hooks_plugin . '_' . $sub_type); }

I have intentionally only added instructions here and no final code since this is hacky and you need to fully understand what you are doing here including being aware that you'll need to repeat this on every H5P update to handle the case of H5P_Event being changed upstream.

If you do not understand what you are doing here and/or forget to keep this in sync with H5P updates this will probably end in a fatal error taking down your entire site at some point.

You have been warned. :wink: