Skip to content

Instantly share code, notes, and snippets.

@PeterBooker
PeterBooker / url_validation.php
Last active August 29, 2015 14:01
WordPress URL Validation Function
<?php
/**
* Validate URLs
*
* @param string $url
* @param boolean $strict
* @return string|boolean
*/
function kbso_validate_url( $url, $strict = false ) {
@PeterBooker
PeterBooker / wp-cloudfront.php
Created July 24, 2014 17:09
Make WordPress fully comptible with AWS CloudFront, to use as a full-page caching solution.
<?php
/**
* Add Last-Modified and Cache-Control Headers to WP Responses
*/
function wp_cloudfront_filter_headers( $headers ) {
/*
* WP already sets Cache-Control headers to avoid caching in the admin.
*/
if ( is_admin() ) {
@PeterBooker
PeterBooker / fallback.php
Last active August 29, 2015 14:17
Foundation 5 Top Bar Custom Menu Walker for WordPress
<?php
/*
* Provide fallback output incase no Menu is selected.
*/
public static function fallback( $args = array() ) {
/*
* No content for left side Fallback
*/
if ( 'left' == $args['menu_class'] ) {
@PeterBooker
PeterBooker / autoupdate_plugins.php
Last active August 29, 2015 14:18
WordPress Auto Updates Plugin Whitelist
<?php
/*
* Plugin Auto Updates Whitelist
* Allows Forced Updates for listed Plugin Slugs
*/
function prefix_forced_update_whitelist_plugins( $update, $item ) {
// Examples, please add plugin slugs that you trust.
$plugins = array (
'jetpack', 'wordpress-seo', 'plugin-slug',
@PeterBooker
PeterBooker / example.php
Last active December 21, 2015 07:19
Background Caching with Transients Example
<?php
/*
* Background Caching with Transients
* Using BBC News API as an example
*/
function pb_get_news() {
/*
* Get transient and if expired refresh data immediately
@PeterBooker
PeterBooker / pb_get_news.php
Created August 19, 2013 22:12
This functions fetches and refreshes the data if needed.
<?php
function pb_get_news() {
/*
* Get transient and if expired refresh data immediately
*/
if ( false === ( $news = get_transient( 'pb_bbc_news' ) ) ) {
// Uses BBC News API to fetch new data
<?php
/*
* Refreshes the Transient data.
*/
function pb_refresh_news() {
// Make HTTP request to BBC News API.
$response = wp_remote_request( 'http://api.bbcnews.appengine.co.uk/stories/headlines' );
<?php
/*
* Display BBC News Headlines
*/
function pb_display_news() {
// Get the News data
$headlines = pb_get_news();
@PeterBooker
PeterBooker / twitter_linkify.php
Last active December 23, 2015 07:28
This is taken from my Kebo Twitter Feed plugin for WordPress and linkifies Tweet text (Hashtags, Mentions and URLs).
<?php
/*
* Converts Tweet text urls, account names and hashtags into HTML links.
* Accepts return data from Twitter API (v1.1)
*/
function kebo_twitter_linkify( $tweets ) {
foreach ( $tweets as $tweet ) {
$hash_length = 45; // Length of HTML added to hashtags
@PeterBooker
PeterBooker / plugin_api.php
Last active December 25, 2015 22:29
This gives an example of how to query the wordpress.org plugins API for data on all plugins.
<?php
/*
* Query the wordpress.org Plugins API
*/
$query = array(
'action' => 'query_plugins',
'request' => serialize(
(object) array(
'page' => 1,