> For the complete documentation index, see [llms.txt](https://framework.deep-web-solutions.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://framework.deep-web-solutions.com/utilities-module/hooks-service/scoped-handler.md).

# Scoped Handler

In addition to the default hooks handler explained on the previous page, the Utilities Module comes pre-packaged with one more hooks handler -- the so-called [`ScopedHooksHandler`](https://github.com/deep-web-solutions/wordpress-framework-utilities/blob/master/src/includes/Hooks/Handlers/ScopedHooksHandler.php).

The scoped handler inherits the `DefaultHooksHandler` so it still keeps a list of hooks arrays and registers them on `run`. However, the hooks registered have a so-called *scope* which is defined by a starting hook and an ending hook.&#x20;

Basically this handler will automagically register all its hooks an a constructor-given hook and will unregister them on another constructor-given hook. This is useful for scenarios where you only want to enqueue some hooks during a specific method call, for example, and don't want them interfering with the rest of the request. Your custom filter for modifying SQL queries should really not bring the whole page down, but only your own custom output if something should go wrong.

You can use it like this:

```php
<?php

use DeepWebSolutions\Framework\Utilities\Hooks\Handlers\ScopedHooksHandler;

class MyClass {
    public function output_my_content() {
        do_action( 'before-my-awesome-output' );
        
        // output ...
        
        do_action( 'after-my-awesome-output' );
    }
}

$scoped_handler = new ScopedHooksHandler( 
        'my-class-output-handler', 
        array( 'hook' => 'before-my-awesome-output' ), 
        array( 'hook' => 'after-my-awesome-output' )
    );
$scoped_handler ->add_filter( ... );
$scoped_handler ->remove_action( ... );

$my_class = new MyClass();
$my_class->output_my_content();
```

{% hint style="warning" %}
The scoped handler uses different semantics for the methods `remove_filter` and `remove_action`. Basically their purpose is to instruct the handler to temporarily unregister those hooks during the given scope.

If you want to unregister hooks that you've added to the handler using `add_action` and `add_filter`,  please use the methods `remove_added_filter` and `remove_added_action`, respectively.
{% endhint %}

Of course, it's also possible to call the `run` and `reset` action methods manually by simply not setting the start and end hooks in the constructor:

```php
<?php

$scoped_handler = new ScopedHooksHandler( 'my-class-output-handler' );
$scoped_handler ->add_filter( ... );
$scoped_handler ->remove_action( ... );

$my_class = new MyClass();

$scoped_handler->run();
$my_class->output_my_content();
$scoped_handler->reset();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://framework.deep-web-solutions.com/utilities-module/hooks-service/scoped-handler.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
