PHP code example of pinkcrab / hook-loader

1. Go to this page and download the library: Download pinkcrab/hook-loader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pinkcrab / hook-loader example snippets


$loader = new Hook_Loader();

// Add actions
$loader->action('some_action', 'my_callback'); // Registered front and admin
$loader->front_action('some_action', 'my_callback'); // Frontend only
$loader->admin_action('some_action', 'my_callback'); // Admin only

// Filters
$loader->filter('some_filter', 'strtolower'); // Registered front and admin
$loader->front_filter('some_filter', 'strtolower'); // Frontend only
$loader->admin_filter('some_filter', 'strtolower'); // Admin only

// Remove hooks
$loader->remove('some_action', 'someone_else_callback', 10); 
$loader->remove_filter('some_action', 'someone_else_callback', 10); // Does the same as remove()
$loader->remove_action('some_action', 'someone_else_callback', 10); // Does the same as remove()

// Ajax and Shortcode.
$loader->shortcode('my_shortcode', 'shortcode_callback');
$loader->ajax('my_action', 'my_callback', true, true);

// Once all have been added, just process with 
$loader->register_hooks();



$loader->ajax('my_action', 'my_callback', true, true); // For both logged in and out users.
$loader->ajax('my_action', 'my_callback', false, true); // For only logged in users.
$loader->ajax('my_action', 'my_callback', true, false); // For only logged out users.
 php
// Simple example
$loader->shortcode(
  'testShortCode',
  function( $atts ) {
    echo $atts['text'];
  }
);

// Called with shortcode as normal (either in php or wp-admin text input) 
do_shortcode( "[testShortCode text='yes']" ); // yes

// Part of a class
class ShortCode {

  protected $some_service;

  public function __construct(Some_Service $some_service){
    $this->some_service = $some_service;
  }

  public function register(Hook_Loader $loader){
    $loader->shortcode('my_shortcode', [$this, ['render_shortcode']]);
  }

  public function render_shortcode(array $args){
    print $this->some_service->do_something($args['something']);
  }
}

 php
add_filter(Hook_Collection::REGISTER_HOOKS, function($hooks){
  // Do something with the hooks
  return $hooks;
});