Skip to content

Instantly share code, notes, and snippets.

@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 / 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+
**/
<?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 / add-frontkit-support.php
Created October 25, 2016 05:12
Add FrontKit support
<?php
add_action( 'after_setup_theme', 'add_frontkit_support' );
function add_frontkit_support() {
add_theme_support( 'frontkit', array(
'title' => '.single-post .entry-title',
'content' => '.single-post .entry-content',
) );
}
@websupporter
websupporter / authorbio-shortcode-auto-include.php
Created January 2, 2017 19:41
Add the authorbio shortcode automatically.
<?php
add_filter( 'the_content', function( $content, $post_id ) {
// Run only on blog posts.
if ( 'post' !== get_post_type( $post_id ) ) {
return $content;
}
// Do not run on the old blog posts, where we already have placed our author bio.
@websupporter
websupporter / rest-api-nonces-not-working.php
Created January 18, 2017 11:56
Example Code for my blog post of REST API and Nonces
<?php
/**
* Plugin Name: Nonces in der REST API Not working
* Author: Websupporter
* Plugin URL: http://websupporter.net/blog/de/nonces-in-der-rest-api/
* Licence: GPL
**/
add_action( 'wp_ajax_nonce-test', 'ajax_nonce_test' );
@websupporter
websupporter / rest-api-nonces.php
Created January 18, 2017 11:57
Example Script for my blog post on Nonces in the REST API
<?php
/**
* Plugin Name: Nonces in der REST API
* Author: Websupporter
* Plugin URL: http://websupporter.net/blog/de/nonces-in-der-rest-api/
* Licence: GPL
**/
<?php
add_action( 'after_setup_theme', 'theme_start_content' );
function theme_starter_content() {
add_theme_support( 'starter-content', array(
'widgets' => array(
'sidebar-1' => array(
'text_business_info',
'search',
),
<?php
add_action( 'after_setup_theme', 'theme_start_content' );
function theme_starter_content() {
add_theme_support( 'starter-content', array(
'widgets' => array(
'sidebar-1' => array(
'text_business_info',
'search',
array(
<?php
class Mein_Widget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( 'meine-base-id', _x( 'My widget', 'theme-slug' ) );
}
function widget( $args, $instance ) {