2022 Developer Survey is open! Take survey.

Hot answers tagged

86 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
user avatar
29 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, ...
user avatar
26 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" * * Authors: @...
user avatar
  • 63.9k
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(...
user avatar
  • 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', ...
user avatar
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 ...
user avatar
  • 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 ...
user avatar
  • 3,129
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 ...
user avatar
10 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 ...
user avatar
  • 283
9 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 ...
user avatar
  • 8,161
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-...
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 ...
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'...
user avatar
  • 1,188
9 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 ...
user avatar
  • 103
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
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
user avatar
7 votes
Accepted

Passing a hardcoded page/post ID into `get_post`

For starters let's dive into what is 5 really. It is the post's ID. But what is ID in turn? It is value in the MySQL table row which identifies the specific post record. Issues with using IDs So ...
user avatar
  • 97.6k
7 votes

Passing a hardcoded page/post ID into `get_post`

This question is going to get some opinion based answers, but well, I'll give it a try. There are a couple of reasons why someone might've told you that, it would be great to know the context also. ...
user avatar
  • 1,309
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, ...
user avatar
6 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,...
user avatar
  • 215
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 ...
user avatar
6 votes
Accepted

Is there a JavaScript API? How to access public and private data in JS?

TL;DR There is no JavaScript API in the WordPress core and no one is planned, but actually, there is no need of it. Backend First of all let's say that, regarding the backend, some useful information ...
user avatar
  • 44.6k
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 ...
user avatar
  • 105k
6 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.
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
user avatar
  • 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 ...
user avatar
  • 10.3k
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 ...
user avatar
  • 19.7k
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.
user avatar
6 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: ...
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,...
user avatar

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