Can't retrieve body params from PUT endpoint using formdata

I have one endpoint to create players, which is a PUT request to a certain route. While I was sending parameters via JSON, it was working fine, but now I need to upload a picture with data, so I switched to formdata format. This is the function running in the endpoint: public function put(WP_REST_Request $request) { ['foto' => $file] = $request->get_file_params(); if (!empty($file)) { if (!function_exists('media_handle_upload')) { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); } $attachmentId = media_handle_upload('foto', …
Category: Web

WordPress Rest API - Get all posts based on post_meta on custom endpoint

So I can't seem to figure out how to get ALL posts based on a specific post meta, so I wanted to reach out and see if someone might be able to assist. What I am trying to achieve: I have website A, which has posts that I can access using /wp-json/wp/v2/posts which outputs ALL posts on the website. Then I have website B which I want to access website A posts, but I want to pull in only specific …
Category: Web

WP_REST_Response - How to return Gzip answer and add Content-encoding header?

This code runs correctly : function get_travelbooks($request) { global $tsp_mdlObj; $result = $tsp_mdlObj->read_travelbooks(); return new WP_REST_Response($result, 200); } Now I would like send a gzip answer. So I try : function get_travelbooks($request) { global $tsp_mdlObj; $result = $tsp_mdlObj->read_travelbooks(); $result = gzencode(json_encode($result), 9); $response = new WP_REST_Response($result, 200); $response->header('Content-Encoding', 'gzip'); return $response; } But this code doesn't work. The content of $result before gzencode(json_encode is an array : Array ( [0] => stdClass Object ( [id] => 5 [name] => travel …
Category: Web

Need to forward Data from WooCommerce Webhook sent to same site Wordpress REST API custom endpoint

I created a Wordpress REST API custom endpoint to be used as a Webhook URL in WooCommerce in order to convert the received data and then send it to a third party system, but the endpoint apparently is not receiving any data. I tested the code by sending some JSON data to my custom endpoint using Postman and it works only after installing another P;ugin to enable Basic Auth. I wonder if the problem is because probably the webhook needs …
Category: Web

Get The latest post from a category

I am using below code to get the latest post from a category www.mysite.com/wp-json/mynamespace/v1/latest-posts?category=it but an error is showing <?php /** * * Get The latest post from a category ! * @param array $params Options for the function. * @return string|null Post title for the latest,? * or null if none * */ function get_latest_post ( $params ){ $post = get_posts( array( 'category' => $category, 'posts_per_page' => 1, 'offset' => 0 ) ); if( empty( $post ) ){ return …
Category: Web

How can I get users email (and additional data) from the rest API?

How can I get the email adress from the users by using the REST API? I'm authenticating with nonce, and it seems to be working since I can do POST requests and change stuff. Do I have to add something to make it return all the user info? This is my JS: (function($) { var nonce = WPsettings.nonce; var rest_url = WPsettings.rest_url; $.ajax( { url: rest_url + 'users/', dataType: "json", beforeSend: function ( xhr ) { xhr.setRequestHeader( 'X-WP-Nonce', nonce ); …
Category: Web

WP Remote Request not returning all of content via request

EDIT: I think I discovered the issue, the endpoint is truncating the content response, does anyone know how to send over all of the data without it being truncated? So I stumbled across some inconsistencies when requesting posts from one of our websites and not being able to pull in ALL of the content for a specific post. Explanation: Let's say that we have our REST API endpoint from the website where the posts live: https://test.com/wp-json/wp/v2/posts. When I decode and …
Category: Web

Custom WP rest api endpoint only working on non https?

I'm googling like an hour for a possible solution while im trying to create my first custom wordpress rest api endpoint. If i call the endpoint by postman it just works, but if i try with jquery, it doesn't. I noticed, that if i call it by the http version it works, but on https it just returns 404? Heres my endpoint registration: add_action('rest_api_init', function () { register_rest_route('the-namespace/v1', '/the-endpoint', [ 'method' => WP_REST_Server::CREATABLE, 'callback' => 'theEndpoint', 'permission_callback' => function () …
Category: Web

Rest API Paginate until all posts are imported

So I wanted to see if someone might be able to help me with this, so below I will explain what I've done: I am using wp_remote_get to grab the posts from another website with the arguments page and per_page. Next, I am checking to see if the post has already been imported using $current_post_id = post_exists($post->title->rendered); and if it does, don't call the wp_insert_post function. Here is what I have working already: If I call the function, it will …
Category: Web

Create multiple rewrite endpoints for custom taxonomy archive

I am trying to create custom end point for the custom taxonomy - 'species' I am trying to use urls like https://example.com/dogs/shop/ https://example.com/cats/shop/ https://example.com/dogs/services/ https://example.com/cats/services/ https://example.com/dogs/sales https://example.com/cats/sales where dogs, cats is taxonomy term Following is the code function my_register_my_taxes_species() { add_rewrite_endpoint( 'shop', EP_PAGES ); add_rewrite_endpoint( 'services', EP_PAGES ); add_rewrite_endpoint( 'sales', EP_PAGES ); add_filter('request', function($vars) { if (isset($vars['shop'])) { $vars['shop'] = true; } if (isset($vars['services'])) { $vars['services'] = true; } if (isset($vars['sales'])) { $vars['sales'] = true; } return $vars; }); add_filter('template_include', …
Category: Web

Endpoint Callback Display for Custom Plugin

I am building a plugin which allows admins to add staff members to a database. I have created a function in my plugin class file... cp_libstaff_frontend_display() ...which displays a table of every staff member in the database to front-end users. This function is attached to shortcode which displays at the URL: mysite.com/librarywp/staff-directory Now what I would like to do is have a link on each staff member's name which takes the user to a page for just that staff member's …
Category: Web

Custom Endpoint - Does it possible to use PUT method with WP API Rest?

If I use the PUT method inside register_rest_route like this : add_action('rest_api_init', function () { register_rest_route( 'mydomain/v1', 'item/(?P<id>\d+)',array( 'methods' => 'PUT', 'callback' => 'update_item' )); }); How can I get the parameters inside the callback With the POST I get these params with : function update_item($request) { $params = $request->get_params(); .... But This doesn't work with the PUT method. Thanks for your advices.
Category: Web

Custom endpoint filtering post by custom taxonomies

I am trying to create a custom endpoint by custom taxonomies, I have already created another for the categories and it works well, however there is something I cannot solve with this function get_latest_posts_by_term($request) { $args = array( 'term' => $request['term_id'] ); $posts = get_posts($args); if (empty($posts)) { return new WP_Error( 'empty_term', 'There are no posts to display', array('status' => 404) ); } $response = new WP_REST_Response($posts); $response->set_status(200); $data = []; $i = 0; foreach($posts as $post) { $data[$i]['id'] = …
Category: Web

How to use endpoint, but remove/rewrite endpoint base?

I've been messing around with rewrites and query_var to generate some listings, for example: ~/my-page/?my_var=1 ~/my-page/?my_var=2 ~/my-page/?my_var=3 After perusing StackExchange and the interwebs a bit, it seems that the overwhelming consensus is that add_rewrite_endpoint is the way to go. As such, I've implemented the following: function wpd_add_my_endpoint(){ add_rewrite_endpoint( 'my_var', EP_PAGES ); } add_action( 'init', 'wpd_add_my_endpoint' ); This yields the following URIs based on the ones I mentioned previously: ~/my-page/my_var/1 ~/my-page/my_var/2 ~/my-page/my_var/3 However, I would like to strip out the 'endpoint …
Category: Web

How to send multipart form data to WordPress endpoint

I built custom React/Gatsby multipart form with file upload and I want to send all form data to WordPress endpoint, upload files to specific folder and display all of the data in the WordPress backend. This is how I send data: const { stepOne, stepTwo, } = useContext(FormStepProvider) const steps = { stepOne: stepOne, stepTwo: stepTwo, } const postData = async () => { const formData = new FormData() formData.append("stepOne", JSON.stringify(stepOne)) formData.append("stepTwo", stepTwo.file) await fetch("https://panel.domain.com/wp-json/test/v1/forms", { method: "POST", headers: { …
Category: Web

Is it possible to create dynamic endpoint URLs?

I want to create an archive endpoint page that will have a dynamic slug, based on the taxonomy term slug. e.g. If the taxonomy is Food and the current term is Onion, I want the endpoint slug to look like onion-recipes. www.domain.com/food/vegetables/onion/onion-recipes Gives me a 404 even after flushing permalinks. Code: add_action( 'init', function() { $term = get_queried_object(); $ep_slug = $term->slug.'-recipes'; add_rewrite_endpoint( $ep_slug, EP_PAGES ); } );
Category: Web

Add Rewrite Endpoint to CPT Archive

I have a Custom Post Type with a hierarchical taxonomy archive and want to add an endpoint page called "infopage" to each term in the archive. e.g. domain.com/tax-term-1/infopage or domain.com/tax-term-2/child-term-3/child-term-4/infopage etc. I don't want to add this as a child term, but rather want it to be a "virtual" page as the contents will be generated dynamically. Tried something like this but was getting a 404 when accessing the endpoint page (yes, I flushed permalinks): add_action( 'init', function() { add_rewrite_endpoint( …
Category: Web

Multiple URL rewrite rules and get every prams

I am creating a messaging system where I have 4 sections (1)inbox (2)sent (3)draft (4)unread so I decide to go with WordPress add rewrite rules I have already added one rewrite rule section. add_rewrite_rule( 'messages/([^/]*)/?$', 'index.php?pagename=messages&section=$matches[1]', 'top' ); Now I want to add one more param after the section is the ID(inbox messages id). so for that, I add a second rewrite rule id. add_rewrite_rule( 'messages/inbox/([^/]*)/?$', 'index.php?pagename=messages&id=$matches[1]&section=$matches[2]', 'top' ); if I visit https://example.com/messages/inbox I can get in query_vars [section] => …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.