{"id":18289933,"url":"https://github.com/mehrshaddarzi/wp-rewrite-api","last_synced_at":"2025-04-09T07:25:12.804Z","repository":{"id":85428063,"uuid":"287682875","full_name":"mehrshaddarzi/wp-rewrite-api","owner":"mehrshaddarzi","description":"Use Rewrite API Method for Ajax Request in WordPress","archived":false,"fork":false,"pushed_at":"2024-12-29T15:44:13.000Z","size":98,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-15T01:41:41.610Z","etag":null,"topics":["wordpress","wordpress-api","wordpress-development","wordpress-php-library","wordpress-plugin"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mehrshaddarzi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-15T05:19:27.000Z","updated_at":"2024-12-29T15:44:16.000Z","dependencies_parsed_at":"2023-03-05T02:00:36.477Z","dependency_job_id":null,"html_url":"https://github.com/mehrshaddarzi/wp-rewrite-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mehrshaddarzi%2Fwp-rewrite-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mehrshaddarzi%2Fwp-rewrite-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mehrshaddarzi%2Fwp-rewrite-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mehrshaddarzi%2Fwp-rewrite-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mehrshaddarzi","download_url":"https://codeload.github.com/mehrshaddarzi/wp-rewrite-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247995996,"owners_count":21030398,"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","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":["wordpress","wordpress-api","wordpress-development","wordpress-php-library","wordpress-plugin"],"created_at":"2024-11-05T14:08:51.762Z","updated_at":"2025-04-09T07:25:12.783Z","avatar_url":"https://github.com/mehrshaddarzi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WordPress Rewrite API Ajax\nUse Rewrite API Method for Ajax Request in WordPress.\n\nRewrite API in WordPress is Very Faster than `admin-ajax.php`.\n\n### How to Install and Use\nActivate this plugin, then Flush Rewrite in WordPress.\n\n### How to Create New Route\n\nuse `WordPress_Rewrite_API_Request` namespace in your class.\n\n\n```php\nnamespace WordPress_Rewrite_API_Request;\n\nclass post\n{\n\n    /**\n     * @request_url =\u003e http://site.com/api/post/get\n     */\n    public static function get()\n    {\n        wp_send_json_success(array(\n            'data' =\u003e get_post($_REQUEST['post_id'], ARRAY_A)\n        ), 200);\n    }\n\n}\n```\n\nAnd use rewrite_api variable in js:\n\n```js\n$.ajax({\n    url: rewrite_api.url + '/' + rewrite_api.prefix + '/post/get',\n    type: 'GET'\n});\n```\n\n### Create Flexible Methods\n\nWe want to create a function to add to cart in WooCommerce:\n\n##### 1) Create PHP Class\n```php\nnamespace WordPress_Rewrite_API_Request;\n\nclass wc_cart\n{\n    \n    public function __construct()\n    {\n        add_action('wp_enqueue_scripts', array($this, '_register_js_script'), 7);\n    }\n\n    // Add Js Script\n    // Helper Function start with _\n    public function _register_js_script()\n    {\n        wp_enqueue_script('woocommerce-cart-rewrite', '../wc-cart.js' , array('jquery', 'wp-rewrite-api'), '1.0.0', true);\n    }\n\n    /**\n     * @request_url =\u003e http://site.com/api/wc_cart/add\n     */\n    public static function add()\n    {\n        // Check Isset Params\n        if(!isset($_REQUEST['product_id'])) {\n            WordPress_Rewrite_API_Request::empty_param('product_id');\n        }\n           \n        // Add To Product\n        $cart_hash_item = WC()-\u003ecart-\u003eadd_to_cart(sanitize_text_field($_REQUEST['product_id']), 1);\n\n        // Response\n        wp_send_json_success(array(\n            'cart_key' =\u003e $cart_hash_item\n        ), 200);\n    }\n}\n```\n\n##### 2) Create Js File (wc-cart.js)\n\n```js\njQuery(document).ready(function ($) {\n\n    let woocommerce_cart_methods = {\n        wc_add_to_cart: function ($tag = false, $product_id = 0) {\n\n            // Sanitize Params\n            if ($tag !== false) {\n                $product_id = $tag.attr('data-product-id');\n            }\n            window.rewrite_api_method.request('wc_cart/add', 'GET', {\n                'product_id': $product_id\n            }, $tag);\n        }\n    };\n\n    // Push To global Rewrite API Js\n    if (typeof window.rewrite_api_method !== 'undefined') {\n        $.extend(window.rewrite_api_method, woocommerce_cart_methods);\n    }\n});\n```\n\n##### 3) Use in Every Project according to FrontEnd\n\n```html\n\u003ca href=\"#\" data-function=\"wc_add_to_cart\" data-product-id=\"123\"\u003eAdd to Cart\u003c/a\u003e\n```\n\n\n```js\njQuery(document).ready(function ($) {\n    \n    // Get All Methods\n    const _methods = window.rewrite_api_method;\n    const add_action = window.rewrite_api_method['add_action'];\n\n    /**\n     * Add Action For Add to Cart\n     */\n    add_action('wc_cart/add',\n        function (data, event) {\n            alert('before-send');\n        }, function (data, event) {\n            alert('success');\n        }, function (data, event) {\n            alert(_methods.to_json(data));\n    });\n});\n```\n\n\n### How to Change Prefix `api` Url\n\nuse `add_filter('rewrite_api_request_prefix_url', 'api');` in WordPress, then flush rewrite;\n\n\n### How to Get My Custom Url in Template function\n\n```php\necho get_rewrite_api_url($class = 'user', $method = 'login'); // http://site.com/rewrite-api/user/login\n```\n\n### How To Disable Load Custom UI Component\n\n```php\nadd_filter('rewrite_api_request_ui_component', '__return_false');\n```\n\nFor Disable Custom Ui Component, see `ui-component.php` and use every filter. for example:\n\n```php\nadd_filter('rewrite_api_request_ui_component_jquery_confirm', '__return_false');\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmehrshaddarzi%2Fwp-rewrite-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmehrshaddarzi%2Fwp-rewrite-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmehrshaddarzi%2Fwp-rewrite-api/lists"}