Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / add_shortcake.php
Created February 3, 2015 15:39
WordPress: add_shortcake alias for add_shortcode
<?php
if ( function_exists( 'add_shortcode' ) && ! function_exists( 'add_shortcake' ) ) {
// tasty WordPress add_shortcode alias, for @technosailor
function add_shortcake( $tag, $func ) {
add_shortcode( $tag, $func );
}
}
@petenelson
petenelson / gga-ssh2-wordpress-sample.php
Created February 27, 2015 21:53
WordPress: Upload a file to a remote server via SSH2
<?php
/*
Plugin Name: GGA SSH2 Sample
Description: Sample SSH2 upload
Author: Pete Nelson (@GunGeekATX)
Version: 1.0
*/
if (!defined( 'ABSPATH' )) exit('restricted access');
@petenelson
petenelson / gga-api-helper.php
Created March 20, 2015 00:09
WordPress: Prevent specific plugins from loading during /api/ requests
<?php
/**
* Plugin Name: GGA API Helper
* Description: Prevents certain plugins from being loaded during API requests to reduce overhead.
* Author: Pete Nelson (@GunGeekATX)
* Version: 1.0.0
*/
/*
@petenelson
petenelson / pn-revisions-in-list-table.php
Last active August 29, 2015 14:18
WordPress: Display a list of post/page/custom post type revisions in the dashboard list tables
<?php
/*
Plugin Name: Revisions in List Table
Description: Allows a list of revisions to display in the dashboard list tables
Author: Pete Nelson
Version: 1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit( 'restricted access');
@petenelson
petenelson / timeline-express-filter-test.php
Created April 24, 2015 18:51
WordPress: Test the new Timeline Express filter
<?php
/*
Plugin Name: Timeline Express Custom Icon Filter Test
*/
add_filter( 'timeline-express-custom-icon-html', 'pn_timeline_express_custom_icon_html_test', 10, 3 );
function pn_timeline_express_custom_icon_html_test( $html, $post, $timeline_express_options ) {
$custom_png_icon = get_post_meta( $post->ID, '_custom_png_icon', true );
@petenelson
petenelson / multiformat-wp-api-response.php
Last active May 30, 2024 11:36
WordPress: Example of returning non-JSON results from the WP-API
<?php
// reference https://github.com/WP-API/WP-API/blob/develop/lib/infrastructure/class-wp-rest-server.php
// serve_request() function
add_filter( 'rest_pre_serve_request', 'multiformat_rest_pre_serve_request', 10, 4 );
function multiformat_rest_pre_serve_request( $served, $result, $request, $server ) {
// assumes 'format' was passed into the intial API route
// example: https://baconipsum.com/wp-json/baconipsum/test-response?format=text
@petenelson
petenelson / add-featured-image-to-post.php
Created July 9, 2015 22:29
WordPress REST API: Add featured image link to posts
<?php
add_filter( 'rest_prepare_post', array( $this, 'add_featured_image_link' ), 10, 3 );
public function add_featured_image_link( $data, $post, $request ) {
if ( has_post_thumbnail( $post->ID ) ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$data->add_link( 'featured_image', $featured_image[0], array( 'width' => absint( $featured_image[1] ), 'height' => absint( $featured_image[2] ) ) );
}
@petenelson
petenelson / rest-api-restrict-media-endpoint.php
Created July 17, 2015 02:48
WordPress REST API: Restrict access to the media endpoint to only authenticated users
<?php
// restrict access to the media endpoint
add_action( 'init', function() {
// _add_extra_api_post_type_arguments() in the WP REST API sets this to true
// we'll turn it off for unauthenticated requests
global $wp_post_types;
$wp_post_types['attachment']->show_in_rest = is_user_logged_in();
@petenelson
petenelson / rest-api-disallow-non-ssl.php
Last active August 29, 2015 14:25
WordPress REST API: Disallow non-SSL
<?php
add_filter( 'rest_pre_dispatch', 'rest_api_disallow_non_ssl', 10, 3 );
function rest_api_disallow_non_ssl( $response, $server, $request ) {
if ( ! is_ssl() ) {
$response = new WP_Error( 'rest_forbidden', __( "SSL is required to access the REST API" ), array( 'status' => 403 ) );
}
@petenelson
petenelson / rest-api-force-link-header-https.php
Created July 24, 2015 01:03
WordPress REST API: Force Link rel header to https