Skip to content

Instantly share code, notes, and snippets.

View danielbachhuber's full-sized avatar

Daniel Bachhuber danielbachhuber

View GitHub Profile
@danielbachhuber
danielbachhuber / query-params.php
Last active March 13, 2016 20:04
Compare query arguments supported by each WP REST API endpoint
<?php
/**
* Prepare a table comparing query arguments supported by each endpoint
*
* wp eval-file query-params --skip-wordpress
*/
$endpoints = array(
'posts' => '/wp/v2/posts',
'pages' => '/wp/v2/pages',
'media' => '/wp/v2/media',
<?php
/**
* Make a remote GET using DIGEST authentication.
*
* DIGEST authenticated requests require two GET requests:
* 1. GET the www-authenticate header for nonce, realm, opaque, and other values
* 2. GET the expected response body by constructing an 'Authorization' header
* with nonce, realm, and other values provided by the server.
*
@danielbachhuber
danielbachhuber / disable-logged-out-users.php
Last active September 5, 2024 01:55
Disable WP REST API requests for logged out users
<?php
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'restx_logged_out', 'Sorry, you must be logged in to make a request.', array( 'status' => 401 ) );
}
return $result;
<?php
/**
* See which routes support which contexts
*
* Run with `wp eval-file test-contexts.php --skip-wordpress`
*/
$response = WP_CLI\Utils\http_request( 'GET', 'http://wordpress-develop.dev/wp-json' );
$body = json_decode( $response->body, true );
$data = array();
@danielbachhuber
danielbachhuber / rest-api-schema.md
Last active October 12, 2015 23:27
JSON Schema in REST API endpoints

Q: What is the purpose of defining the resource with a schema?

  • To have the format of the object documented in a structured manner. Because it's documented in a structured manner, we can automagically create human-readable documentation from it.
  • Closely-coupling structured documentation to the code will help prevent it from getting out of date.
  • We can also use the schema internally to programmatically manipulate the request or response (e.g. validate required parameters, filter response based on context)

Q: Why JSON schema?

  • It is quite graybeard. JSON schema has been around for a long time, and is a well-known standard.
  • We can extend JSON schema base spec to include our own attributes, and publish this extension.
@danielbachhuber
danielbachhuber / gist:218df6da18f79ba2b854
Created April 16, 2015 21:09
Character countdown for Fieldmanager
this.setupMaxLengthCountdown();
$('.fm-item').on('fm_added_element', $.proxy( this.setupMaxLengthCountdown, this ) );
/**
* Set up max length countdown
*/
setupMaxLengthCountdown: function() {
$('.fm-element[data-fusion-enable-max-length-countdown]').each( function(){
var el = $(this);
<?php
/*
* Hack to restore the post_name that get_default_post_to_edit() noops
* Our assigned post_name via wp_insert_post_data can get fubar on post-new.php
*
* @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/post.php#L588
*/
add_filter( 'post_updated_messages', function( $messages ) {
global $pagenow, $post;
@danielbachhuber
danielbachhuber / gist:8af274e2b7f21c8c3bb6
Created July 3, 2014 14:40
Post-style permalinks for your custom post types
<?php
/**
* Post-style permalinks for your custom post types
* e.g. %year%/%monthnum%/%day%/%postname%
*/
function dbx_get_post_types() {
return array(
// replace with your custom post types
'my-custom-post-type'
);
@danielbachhuber
danielbachhuber / gist:c1554c1777dbb0460bfb
Created May 12, 2014 21:58
Serve a subset of terms at /term-slug instead of /tag/term-slug
<?php
/**
* 'special-term' is a unique butterfly
*/
function dbx_get_special_terms() {
return array( 'special-term' );
}
/**
@danielbachhuber
danielbachhuber / gist:9550397
Created March 14, 2014 15:48
In multisite, give editors and above the ability to upload whatever they want.
<?php
/**
* Allow editors and above to upload whatever they want
*/
add_filter( 'map_meta_cap', function( $caps, $cap, $user_id ) {
if ( 'unfiltered_upload' !== $cap ) {
return $caps;
}