This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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+ | |
**/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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 ) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |