{"id":15292251,"url":"https://github.com/anthonybudd/wp_ajax","last_synced_at":"2025-10-07T09:50:51.141Z","repository":{"id":56948812,"uuid":"82858259","full_name":"anthonybudd/WP_AJAX","owner":"anthonybudd","description":"A simple class for creating concise WordPress AJAX actions","archived":false,"fork":false,"pushed_at":"2018-05-19T17:50:39.000Z","size":58,"stargazers_count":46,"open_issues_count":1,"forks_count":17,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-09-23T09:02:01.254Z","etag":null,"topics":["ajax","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/anthonybudd.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-22T22:20:05.000Z","updated_at":"2025-08-27T12:11:45.000Z","dependencies_parsed_at":"2022-08-21T08:20:21.482Z","dependency_job_id":null,"html_url":"https://github.com/anthonybudd/WP_AJAX","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/anthonybudd/WP_AJAX","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonybudd%2FWP_AJAX","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonybudd%2FWP_AJAX/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonybudd%2FWP_AJAX/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonybudd%2FWP_AJAX/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anthonybudd","download_url":"https://codeload.github.com/anthonybudd/WP_AJAX/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anthonybudd%2FWP_AJAX/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278755147,"owners_count":26040034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ajax","wordpress"],"created_at":"2024-09-30T16:17:05.421Z","updated_at":"2025-10-07T09:50:51.126Z","avatar_url":"https://github.com/anthonybudd.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WP_AJAX\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://ideea.co.uk/static/wp_ajax.png\"\u003e\u003c/p\u003e\n\n### Simple Controllers for WordPress\nWP_AJAX is a class for interfacing with WordPress’s built in AJAX system. This class is designed to fully abstract the developer from the traditional hook based system to a clean controller style structure. Simply define the class, echo urls with url($params = []) method and write what code you want to execute in the run() method.\n\n\n## Introduction: **[Medium Post](https://medium.com/@AnthonyBudd/wp-ajax-97d8f1d83e26)**\n\n```php\nClass ExampleAction extends WP_AJAX{\n\n    protected $action = 'example-action';\n\n    protected function run(){\n\n    \t// Your Code Here!\n    \t\n    \tupdate_option('name', $this-\u003eget('name'));\n\n    }\n}\nExampleAction::listen();\n\nExampleAction::url() // http://example.com/wp-admin/admin-ajax.php?action=example-action\nExampleAction::url(['name' =\u003e 'Anthony Budd']) // http://example.com/wp-admin/admin-ajax.php?action=example-action\u0026name=Anthony%20Budd\n\n```\nExampleAction.php\n\n### Font-end\n\n```html\n\u003ca href=\"\u003c?= ExampleAction::url(['name' =\u003e 'Anthony Budd']) ?\u003e\" \u003eThis is a link\u003c/a\u003e\n```\nPage-Template.php\n\n#### Or\n\n```JS\n$('.submit-btn').click(function(){\n\n    $.post('http://example.com/wp-admin/admin-ajax.php',{\n        action: 'example-action',\n        name: $('.name-field').val(),\n    },function(data){\n        console.log(data)\n    }, 'JSON');\n    \n});\n```\nscript.js\n\n# Installation\n\nRequire WP_AJAX with composer\n\n```\n$ composer require anthonybudd/wp_ajax\n```\n\n#### Or\n\nDownload the WP_AJAX class and require it at the top of your functions.php file.\n\n```php\n    require 'src/WP_AJAX.php';\n```\n\n***\n\n# Setup\nYou 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)).\n```php\nClass Example extends WP_AJAX\n{\n    protected $action = 'example';\n\n    protected function run(){\n        echo \"Success!\";\n    }\n}\n```\n\n***\n\n# Listen\nNext 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.\n```php\nExampleAJAX::listen();\n```\n\nIf you would like to only allow signed in users to access your AJAX endpoint add the argument FALSE to the listen method.\n```php\nExampleAJAX::listen(FALSE);\n```\n\n***\n\n### JSON Response\nIf 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.\n\nI 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.\n\n```php\nClass ExampleAJAX extends WP_AJAX{\n    ..\n\n    protected function run(){\n        $post5 = get_post(5);\n\n        $this-\u003eJSONResponse($post5);\n    }\n}\n\n```\n\n***\n\n### Helper Methods\n\n```php\nExample::url() // Returns the url of the ajax endpoint. Example http://ajax.local/wp/wp-admin/admin-ajax.php?action=example\n\n$this-\u003eisLoggedIn(); // Returns TRUE or FALSE if the current visitor is a logged in user.\n\n$this-\u003ehas($key); // has() will return TRUE or FALSE if an element exists in the $_REQUEST array with a key of $key\n\n$this-\u003eget($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.\n\n$this-\u003erequestType(); // Returns 'PUT', 'POST', 'GET', 'DELETE' depending on HTTP request type\n\n$this-\u003erequestType('POST'); // Returns (bool) \n\n$this-\u003erequestType(['POST', 'PUT']); // Returns (bool)  \n```\n\n\n# Example\n```php\nClass CreatePost extends WP_AJAX\n{\n    protected $action = 'create_post';\n\n    protected function run(){\n        if($this-\u003eisLoggedIn()){\n            $post = [\n                'post_status' =\u003e 'publish'\n            ];\n            \n            if( $this-\u003erequestType(['POST', 'put']) ){\n                $post['post_content'] = 'This request was either POST or PUT';\n            }else if( $this-\u003erequestType('get') ){\n                $post['post_content'] = 'This request was GET';\n            }\n\n            $post['post_title'] = sprintf('This post was created by %s', $this-\u003euser-\u003edata-\u003euser_nicename);\n            \n            wp_insert_post($post);\n\n            $this-\u003eJSONResponse($post);\n        }\n    }\n}\n\nCreatePost::listen();\n\n// http://example.com/wp-admin/admin-ajax.php?action=create_post\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonybudd%2Fwp_ajax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanthonybudd%2Fwp_ajax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanthonybudd%2Fwp_ajax/lists"}