Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * USAGE: include this file,
  5. * in the form template, add an hidden input with name "action" and value "X"
  6. * call register_form_action('X', *callback);
  7. */
  8.  
  9. if ( ! function_exists( 'add_action' ) ) {
  10. die( 'Nothing to see here, move along' );
  11. }
  12.  
  13. $form_actions_list = [];
  14. $form_action_prefix = 'action_';
  15. if ( ! function_exists( 'register_form_action' ) ) {
  16. function register_form_action( $name, $callback ) {
  17. global $form_actions_list, $form_action_prefix;
  18. $form_actions_list[] = $name;
  19. add_action( $form_action_prefix . $name, $callback );
  20. }
  21. }
  22.  
  23. add_action( 'init', function () {
  24. if ( isset( $_POST['action'] ) ) {
  25. global $form_actions_list, $form_action_prefix;
  26. $actionName = $form_action_prefix . $_POST['action'];
  27. if ( in_array( $_POST['action'], $form_actions_list ) && has_action( $actionName ) ) {
  28. do_action( $actionName );
  29. }
  30. }
  31. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement