Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
tommcfarlin / remove-javascript-from-wordpress-widget.php
Created June 20, 2012 12:58
Removing JavaScript tags from WordPress Widget input fields
/**
* This function is part of the WordPress Widget API.
*
* It's fired when the widget is being updated and using the incoming
* $new_instance to update the values stored in the incoming $old_instance.
*
* We're allowing users to store CSS and HTML in their input field but we're
* stripping out JavaScript tags.
*/
public function update( $new_instance, $old_instance ) {
@tommcfarlin
tommcfarlin / functions.php
Created August 27, 2012 13:09
A simple example of how to add a basic sidebar to a WordPress theme
/**
* Registers a new sidebar with the theme.
*
* Note: replace 'themename' with the name of your theme.
*/
function themename_add_sidebar() {
/*
* Description of the parameter array is as follows:
*
@tommcfarlin
tommcfarlin / meta-data-serialization.php
Last active November 4, 2022 00:28
An example function used to demonstrate how meta data is typically saved in a WordPress theme or plugin. The gist is made public so that developers can contribute to the standard security boilerplate functionality in order to simplify, reduce, and improve our serialization functions.
<?php
/**
* An example function used to demonstrate how to use the `user_can_save` function
* that provides boilerplate security checks when saving custom post meta data.
*
* The ultimate goal is provide a simple helper function to be used in themes and
* plugins without the need to use a set of complex conditionals and constants.
*
* Instead, the aim is to have a simplified function that's easy to read and that uses
* WordPress APIs.
@tommcfarlin
tommcfarlin / jquery-boilerplate.js
Created March 7, 2013 16:12
[WordPress] Properly loading jQuery within WordPress without having to use the `noConflict` method, or creating your own reference such as `$wp = jQuery`.
/**
* This gist demonstrates how to properly load jQuery within the context of WordPress-targeted JavaScript so that you don't
* have to worry about using things such as `noConflict` or creating your own reference to the jQuery function.
*
* @version 1.0
*/
(function( $ ) {
"use strict";
$(function() {
@tommcfarlin
tommcfarlin / strip-hidden-ascii-chars.php
Last active August 2, 2019 22:37
[WordPress] Occasionally, I've been parsing out data provided to me by third-parties and there have been hidden ASCII characters that can muck up programmatically inserting data into the database. Here's a simple regex for stripping out everything *except* alphanumeric characters.
<?php
// Replace anything that is not an 'a-z', 'A-Z', or '0-9' from the given $value
$value = preg_replace( "/[^a-zA-Z0-9\s]/", "", $value );
/*
* Read the comments below to see some of the available functions WordPress provides for evaluating the validity of the characters in the input string.
* /
@tommcfarlin
tommcfarlin / add-custom-post-type-menu.php
Created April 25, 2013 12:38
[WordPress] Add a custom post type menu as a child of an existing custom post type menu.
<?php
// Define the 'Portfolio' post type. This is used to represent galleries
// of photos. This will be our top-level custom post type menu
$args = array(
'labels' => array(
'all_items' => 'Gallery',
'menu_name' => 'Portfolio',
'singular_name' => 'Gallery',
'edit_item' => 'Edit Gallery',
@tommcfarlin
tommcfarlin / complete-mobile-media-query-boilerplate.css
Last active July 22, 2018 06:57
A media query boilerplate for responsive design that covers iPhones, iPhone 5s, tablets, and iPads for viewing sites and applications in both portrait and landscape mode.
/**
* Note that the following media queries are intended to be used for the specified device or screen size
* in both portrait and landscape mode.
*
* Desktop queries are not provided since the default styles for most sites and applications typically focus
* on that for the default sites.
*
* Contributes, comments, and all that fun stuff always welcome :).
*/
@tommcfarlin
tommcfarlin / ajax-reg.js
Last active August 26, 2017 14:08 — forked from csknk/ajax-reg.js
/**
* This file is enqueued by means of wp_enqueue_script() - variables are passed
* in from PHP by means of wp_localize_script()
*
*/
/* TM: We use an anonymous function to invoke the JavaScript. Also refactored for proper
* WordPress coding standards.
*/
(function( $ ) {
@tommcfarlin
tommcfarlin / get-trello-names.js
Last active July 7, 2021 15:15
[JavaScript] Returns an array of all of the names of a Trello boards members.
/**
* Retrieves a list of all of the members on a Trello board and stores and
* returns their names in an array. It will not include any duplicates.
*
* Trello names are usually represented as "Elliot Alderson (mrrobot)" but the
* returned array will only return an array with their actual name (that is,
* Ellio Alderson).
*
* This does not require jQuery or any third-party library to run. If you want
* to run this from the console of Chrome, then paste this entire function into
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"