PHPackages                             nbwphooks/wphooks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nbwphooks/wphooks

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nbwphooks/wphooks
=================

0.4.0(4y ago)09MITPHP

Since May 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/nabilabdelber/nbwphooks)[ Packagist](https://packagist.org/packages/nbwphooks/wphooks)[ RSS](/packages/nbwphooks-wphooks/feed)WikiDiscussions main Synced today

READMEChangelog (3)DependenciesVersions (6)Used By (0)

WP\_AJAX
========

[](#wp_ajax)

### A simple class for creating concise WordPress AJAX actions

[](#a-simple-class-for-creating-concise-wordpress-ajax-actions)

```
Class ExampleAJAX extends WP_AJAX
{
    protected $action = 'example';

    protected function run(){
        echo "Sucess!";
    }
}

ExampleAJAX::listen();

// http://example.com/wp-admin/admin-ajax.php?action=example
```

Introduction: [Medium Post](https://medium.com/@AnthonyBudd/wp-ajax-97d8f1d83e26#.pzyhw22zd)
============================================================================================

[](#introduction-medium-post)

---

### Features

[](#features)

- Simple to use
- Automatiocaaly dies when finished
- Lots of useful helpers

---

### Installation

[](#installation)

Require WP\_AJAX with composer

```
$ composer require anthonybudd/wp_ajax

```

#### Or

[](#or)

Download the WP\_AJAX class and require it at the top of your functions.php file.

```
    require 'src/WP_AJAX.php';
```

---

### Setup

[](#setup)

You will need to create a new class that extends WP\_AJAX. This class must have one protected property called $action and one protected method named run(). $action will be the AJAX action name [See wp\_ajax\_(action)](https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)).

```
Class Example extends WP_AJAX
{
    protected $action = 'example';

    protected function run(){
        echo "Success!";
    }
}
```

---

### Listen

[](#listen)

Next you have to call the static method listen(). This will create all of the hooks so WordPress knows to call the run() method when the correct AJAX endpoint is hit. Note: You will need to call the listen() method for each of your AJAX actions.

```
ExampleAJAX::listen();
```

If you would like to only allow signed in users to access your AJAX endpoint add the argument FALSE to the listen method.

```
ExampleAJAX::listen(FALSE);
```

---

### JSON Response

[](#json-response)

If you want to respond to an AJAX request with data the JSONResponse() method will automatically set the content type header to ‘application/json’ and JSON encode the data provided to the method.

I am aware that WordPress has a function called [wp\_send\_json()](https://codex.wordpress.org/Function_Reference/wp_send_json) but, due to the fact that I know how much it annoys WP developers that I have included this method, I will not be removing it.

```
Class ExampleAJAX extends WP_AJAX{
    ..

    protected function run(){
        $post5 = get_post(5);

        $this->JSONResponse($post5);
    }
}
```

---

### Helper Methods

[](#helper-methods)

```
Example::url() // Returns the url of the ajax endpoint. Example http://ajax.local/wp/wp-admin/admin-ajax.php?action=example

$this->isLoggedIn(); // Returns TRUE or FALSE if the current visitor is a logged in user.

$this->has($key); // has() will return TRUE or FALSE if an element exists in the $_REQUEST array with a key of $key

$this->get($key, [ $default = NULL ]); // The get() method will return the specified HTTP request variable. If the variable does not exist it will return NULL by default. If you would like to set a custom string as the default, provide it as the second argument.

$this->requestType(); // Returns 'PUT', 'POST', 'GET', 'DELETE' depending on HTTP request type

$this->requestType('POST'); // Returns (bool)

$this->requestType(['POST', 'PUT']); // Returns (bool)
```

---

### Example

[](#example)

```
Class CreatePost extends WP_AJAX
{
    protected $action = 'create_post';

    protected function run(){
        if($this->isLoggedIn()){
            $post = [
                'post_status' => 'publish'
            ];

            if( $this->requestType(['POST', 'put']) ){
                $post['post_content'] = 'This request was either POST or PUT';
            }else if( $this->requestType('get') ){
                $post['post_content'] = 'This request was GET';
            }

            $post['post_title'] = sprintf('This post was created by %s', $this->user->data->user_nicename);

            wp_insert_post($post);

            $this->JSONResponse($post);
        }
    }
}

CreatePost::listen();

// http://example.com/wp-admin/admin-ajax.php?action=create_post
```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

1819d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d84d6d2d8a84a87c6ebc72a916d689945d4e226b9aafe84c73ab7603b673987c?d=identicon)[nabil.abdelber](/maintainers/nabil.abdelber)

---

Top Contributors

[![nabilabdelber](https://avatars.githubusercontent.com/u/68895992?v=4)](https://github.com/nabilabdelber "nabilabdelber (7 commits)")

### Embed Badge

![Health badge](/badges/nbwphooks-wphooks/health.svg)

```
[![Health](https://phpackages.com/badges/nbwphooks-wphooks/health.svg)](https://phpackages.com/packages/nbwphooks-wphooks)
```

###  Alternatives

[phpjuice/slopeone

PHP implementation of the Weighted Slope One rating-based collaborative filtering scheme.

868.7k](/packages/phpjuice-slopeone)[thisisbd/silverstripe-fontawesome-iconpickerfield

Font Awesome Icon Picker for SilverStripe 3.1.x based on http://mjolnic.com/fontawesome-iconpicker

143.3k1](/packages/thisisbd-silverstripe-fontawesome-iconpickerfield)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
