Hooks - PHP

SleekByte provides several PHP hooks that allow you to customize its behavior and extend its functionality. These hooks give you control over snippet loading, default configurations, and file discovery.

snippets/paths

This filter allows you to add custom directories where SleekByte will look for snippets. By default, SleekByte uses the uploads directory, but you can add theme directories, plugin directories, or any custom location.

            add_filter('sleekByte/snippets/paths', function ($sources) {
      // Add custom snippets directory
      $sources[] = [
        'name' => 'Custom Snippets',
        'path' => get_template_directory() . '/snippets',
        'sort' => 50,
      ];

      // Add another directory for client-specific snippets
      $sources[] = [
        'name' => 'Client Snippets',
        'path' => WP_CONTENT_DIR . '/client-snippets',
        'sort' => 75,
      ];

      return $sources;
    });
  
                    
Copy

Parameters:

  • $sources (array) - Array of snippet source directories

Source Array Structure:

  • name (string) - Display name for the source in SleekByte interface
  • path (string) - Absolute path to the directory containing snippets
  • sort (int) - Sort order (lower numbers appear first, default is 100)

This hook is particularly useful for:

  • Adding theme-specific snippet directories
  • Creating client-specific snippet collections
  • Organizing snippets by functionality or project
  • Making snippets portable across WordPress installations

snippets/defaults/context

This filter allows you to change the default context for snippets that don't specify a context in their snippet.json configuration. The default context determines where snippets load (frontend, admin, or block-editor).

            add_filter('sleekByte/snippets/defaults/context', function ($defaultContext) {
      // Change default context to load snippets in admin by default
      return 'admin';

      // Or return an array for multiple contexts
      // return ['frontend', 'admin'];
    });
  
                    
Copy

Parameters:

  • $defaultContext (string) - The default context, initially set to 'frontend'

Available Contexts:

  • frontend - Public-facing pages (default)
  • admin - WordPress admin area
  • block-editor - Gutenberg block editor

This hook is useful when:

  • Most of your snippets are admin-focused
  • You want to change the global default behavior
  • You're building a site where snippets primarily serve internal users

snippets/defaults/hook

This filter allows you to change the default WordPress hook for PHP file execution. This affects all snippets that don't specify a hook in their snippet.json configuration.

            add_filter('sleekByte/snippets/defaults/hook', function ($defaultHook) {
      // Change default hook to 'init' instead of 'wp'
      return 'init';

      // This affects all snippets that don't specify a hook
      // in their snippet.json configuration
    });
  
                    
Copy

Parameters:

  • $defaultHook (string) - The default hook, initially set to 'wp'

Common WordPress Hooks:

  • wp - After WordPress loads (default)
  • init - During WordPress initialization
  • wp_head - In the document head
  • wp_footer - Before closing body tag
  • admin_init - Admin area initialization
  • after_setup_theme - During theme setup

This hook is beneficial when:

  • You need snippets to run earlier in the WordPress loading process
  • Most of your snippets require a specific execution timing
  • You want to optimize performance by running code at the appropriate time

Hook usage examples

Complete custom setup

            // Add custom snippet directories
    add_filter('sleekByte/snippets/paths', function ($sources) {
      $sources[] = [
        'name' => 'Theme Snippets',
        'path' => get_template_directory() . '/sleek-snippets',
        'sort' => 10,
      ];

      $sources[] = [
        'name' => 'Must-Use Snippets',
        'path' => WPMU_PLUGIN_DIR . '/snippets',
        'sort' => 5,
      ];

      return $sources;
    });

    // Change default context to admin
    add_filter('sleekByte/snippets/defaults/context', function ($defaultContext) {
      return 'admin';
    });

    // Change default hook to init
    add_filter('sleekByte/snippets/defaults/hook', function ($defaultHook) {
      return 'init';
    });
  
                    
Copy

Conditional default context

            add_filter('sleekByte/snippets/defaults/context', function ($defaultContext) {
      // Use admin context for staging sites, frontend for production
      if (wp_get_environment_type() === 'staging') {
        return 'admin';
      }

      return 'frontend';
    });
  
                    
Copy

Environment-based snippet paths

            add_filter('sleekByte/snippets/paths', function ($sources) {
      // Add development snippets only in development environment
      if (wp_get_environment_type() === 'development') {
        $sources[] = [
          'name' => 'Development Snippets',
          'path' => WP_CONTENT_DIR . '/dev-snippets',
          'sort' => 1,
        ];
      }

      // Add production-only snippets
      if (wp_get_environment_type() === 'production') {
        $sources[] = [
          'name' => 'Production Snippets',
          'path' => WP_CONTENT_DIR . '/prod-snippets',
          'sort' => 200,
        ];
      }

      return $sources;
    });
  
                    
Copy

These hooks provide powerful ways to customize SleekByte's behavior to match your specific development workflow and project requirements.