Hot answers tagged

62

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


44

I might be misunderstanding you, but why not use wp_create_nav_menu()? E.g., this is what I do to create a custom BuddyPress menu when I detect BP as active: $menuname = $lblg_themename . ' BuddyPress Menu'; $bpmenulocation = 'lblgbpmenu'; // Does the menu exist already? $menu_exists = wp_get_nav_menu_object( $menuname ); // If it doesn't exist, let's ...


25

The API you offer in a plugin or a theme depends on the logic of that specific code. There is probably no guide that applies to all situations. I am a contributor for multiple plugins with APIs, and what I have learned so far is: Do not offer an API until you really know how people use your code. Release the first two or three versions without any API. ...


19

The most comprehensive method is get_current_screen added in WordPress 3.1 $screen = get_current_screen(); returns WP_Screen Object ( [action] => [base] => post [id] => post [is_network] => [is_user] => [parent_base] => edit [parent_file] => edit.php [post_type] => post [taxonomy] => )


18

I think the best option is an endpoint. You get all the data as a simple string, so you can decide how it will be parsed, and you don’t have to worry about collisions with other rewrite rules. One thing I learned about endpoints: keep the main work as abstract as possible, fix the glitches in WordPress’ API in a data agnostic way. I would separate the ...


18

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: @DSkinner, @ImmortalFirefly and @SteveEdson * * @link https://stackoverflow.com/a/28429487/2078474 */ function buildTree( array &$elements, $parentId = 0 )...


18

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, 'callback' => 'get_animals', 'args' => [ 'id' ], ] ); The second parameter is simply a regex, thus you can use normal regex logic to ...


16

Cross-site Scripting Issues You cannot transfer WP auth cookies between domains. You also don't want to store plaintext passwords for logging into another WP installation programmatically. So, you'll have to have users log into WordPress, and then access their login status via an API endpoint from the third-party site. This lets WordPress handle all the ...


15

Here is the code I've used, based on the original question and on Dominic P's answer... /* * Modify HTTP header */ function add_header_xua($headers) { // var_dump($headers); #=> if you want to see the current headers... if (!is_admin()) { $headers['X-UA-Compatible'] = 'IE=edge,chrome=1'; } return $headers; } ...


13

As a complement to ZaMoose's anwser, here's how you would create a "Page-type" menu item (not a "Custom" one): wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => 'About', 'menu-item-object' => 'page', 'menu-item-object-id' => get_page_by_path('about')-...


13

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 code to your functions.php: <?php function post_fetured_image_json( $data, $post, $context ) { $featured_image_id = $data->data['...


11

I know it's been a while, but if anyone else stumbles on this, I found a WordPress hook specifically for modifying HTTP headers. The hook is wp_headers and it's called in the wp class. The first argument passed is an array of headers with the header name as the key. The second argument is a reference to the wp class object.


11

If you look at the source for the add_action() core function, it's just a wrapper for add_filter() function... And if you look at the do_action() core function, it's very similar to the apply_filters() core function, with one very key difference: it does not return a value. So what does this mean? actions are like filters, except an action does not return ...


10

I have a few issues with the accepted answer - that doesn't make it wrong, but I'll post my own code below which I feel might have a better result for some people since I had the same question but wanted to do the same thing with less code. First, the above code creates "URL" type navigation items, which is fine for some people but I want to link to PAGES, ...


10

...because your input (POST) name needs to match the one in your register_setting call: register_setting( 'first_tab_options', 'first_tab_items' ); .... <input type="text" name="first_tab_items"... Otherwise how the hell does WP know that some_name in POST holds your option data? ;)


9

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(__FILE__, array('Version' => 'Version'), false); $plugin_version = $plugin_data['Version']; Under the hood get_file_data does some cleaver scanning to be ...


9

There's a few bits here that apply, but the short of it is this code in customize-preview.js: this.body.on( 'click.preview', 'a', function( event ) { event.preventDefault(); self.send( 'scroll', 0 ); self.send( 'url', $(this).prop('href') ); }); The event.preventDefault prevents the links from actually working. The following code then sends a ...


8

No, but it should ;) There are several custom field class's (backend). wpAlchemy : https://github.com/farinspace/wpalchemy Meta Box Script: https://github.com/rilwis/meta-box My-Meta-Box: https://github.com/bainternet/My-Meta-Box meta-box-class: https://github.com/corycrowley/meta-box-class Meta Boxes Class: https://github.com/Bakke/Wordpress-Custom-Meta-...


8

There multiple WordPress Apis for getting a list of plugins like OTP listed in his answer. get_plugins() :Check the plugins directory and retrieve all plugin files with plugin data. Returns an array. Note: Only works on the admin side Example: function list_the_plugins() { $plugins = get_plugins(); foreach ( $plugins as $plugin ) { ...


8

If you are using PHP 5.2+ you're best bet is to just make a PHP array or object and use json_encode(). UPDATED: $cats = get_categories(); $output = array('categories' => array()); foreach ($cats as $cat) { $cat_output = array( 'cat_id' => $cat->term_id, 'cat_name' => $cat->name, 'posts' => array(), ); ...


8

I tried for a bit and could not get this to work perfectly, but it's close, it's hard to extend with the <form> for the popup styles being hardcoded, thought possible with more work. To get started you can: Enqueue the link popup javascript and styles, the main .js file is wp-includes/wplink.js. Depending on where you loading this you might need to ...


8

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 then you can whitelist domains that you don't want to block. define( 'WP_ACCESSIBLE_HOSTS', 'example.com, domain.com' );


8

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 seen in your example output. The WP_REST_Posts_Controller class has the following in its get posts method $posts_query = new WP_Query(); $query_result = $...


8

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', button: { text: 'Select' }, library: { type: [ 'video', 'image' ] }, });


8

See if your url is correct. Example: website.com/wp-json/wp/v2/posts/?categories=3&per_page=‌​50


7

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. Upto version 3.4 or 3.5, that function only worked when the parameter was a var, that might be one of the reasons why someone told you that. I, myself, don't ...


7

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 first there are some conceptual problems with it. It's not content. It's not something user creates, manipulates, or (most of the time) aware of. WP is (by design) ...


7

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 for the following four keys: page_template, post_category, tags_input and ancestors). Here's a quick example that shows the behavior: <?php $post_id = 42; $...


7

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, create a function that gets and stores in a WordPress transient the result of an API request. <?php /** * Plugin Name: WordPress StackExchange Question ...


6

Edit: get_post_type_labels was never intended to be a public function. It is only used internally when registering a post type. See these trac tickets: get_taxonomy_labels() and _get_custom_object_labels() fail if $object->taxonomy is not array Fatal error: Cannot use object of type stdClass as array in /.../wp-includes/post.php on line 1202 Like you ...


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