Hot answers tagged

90 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
35 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, ...
29 votes
Accepted

Convert output of nav_menu items into a tree-like multidimensional array

The problem of building a tree from a flat array has been solved here with this, slightly modified, recursive solution: /** * Modification of "Build a tree from a flat array in PHP" * * ...
  • 65.6k
16 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(...
  • 273
16 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', ...
15 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 ...
  • 301
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 ...
  • 3,219
11 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 ...
  • 303
10 votes
Accepted

Disable requests to api.wordpress.org

You can Disable HTTP Calls by adding this in your wp-config.php define( 'WP_HTTP_BLOCK_EXTERNAL', TRUE ); This will disable/block all external HTTP requests and will make website alot faster. And ...
  • 8,311
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 ...
10 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 ...
  • 113
9 votes
Accepted

Does the WordPress API have Modal Dialogs

Yes, WordPress has modal dialog and it is called as Thickbox, but I am not sure how flexible it is to implement what you want to. Here is the code - <?php add_thickbox(); ?> <div id="my-...
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 ...
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'...
  • 1,303
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
  • 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
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,...
  • 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.
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: ...
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, ...
7 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 ...
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 ...
6 votes

Using filters and actions for plugin API?

Both approaches are not mutually exclusive. As @gmazzap said, don’t create a callback hell. But you can provide an initial hook, so other developers don’t have to rely on the rather slow ...
  • 106k
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
  • 160
6 votes

REST API for Multisite

I'm using the REST API to pull data about one multisite installation and feed it to sites in another multisite installation. Here's some of the code in use: class WPSE205354_Demo { function ...
  • 11.1k
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 ...
  • 20.1k
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.
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,...
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' => '...
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' ...
  • 76

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