Skip to content

Instantly share code, notes, and snippets.

<?php
function only_allow_logged_in_rest_access( $access ) {
if( ! is_user_logged_in() ) {
return new WP_Error( 'rest_cannot_access', 'Du kannst nur eingeloggt die REST API nutzen.', array( 'status' => rest_authorization_required_code() ) );
}
return $access;
}
add_filter( 'rest_authentication_errors', 'only_allow_logged_in_rest_access' );
@websupporter
websupporter / rest-api-filter-param.php
Last active October 21, 2016 11:40
Before the content endpoints ended up in core, you where able to filter posts with the filter parameter. The support was dropped with the merge. This plugin brings back the support
<?php
/**
* Plugin Name: WordPress REST API filter query
* Description: This plugin adds a "filter" query parameter to API collections to filter returned results based on arbitrary WP_Query parameters, adding back the "filter" parameter that was removed from the API when it was merged into WordPress core.
* Author: WP REST API Team
* Author URI: http://v2.wp-api.org
* Version 0.1
* License: GPL2+
**/
@websupporter
websupporter / api.js
Created September 3, 2016 07:02
Simple usage of the Rest nonce
$.ajax( {
url: wpApiSettings.root + 'wp/v2/posts/1',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
@websupporter
websupporter / enqueue-api-script.php
Created September 3, 2016 06:59
usage of rest_url() and rest nonce
<?php
function slug_enqueue_scripts() {
wp_enqueue_script( 'api-script', plugins_url( __FILE__ ) . 'script.js', array( 'jquery' ) );
wp_localize_script(
'wp-script',
'wpApiSettings',
array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
@websupporter
websupporter / register-field.php
Created September 1, 2016 14:31
register_rest_field() #wcfra
<?php
add_action( 'rest_api_init', 'slug_register_something_random' );
function slug_register_something_random() {
register_rest_field( 'post',
'something',
array(
'get_callback' => 'slug_get_something',
'update_callback' => 'slug_update_something',
'schema' => null,
@websupporter
websupporter / register-meta-for-rest.php
Last active September 2, 2016 12:10
How to Register custom fields for the REST API
<?php
$args = array(
'type' => 'string',
'description' => 'The International Standard Book Number',
'single' => true,
'sanitize_callback' => 'sanitize_isbn',
'auth_callback' => 'is_allowed_to_change_isbn',
'show_in_rest' => true,
);
@websupporter
websupporter / register-cpt-for-api.php
Created August 29, 2016 06:10
How to register a custom post type for the REST API #wcfra
<?php
function register_cpt() {
$args = array(
'labels' => array(
'name' => 'Name',
'singular_name' => 'Single',
),
'menu_position' => 5,
'public' => true,
'show_in_rest' => true, // Mache diesen Post Type über die API zugänglich.
@websupporter
websupporter / register-rest-route.php
Last active August 28, 2016 07:58
How to register an endpoint in the WordPress REST API #wcfra
<?php
add_action( 'rest_api_init', 'my_own_restroute' );
function my_own_restroute() {
register_rest_route(
'my-namespace/v1',
'/route/',
array(
'methods' => 'GET',
'callback' => 'my_restroute_callback',
'args' => array(
@websupporter
websupporter / die-in-beauty.php
Last active May 27, 2016 10:06
Use the wp_die_handler to alter your frontend dead
<?php
/**
* Plugin Name: Schöner Sterben mit wp_die()
* Plugin Author: David Remer/Websupporter
**/
add_filter( 'wp_die_handler', 'die_in_beauty_filter' );
function die_in_beauty_filter( $die_handler ) {
if ( is_admin() ) {
return $die_handler;
@websupporter
websupporter / sanitize-with-register-meta.php
Created May 16, 2016 08:26
Use register_meta to sanitze your meta values
<?php
register_meta( 'post', 'mein_metakey', 'mein_metakey_sanitize_callback' );
function mein_metakey_sanitize_callback( $value, $key, $type ){
if ( 'post' === $type && 'mein_metakey' === $key )
return (int) $value;
return $value;
}
?>