PHPackages                             mrbobbybryant/wp-api-endpoints - 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. [API Development](/categories/api)
4. /
5. mrbobbybryant/wp-api-endpoints

ActiveLibrary[API Development](/categories/api)

mrbobbybryant/wp-api-endpoints
==============================

Quickly create custom endpoints in WordPress

212PHP

Since Dec 29Pushed 10y ago1 watchersCompare

[ Source](https://github.com/mrbobbybryant/wp-api-endpoints)[ Packagist](https://packagist.org/packages/mrbobbybryant/wp-api-endpoints)[ RSS](/packages/mrbobbybryant-wp-api-endpoints/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

wp-api-endpoints
================

[](#wp-api-endpoints)

This library consists of two main classes. An abstract class which can be used as a base for setting up ajax endpoints in WordPress. It utilizes the rewrite endpoints instead of admin-ajax requests. The second class provides an api layout above the Base Class. This class makes it easy to create and remove endpoints. It also makes it easy create localized data for use with your newly created Endpoints.

There are a number of benefits to this approach.

1. A unique url that is server cachable for high traffic, reader facing endpoints such as infinite scroll.
2. Allows organization of api endpoints into a use specific class with a unique endpoint. For better code organization.
3. Makes API building and testing easier because you can request the url directly in the browser.

### Create an Endpoint.

[](#create-an-endpoint)

To get started lets look at a very basic example.

```
use WP_API_ENDPOINTS\ENDPOINT_HANDLER as Handler;

function get_author_list() {
$authors = get_users('role=author');
wp_send_json_success( json_encode( $authors ) );
}

$ajax_handler = new Handler\Ajax_Handler();
$ajax_handler->add_admin_endpoint( 'get_author_list' );

```

In the above example, we have created our first endpoint.

- Define the callback function which will handle any requests to the endpoint.
- Initialize the Endpoint API Class.
- Leveraged a method in the Endpoint API class called `add_admin_endpoint()`

Thats it. Now if you navigate to yoursite.com/ajax/get\_author\_list, you will see a JSON feed of all users who have the role of author assigned. Another thing to point out is that this endpoint is an Admin Endponit, so only logged in users are able to hit it.

### Localization

[](#localization)

Another common requirement by most WordPress Endpoints/AJAX processes is localization. In WordPress this is handled by a function call `wp_localize_script()`.

And at a bare minimum, you will generally need at least two pieces of data.

- The endpoint URL for your Ajax request.
- A NONCE to confirm any incoming Ajax requests are save.

The Endpoint API classes provides a useful interface for creating those localizations. By default it will include the endpoint URL and a NONCE. Lets look at an example of this.

```
use WP_API_ENDPOINTS\ENDPOINT_HANDLER as Handler;
$ajax_handler = new Handler\Ajax_Handler();

$ajax_handler->add_admin_localization(
	'my_admin_endpoint',
	'ajax_handler_js',
	'ajaxAdminHandler',
	$localized_data
);

```

The API Method `add_admin_localization` accepts have paramenters:

- `endpoint (string) ` **required** - The related Endpoint for this localized data.
- `Javascript Handler (string) ` **required** - Just like `wp_localized_script` we need to associate this data to a specific javascript file/handle.
- `Unique Handle (string)` **required** - This will be used to identify the localized data. In our example the unique name for this data will be `ajaxAdminHandle`. Once registered, if you type that into a browser console, you will see the data listed.
- `Data (array)` **optional** This is the array of data you would like to pass via this localization. The next example will show this in more detail. This param is optionl. By default it is an empty array.

### Passing Additional Data

[](#passing-additional-data)

Let's look at a more useful example of localizing data.

```
$data = array(
    'siteURL'   => esc_url( site_url() ),
    'siteTitle' => esc_html( bloginfo( 'name' ) ),
    'myCustomQuery' => esc_html( my_custom_query() )
);

$ajax_handler = new Handler\Ajax_Handler();

$ajax_handler->add_admin_localization(
	'my_admin_endpoint',
	'ajax_handler_js',
	'ajaxAdminHandler',
	$data
);

```

Here you can see another example of using `add_admin_endpoint`. This time I actually spelled out an example of using localization to pass data inside of WordPress for use by our Javascript file `ajax_handler_js`.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8323ec7131dbb68e4fd8afc49af920a501a932ad401cc2c08911241d39ef1dfc?d=identicon)[mrbobbybryant](/maintainers/mrbobbybryant)

---

Top Contributors

[![joeyblake](https://avatars.githubusercontent.com/u/182726?v=4)](https://github.com/joeyblake "joeyblake (2 commits)")

### Embed Badge

![Health badge](/badges/mrbobbybryant-wp-api-endpoints/health.svg)

```
[![Health](https://phpackages.com/badges/mrbobbybryant-wp-api-endpoints/health.svg)](https://phpackages.com/packages/mrbobbybryant-wp-api-endpoints)
```

###  Alternatives

[exsyst/swagger

A php library to manipulate Swagger specifications

35916.4M7](/packages/exsyst-swagger)[hubspot/api-client

Hubspot API client

24016.2M18](/packages/hubspot-api-client)[pocketmine/bedrock-protocol

An implementation of the Minecraft: Bedrock Edition protocol in PHP

172437.8k11](/packages/pocketmine-bedrock-protocol)[botman/driver-telegram

Telegram driver for BotMan

93459.5k6](/packages/botman-driver-telegram)

PHPackages © 2026

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