# Callbacks

Callbacks allow you to run custom JavaScript code in certain parts of execution. For example, if you have an Edit Comments plugin, you can run a custom function that initializes the plugin after a comment has been posted.

Each callback can pass arguments, which are documented below and will be in scope for any custom JS functionality that needs to run.

A common use-case of callbacks is another plugin that needs to run before, during, and after Ajaxify Comments loads.

### OnBeforeSelectElements Callback

This allows you to run JavaScript code before Ajaxify Comments has selected the DOM elements.

<figure><img src="/files/FvXkSsM1YbqLZHOPzCTZ" alt=""><figcaption></figcaption></figure>

One argument is in scope, which is a jQuery representation of the DOM elements selected.

If you'd rather use a native JS event, you can use the event: **`wpacBeforeSelectElements`**

```javascript
document.addEventListener( 'wpacBeforeSelectElements', function( e ) {
	console.log( 'Custom event fired:', e );
	console.log( 'Extracted body:', e.detail.dom );
} );
```

### OnBeforeSubmitComment Callback

<figure><img src="/files/FY4QdKBA2i6fNccUt0c0" alt=""><figcaption></figcaption></figure>

This callback provides no arguments.

If you'd rather use a native JS event, you can use the event: **`wpacBeforeSelectElements`**

<pre class="language-javascript"><code class="lang-javascript"><strong>document.addEventListener( 'wpacBeforeSelectElements', function( e ) {
</strong>	console.log( 'Custom event fired:', e );
} );
</code></pre>

### OnAfterPostComment Callback

<figure><img src="/files/gDfba5XMODf0aXi7qtXb" alt=""><figcaption></figcaption></figure>

#### Arguments:

1. commentUrl (string)
2. unapproved (boolean)

If you'd rather use a native JS event, you can use the event: **`wpacAfterPostComment`**

```javascript
document.addEventListener( 'wpacAfterPostComment', function( e ) {
	console.log( 'Custom event fired:', e );
	console.log( 'Extracted body:', e.detail.commentUrl, e.detail.unapproved );
} );
```

### OnBeforeUpdateComments Callback

<figure><img src="/files/593zx5ZvQgBOZQmDFZwQ" alt=""><figcaption></figcaption></figure>

#### Arguments

* newDom (jQuery DOM element)
* commentUrl (string)

If you'd rather use a native JS event, you can use the event: **`wpacBeforeUpdateComments`**

```javascript
document.addEventListener( 'wpacAfterPostComment', function( e ) {
	console.log( 'Custom event fired:', e );
	console.log( 'Extracted body:', e.detail.newDom, e.detail.commentUrl );
} );
```

### OnAfterUpdateComments Callback

<figure><img src="/files/JEM6lVcw6VfJOlFHPvWQ" alt=""><figcaption></figcaption></figure>

#### Arguments

* newDom (jQuery DOM element)
* commentUrl (string)

```javascript
document.addEventListener( 'wpacAfterUpdateComments', function( e ) {
	console.log( 'Custom event fired:', e );
	console.log( 'Extracted body:', e.detail.newDom, e.detail.commentUrl );
} );
```


---

# Agent Instructions: 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:

```
GET https://docs.dlxplugins.com/ajaxify-comments/advanced-topics/callbacks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
