Hot answers tagged

95 votes
Accepted

WP Rest API - How to get featured image

You can get it without plugins by adding _embedas param to your query /?rest_route=/wp/v2/posts&_embed /wp-json/wp/v2/posts?_embed
Eslam Mahmoud's user avatar
37 votes
Accepted

Wordpress Rest API custom endpoint optional param

You should put the named parameters of the route regex into an optional capturing group: register_rest_route( 'api', '/animals(?:/(?P<id>\d+))?', [ 'methods' => WP_REST_Server::READABLE, ...
Nicola Peluchetti's user avatar
19 votes

I Want to Get A Plugin Version Number Dynamically

An alternative to get_plugin_data() is get_file_data() which is available without the overhead of loading additional files. Simply add this to your main plugin file: $plugin_data = get_file_data(...
paulcol.'s user avatar
  • 303
18 votes
Accepted

Show only images and videos in a wp.media window

It's been a while since this question was asked, but on the off chance that you are still looking for a solution: items_frame = wp.media.frames.items = wp.media({ title: 'Add to Gallery', ...
user433351's user avatar
16 votes

WP Rest API - How to get featured image

I would NOT use the better rest API plugin. It did add featured images to the rest api but it also broke it. This is the simplest solution I was able to find that actually worked. Add the following ...
Null TX's user avatar
  • 311
12 votes
Accepted

Set up WP Authentication from External API

Update: Made a blog post to explain this better :) I was able to do this by WP's authenticate filter inside a new plugin; most of which is guided by this tutorial by Ben Lobaugh. Major points on the ...
Suika's user avatar
  • 313
11 votes

Adding additional data to WP_Post object

If your extra data directly references a post meta you don't have to do anything, because WP_Post implements the »magic« methods __isset() and __get() which directly asks for post meta keys (except ...
David's user avatar
  • 3,269
11 votes

WP REST API only returning partial list of users

To anyone who might still be hitting this problem, here's a checklist: Make sure you are authenticated AND your user has the list_users capability. Example: When adding a custom role, I make sure ...
Abe Caymo's user avatar
  • 223
10 votes
Accepted

WP-API v2 Custom Endpoint Response Formatting

You can call the REST API methods to prepare your output in the same way that the plugin does by default, this will also allow any plugins to tie into the output as you have used the ACF plugin as ...
Chris Morris's user avatar
9 votes
Accepted

get_userdata by username

get_userdata() function is an alias of get_user_by('ID') function. Use this code: $username = 'your user name'; $user = get_user_by('login', $username); It will return WP_User object on success, or ...
Frank P. Walentynowicz's user avatar
9 votes
Accepted

Fetch All Posts (Including Those Using a Custom Post Type) With Wordpress API

I ended up extending the API like suggested in the comments to my question, although I was hoping there was a default route that fetched all posts of all post types. Apparently, there isn't. So here'...
grazdev's user avatar
  • 1,323
8 votes

WP Rest API - How to get featured image

You can get the name of the image with this path: array_name._embedded['wp:featuredmedia']['0'].source_url
RobK's user avatar
  • 81
8 votes
Accepted

Get more than 10 posts in a specific category with the wordpress api

See if your url is correct. Example: website.com/wp-json/wp/v2/posts/?categories=3&per_page=‌​50
Cesar Henrique Damascena's user avatar
8 votes

WP Rest API not working

First you've to Check if the WordPress REST API is enabled or not The best way to check is to visit this URL: https://yoursite.com/wp-json. If you see some JSON response, REST API is enabled. If it’s ...
Anand Raja's user avatar
7 votes

How can you check if you are in a particular page in the WP Admin section? For example how can I check if I am in the Users > Your Profile page?

I find it weird that no one has mentioned the fact that the add_menu_page function returns an action hook which you can use to do certain actions only on those pages $hook = add_menu_page($menu_title,...
Tofandel's user avatar
  • 225
7 votes

Get a list of all WordPress.org Plugins?

You can start with something like this: https://api.wordpress.org/plugins/info/1.2/?action=query_plugins&request[page]=1&request[per_page]=400 I think it's self-explanatory.
Michael Field's user avatar
7 votes
Accepted

WP API Get post with tag names instead of tag ID's

I figured something out based on what I found at this post. Basically I need a plugin that listens for when the REST response is about to go out. The plugin code would be similar to the following: ...
The Unknown Dev's user avatar
7 votes

External API to WP

I'd recommend writing a simple plugin. Read through the Plugin Development Handbook, but it's pretty easy. The only thing necessary for a plugin is the header with the Plugin Name. In the plugin, ...
Nathan Johnson's user avatar
6 votes

Difference Between Filter and Action Hooks?

In simple word's. Actions are those PHP functions which execute the output. Filters are those PHP functions which return the output. Updated: We can extend any plugin which use the actions ...
maheshwaghmare's user avatar
6 votes

WordPress API - count posts

With WP-CLI installed from https://wp-cli.org/ you can retrieve the total post count by using: wp post list --format=count Full documentation for the POST LIST command
Justin's user avatar
  • 160
6 votes
Accepted

how to authenticate for the REST API from a plugin and from command line

GET requests, like listing posts, doesn't need authentication at least you need to get private posts. Your problem is that you are using a route/endpoint that doesn't exist. In WordPress 4.4, WP REST ...
cybmeta's user avatar
  • 20.4k
6 votes

WP Rest API - How to get featured image

Take a look at a plugin called Better REST API Featured Image. It adds the featured image URL to the original API response.
Michael Cropper's user avatar
6 votes
Accepted

Is it safe to fix Access-Control-Allow-Origin (CORS origin) errors with a php header directive?

Yes, you open your site to being requested via AJAX to any other script in the whole web. It would be better if you limit the origin to one specific remote domain from which you are consuming the API,...
Jesús Franco's user avatar
6 votes

How can I cache WordPress Rest API Response

You should create a new instance from WP_REST_Response to set the Cache-Control value. <?php register_rest_route('wp/v2', '/your_endpoint', array( 'methods' => 'GET', 'callback' => '...
Mostafa Soufi's user avatar
6 votes
Accepted

Send data to 3rd party api with wp_remote_post on wp_login

The 'body' needs to be an array, not including the 'json_encode($user)' piece. $response = wp_remote_post( 'myapp.com/endpoint', array( 'method' => 'POST', 'headers' => array('Content-Type' ...
patman's user avatar
  • 76
6 votes
Accepted

fetching via fetch/ajax gutenberg block data from third party

Well, I figured it out. Though my solution is probably far from elegant (I'm not making concessions for timeouts or usability/accessibility). My problem was mostly overzealous configuration, copy and ...
jshwlkr's user avatar
  • 512
6 votes
Accepted

Sending POST Request from server

'wp_remote_post' is the function you're searching for. $response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1....
Krzysiek Dróżdż's user avatar
6 votes
Accepted

base64_encode conflict with convert_smilies in wordpress

Here's the code from formatting.php if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) { // HTML loop taken from texturize function, could possible be consolidated. $...
Rup's user avatar
  • 4,207
5 votes
Accepted

Query WP REST API v2 by multiple meta keys

Adding a custom endpoint is pretty straightforward. I also modified the url to look more like http://example.com/wp-json/namespace/v2/posts?filter[meta_value][month]=12&filter[meta_value][year]=...
jgraup's user avatar
  • 9,719
5 votes
Accepted

Creating woocommerce product using WordPress REST API

The new api's documentation can be found here Woocommerce Rest API And you can create a Woocommerce product by posting data to the wp-json endpoint /wp-json/wc/v1/products (Documentation Here) $data ...
Tunji's user avatar
  • 2,921

Only top scored, non community-wiki answers of a minimum length are eligible