Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/YahnisElsts/ajax-wrapper
AJAX action wrapper for WordPress
https://github.com/YahnisElsts/ajax-wrapper
wordpress
Last synced: about 1 month ago
JSON representation
AJAX action wrapper for WordPress
- Host: GitHub
- URL: https://github.com/YahnisElsts/ajax-wrapper
- Owner: YahnisElsts
- Created: 2017-01-19T19:05:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-01-11T14:53:48.000Z (12 months ago)
- Last Synced: 2024-05-09T23:56:15.798Z (8 months ago)
- Topics: wordpress
- Language: PHP
- Size: 12.7 KB
- Stars: 21
- Watchers: 2
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-wordpress - AJAX Action Wrapper
- awesome-wp-developer-tools - AJAX Action Wrapper - This helper library makes it easier to handle AJAX requests in WordPress plugins. (WordPress Plugins)
README
# AJAX Action Wrapper
This helper library makes it easier to handle AJAX requests in WordPress plugins. Mainly for personal use.
### Example
Define action:
```php
$exampleAction = ajaw_v1_CreateAction('ws_do_something')
->handler(array($this, 'myAjaxCallback'))
->requiredCap('manage_options')
->method('post')
->requiredParam('foo')
->optionalParam('bar', 'default value')
->register();
```Call from JavaScript:
```javascript
AjawV1.getAction('ws_do_something').post(
{
'foo': '...'
},
function(response) {
console.log(response);
}
);
```### Features
- Automate common, boring stuff.
- [x] Automatically pass the `admin-ajax.php` URL and nonce to JS.
- [x] Define required parameters.
```php
$builder->requiredParam('foo', 'int')
```
- [x] Define optional parameters with default values.
```php
$builder->optionalParam('meaningOfLife', 42, 'int')
```
- [x] Automatically remove "magic quotes" that WordPress adds to `$_GET`, `$_POST` and `$_REQUEST`.
- [x] Encode return values as JSON.
- Security should be the default.
- [x] Generate and verify nonces. Nonce verification is on by default, but can be disabled.
```php
$builder->withoutNonce()
```
- [x] Check capabilities.
```php
$builder->requiredCap('manage_options');
```
- [x] Verify that all required parameters are set.
- [x] Validate parameter values.
```php
$builder->optionalParam('things', 1, 'int', function($value) {
if ($value > 10) {
return new WP_Error(
'excessive_things',
'Too many things!',
400 //HTTP status code.
);
}
})
```
- [x] Set the required HTTP method.
```php
$builder->method('post')
```
- Resilience.
- [ ] Lenient response parsing to work around bugs in other plugins. For example, deal with extraneous whitespace and PHP notices in AJAX responses.
- [x] Multiple versions of the library can coexist on the same site.### Why not use the REST API instead?
Backwards compatibility. In theory, this library should be compatible with WP 4.1+.