Getting featured image with PHP and not javascript from wordpress api _embed

Not having access to the functions file, I can't add the function to add the featured image to the API so I need to use wp-json/wp/v2/posts?_embed. With javascript I believe it is ._embedded['wp:featuredmedia']['0'].source_url so I tried with php after using wp_remote_get, $post->_embedded['wp:featuredmedia'][0]->source_url but the error I am getting is : Cannot use object of type stdClass as array
Category: Web

WP API post__not_in is not working

I'm using WP API to get posts in my application and I'm trying to exclude some posts from query using filter[post__not_in]. after removing filter in WP 4.7, I'm using WP REST API filter parameter plugin to get it back. and it works for all parameters but for post__not_in it's not effecting at all, I still get all posts. I saw some closed issues about this on WP API repo on github, they said that post__not_in are not allowed for unauthenticated …
Category: Web

Custom endpoint and X-WP-TotalPages (headers)

I have a custom endpoint defined like so: add_action( 'rest_api_init', function () { register_rest_route( 'menc/v1', '/crosscat/(?P<dept>[\w-]+)/(?P<cat>[\w-]+)', array( 'methods' => 'GET', 'callback' => 'dept_cat_api', 'args' => array( 'dept' => array( 'sanitize_callback' => function($param, $request, $key) { return sanitize_text_field( $param ); } ), 'cat' => array( 'sanitize_callback' => function($param, $request, $key) { return sanitize_text_field( $param ); } ), 'per_page' => array( 'default' => 10, 'sanitize_callback' => 'absint', ), 'page' => array( 'default' => 1, 'sanitize_callback' => 'absint', ), 'orderby' => array( 'default' …
Category: Web

Cannot use WordPress Application Passwords: "code": "rest_no_route" "status":404 for /wp-json/wp/v2/users/me/application-passwords

I am trying to use Application Passwords on Wordpress 5.9 for the development of a client app. The page https://www.example.com/wp-admin/authorize-application.php shows the Application Passwords confirmation prompt, but if I click generate "Approve the connection" for the app, an error is shown: "No Path provides a match between the URL and the requested method." I also tried to GET the path "/wp-json/wp/v2/users/me/application-passwords?_locale=user" that is supposed to answer the request, but it says 404 error. The base path for wp api works …
Category: Web

Display post title from WordPress excluding a string via API

Imagine you have custom post types - Users. There are fields for each user as the post title, description, image, and rules. All titles are names of the Users and all they have Mr. before the names. What is the correct way and how to display all these fields via API in another site(not WordPress) but for the titles to exclude/strip the string Mr.?
Category: Web

WP REST API Access-Control-Allow-Origin response is cached

I have an AngularJS site that is pulling in posts from WordPress via the plugin "WP REST API (WP API)". I am testing a site on both dev and staging subdomains, and the WP API works on both. However, when I visit one domain, and then switch over to another domain immediately afterward, then the dynamically-generated Access-Control-Allow-Origin is "stuck" on the previous domain to make the API calls. It's not until I refresh the page, that the response header updates …
Category: Web

No 'Access-Control-Allow-Origin' when call rest API

I am using ReactJs with WordPress. WordPress running on localhost 8888 port and React app running on 3000 port. When I change the WordPress site URL (from settings->general) to localhost:300 then I am getting the following error, Access to XMLHttpRequest at 'http://localhost:8888/bolb/wp-json/wp/v2/categories' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. It's not working on the postman too. But, it's okay if I use the same URL (http://localhost:8888/bolb) as the site …
Category: Web

Woocommerce REST API not considering discounts and coupons

I am trying to create an order on my WooCommerce store from my mobile application using the REST API v2. The order is created successfully but I can't find any solution to the following issues. I send a coupon_lines array within the order object and when the order is viewed on the admin panel, it shows the coupon there but the order total is not affected at all. Then I decided to calculate the total and discounts based on the …
Category: Web

Wordpress REST API - get custom taxonomy category posts

I want to use Wordpress REST API to get the posts that are inside a custom taxonomy created by the theme. The taxonomy is called "my_portfolio" but if I use this I have this response: {"code":"rest_no_route","message":"Nessun percorso fornisce una corrispondenza tra l'URL ed il metodo richiesto.","data":{"status":404}} Instead if I use "my_portfolio_category" I have 4 categories that are called: "photo", "video", "design", "web". I want to retrieve the posts inside the category "photos" but the only thing I managed to get …
Category: Web

403 error rest_'cookie_invalid_nonce' on API Request

I'm currently developing a Plugin in which I'm in need of retrieving information from a third-party plugin API. To authenticate my users, I am trying to use the vanilla authentication via cookie as explained in the official documentation, but every request is refused with a 403 rest_cookie_invalid_nonce code. I cannot understand what I'm doing wrong. My code: function makeRequest(){ $url = 'https://xxxxxx.com.br/wp-json/wcfmmp/v1/orders/'; $nonce = wp_create_nonce('wp_rest'); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'X-WP-Nonce:'. $nonce, 'Content-Type: application/json', )); $result …
Category: Web

WP API: Get posts excluding those with a tag or category name

I know from this post that I can get posts by tag name by just specifying a filter[tag] query parameter. And similarly with filter[category_name]. Is there a way to get posts that are not in a list of tags or categories for the purpose of excluding posts? I would prefer to do it with the name, or else I would have to make another request just to get the ID of the category, which would just slow down my overall …
Category: Web

Allow REST API Endpoint to specific user and hide from public

So I've been working on trying to figure this out, but I can't seem to get the right answer or even find docs online regarding this issue, so I'm reaching out to the community to see if I can get some assistance. So I have the following RestRoutes class: class RestRoutes { private string $namespace; private string $resource; public function __construct() { $this->namespace = 'twitter/v1'; $this->resource = '/posts/'; $this->init(); } public function init(): void { add_action('rest_api_init', [$this, 'register_rest_route']); } public …
Category: Web

Create a custom post with custom post-metadata with WP API

I'm using WP API to insert a custom post in Wordpress. The custom post is "property". What I now need to do is assign that post a "metadata" (DB table "wp_postmeta") At first I thought It was going to be easy like (I'm using Symfony Http class here): Http::withBasicAuth($user, $password)->post("{$endpoint}/property", [ 'date' => now(), 'status' => 'publish', 'title' => 'Test', //etc... 'meta' => [ 'custom_metadata' => 1234 ] ]); The custom property post gets created but the meta does not. …
Category: Web

Combining results from WP-API using AngularJS

I currently have this site - http://dev.5874.co.uk/scd-data/ where I have a dropdown which displays results from WP-API which I am pulling in through AngularJS. It currently combines the two sets of results as they're separate URL's, the results are in categories within a custom post type so if both posts are 'tagged' in the same category chosen they display twice. I need a way to combine the two sets of results but only showing one of the posts - I …
Category: Web

WP-API + JS Backbone client - how to update post meta

Using the WP API and Backbone-based JS client, I'm trying to update a post's metadata as follows: var parentId = 96; // the post id var metaData = new wp.api.collections.PostMeta('', {parent: parentId}); metaData.fetch() .done(function(data) { var someKey = data.findWhere({key: 'someKey'}); someKey.set('value', 'newValue'); someKey.save(); }); Everything works fine until the calling of save(), which throws the following error: PUT http://example.org/wp-json/wp/v2/posts/undefined/meta/1901 404 (Not Found) Trying to sync the collection also fails. Any thoughts? Thanks!
Category: Web

WP API to fetch all records for /posts, /media, /categories

I was using the below API to fetch all posts http://example.com/wp-json/wp/v2/posts/?filter[posts_per_page]=-1 Since the last two days, all of a sudden it stopped working. For example, the first API used to give me 37 records with 10 records per page, but now it only gives me 10 records instead of 37. It does not return records for rest of the pages. I checked the WordPress site from where I am trying to fetch it, it still has 4 pages and 37 …
Category: Web

How can I create new CustomPostType record using wp.api.collections?

I would be very grateful for your help. Already racked my brain for today! So, I used the CPT UI plugin and created my custom post type with it. Immediately when setting it up, I enabled RestAPI support (I always do this), and I also used the default WP_REST_Posts_Controller setting. When I give the command to add the default Post var review = new wp.api.models.Post( { title: 'Test post' } ); review.save(); everything is fine! But when I try to …
Category: Web

No featured media in API request

when I make a request to http://localhost/wordpress/wp-json/wp/v2/posts/?_embed&categories=${categoryId}&per_page=${state.perPage} I get the following result [ { id: 3, date: '2019-04-20T16:10:00', date_gmt: '2019-04-20T16:10:00', guid: { rendered: 'http://localhost/wordpress/?p=3' }, modified: '2019-04-20T16:36:30', modified_gmt: '2019-04-20T16:36:30', slug: 'slug-title', status: 'publish', type: 'post', link: 'http://localhost/wordpress/2019/04/20/slug-title/', title: { rendered: 'title' }, content: { rendered: '\n<figure class="wp-block-image"><img ' + 'src="http://localhost/wordpress/wp-content/uploads/2019/04/sirius.jpg" ' + 'alt="" class="wp-image-4" ' + 'srcset="http://localhost/wordpress/wp-content/uploads/2019/04/sirius.jpg ' + '370w, ' + 'http://localhost/wordpress/wp-content/uploads/2019/04/sirius-217x300.jpg ' + '217w" sizes="(max-width: 370px) 100vw, 370px" ' + '/></figure>\n', protected: false }, excerpt: { rendered: '', …
Category: Web

Create a range slider to change font-size with different screens sizes in wordpress customizer api

I have been trying to create a theme customizer to change the font size. To do that I have created settings and control. But the issue is I am unable to achieve like as shown in the image. I have used the range input type to adjust the value and it updates according to the increased or decreased value. I want to have a slider with a box that indicates the value and can be used to change the value …
Category: Web

About

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