Skip to content

Instantly share code, notes, and snippets.

@rohmann
rohmann / anchor.js
Created April 3, 2014 22:07
Target an a tag normally used for anchor navigation. Provide an offset element, and it can do an animated scroll and compensate for fixed headers
jQuery(function($){
$('section a').click(function(e){
e.preventDefault();
$('html,body').animate({
scrollTop: $('#' + $(this).attr('href').split("#").slice(-1)[0] ).offset().top - $('header').height()
},700 ,'swing');
});
});
@rohmann
rohmann / wp-debug.php
Last active August 29, 2015 14:01
Debugging for live WordPress environments.
define('WP_DEBUG', true);
if (WP_DEBUG) {
/**
* Log errors to wp-content/debug.log
* Don't forget to delete this when you're finished,
* as it may be publicly accessible.
*/
define('WP_DEBUG_LOG', true);
@rohmann
rohmann / wp-debug-logging.php
Last active December 20, 2015 23:38
http://git.io/gJANNQ Enables debug logging for Wordpress.This snippet can be placed in wp-config.php to quickly setup debug logging. Adapted from: http://premium.wpmudev.org/forums/topic/the-right-way-to-debug-php-in-wordpress
define('WP_DEBUG', true);
/**
* When debug mode is enabled, you can enable logging to store errors in wp-content/debug.log
* This is useful for finding errors that might not get displayed on screen
*
* The next conditional section will make sure errors are not shown to users when logging is enabled
* This helps with quick troubleshooting on live sites
*/
define('WP_DEBUG_LOG', true);
@rohmann
rohmann / toolbar-nav.php
Last active December 21, 2015 04:58
This will give you a "theme location" for nav menus that will be placed in the toolbar. Had this in a repo, but moving to a gist.
<?php
/*
Plugin Name: Custom Toolbar
Plugin URI:
Description:
Author: Alexander Rohmann
Version: 1.0
Author URI: http://github.com/rohmann
*/
@rohmann
rohmann / wp-tiny-spam-protect.php
Last active December 21, 2015 04:59
Simple spam protection for Wordpress. Base64 encode with PHP, then decode with Javascript. Was in a repo, but just moving to gist as it's simple enough.
<?php
//Create a shortcode that encrypts the given text and adds a tag around the text that will lated be used to find the text on the client side
add_shortcode( 'nospam', function ( $atts, $content = null ) {
return '<em class="tsp-nospam">'.base64_encode($content).'</em>';
});
add_action('wp_head', function(){ ?>
<script>/* js base64 decode modified/condensed from http://www.webtoolkit.info/javascript-base64.html*/function wptspdecode(a){var b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",c="",d,e,f,g,h,i,j,k=0,l="",m=c1=c2=0;a=a.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(k<a.length){g=b.indexOf(a.charAt(k++));h=b.indexOf(a.charAt(k++));i=b.indexOf(a.charAt(k++));j=b.indexOf(a.charAt(k++));d=g<<2|h>>4;e=(h&15)<<4|i>>2;f=(i&3)<<6|j;c+=String.fromCharCode(d);i!=64&&(c+=String.fromCharCode(e));j!=64&&(c+=String.fromCharCode(f))}k=0;while(k<c.length){m=c.charCodeAt(k);if(m<128){l+=String.fromCharCode(m);k++}else if(m>191&&m<224){c2=c.charCodeAt(k+1);l+=String.fromCharCode((m&31)<<6|c2&6
@rohmann
rohmann / multisite-default-role.php
Last active December 21, 2015 05:59
Change the default role for users when creating a new site. http://git.io/6_L4Bw
<?php
//This will make the user an editor instead of admin for a new site
add_action('wpmu_new_blog','change_new_blog_role',100,6);
function change_new_blog_role($blog_id, $user_id, $domain, $path, $site_id, $meta) {
remove_user_from_blog($blog_id, $user_id);
add_user_to_blog($blog_id, $user_id, 'editor');
}
@rohmann
rohmann / gitarchivemaster.sh
Created August 24, 2013 05:47
Clones repository and creates zip file. (repo-name-master.zip)
#!/bin/bash
# Usage: $ ./gitarchivemaster.sh http://url/to/repo.git dest-folder
url=$1
name=$2
#Clone repo (delete existing)
rm -fR ${name}
git clone ${url} ${name}
@rohmann
rohmann / github_webhook.php
Last active December 21, 2015 15:28
Simple script for use with GitHub web hooks
<?php
//Only allow requests from Github (see: https://gist.github.com/webtekk/6326465)
if (!githubOrigin()) die('Unauthorized');
$data = json_decode($_POST['payload'],true);
//Only continue if this commit is on the master branch
if (!$data['ref'] == 'refs/heads/master') die('Not a master branch commit');
@rohmann
rohmann / github_origin.php
Last active December 21, 2015 15:28
Ensure a web request is coming from github. Useful for setting up web hooks
<?php
function githubOrigin() {
function netMatch($CIDR,$IP) {
list ($net, $mask) = explode ('/', $CIDR);
return ( ip2long ($IP) & ~((1 << (32 - $mask)) - 1) ) == ip2long ($net);
}
return (netMatch("204.232.175.64/27",$_SERVER['REMOTE_ADDR']) || netMatch("192.30.252.0/22",$_SERVER['REMOTE_ADDR']));
}
@rohmann
rohmann / remove-blog-option.php
Last active December 21, 2015 16:39
Removes the "Gimme a site / Just a username" option from Wordpress multisite signups. http://git.io/NO6z0A
<?php
function remove_signup_option(){
//Predefine what kind of registration is happening. wp-signup.php won't show the option if one is declared
global $active_signup;
$active_signup='blog';
}
add_action('signup_hidden_fields','remove_signup_option');