Skip to content

Instantly share code, notes, and snippets.

View andrewspear's full-sized avatar

Andrew Spear andrewspear

View GitHub Profile
@andrewspear
andrewspear / currentPageURL.php
Last active December 19, 2015 19:18
A simple PHP function to fetch the current page URL with PHP, .htaccess rewrites are respected and optionally you can exclude the query string, useful for creating canonical URLs.
<?
function currentPageURL($includeQueryString = true) {
$pageURL = 'http';
$pageURL .= ($_SERVER["HTTPS"] == "on") ? "s" : "";
$pageURL .= "://";
$pageURL .= ($_SERVER["SERVER_PORT"] != "80") ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
if (!$includeQueryString) {
$pageURL = str_replace('?'.$_SERVER["QUERY_STRING"], '', $pageURL);
}
@andrewspear
andrewspear / .htaccess
Last active December 21, 2015 08:28
Force specific sub-domains of a site to use SSL
# Force SSL for specific subdomains
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(secure|login)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
@andrewspear
andrewspear / .htaccess
Created August 20, 2013 07:54
Force www. prefix on a domain, without hard-coding the domain itself.
# Force www. prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
@andrewspear
andrewspear / valueToTitle.php
Last active December 21, 2015 11:59
Convert a camelCase or underscore_separated string to Title Case. Useful for converting database column names to readable text.
<?
// Convert a camel-case or underscore_separated string to Title Case
function valueToTitle($string) {
$string = preg_replace('/(?!^)([[:upper:]][[:lower:]]+)/', ' $0', $string);
$string = preg_replace('/(?!^)([[:lower:]])([[:upper:]])/', '$1 $2', $string);
$string = str_replace('_', ' ', $string);
$string = ucwords($string);
return $string;
}
@andrewspear
andrewspear / social_counts.inc.php
Last active November 2, 2018 19:50
Count your Facebook followers, Twitter followers, Google+ followers, LinkedIn group followers, Constant Contact subscribers, YouTube subscribers, YouTube channel views.
<?
/*
$facebook_followers = social_count('facebook', array(
'id' => '<your_facebook_page_name_or_id>',
'api_key' => '<your_facebook_app_api_key>',
'api_secret' => '<your_facebook_app_api_secret>'
));
@andrewspear
andrewspear / camelCaseToTitleCase.php
Last active December 23, 2015 18:59
Convert camel case string to a title case string in PHP
<?
function camelCaseToTitleCase($string) {
return ucwords(preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $string));
}
?>
@andrewspear
andrewspear / minified.php
Last active August 29, 2015 14:01
Minify HTML/JS/CSS Output with PHP
<?
/*
Description: Place this code at the top of your PHP page to minify all of it's HTML output
Dependencies: https://code.google.com/p/minify/
*/
function sanitize_output($buffer) {
require_once('min/lib/Minify/HTML.php');
require_once('min/lib/Minify/CSS.php');
@andrewspear
andrewspear / functions.php
Last active July 6, 2017 08:10
Wordpress function to force Facebook to refresh it's cache as a scheduled post
<?
// Ping Facebook with (via a POST request) to refresh the cache (scrape) for a scheduled post as it is published
// Relevant docs: https://developers.facebook.com/docs/opengraph/using-objects#selfhosted-update
// Place this in your Wordpress functions.php file
add_action('transition_post_status', 'purge_future_post', 10, 3);
function purge_future_post($new_status, $old_status, $post) {
if($new_status == 'publish') {
@andrewspear
andrewspear / enhancedNumberInput.jquery.js
Last active August 29, 2015 14:11
Using a number input field is great for basic HTML5 browser validation (but this only kicks in on submit) and smartphones will usually offer up a number-only keyboard which is nice. However this script enhances the HTML5 number input field so it's easier to use: 1) only numbers can be typed in the box, 2) you can't type more characters than the …
// Prevent non-numeric entries on number inputs
$("input[type=number]").keydown(function (e) {
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen
return;
@andrewspear
andrewspear / wp_domain_update.sql
Created April 2, 2015 15:53
If you are updating the domain of your website but it's running Wordpress, run the code below to update all the old domain references in the database with new domain references.
UPDATE `wp_posts` SET `guid` = replace(guid, 'http://old.domain.com', 'https://new.domain.com');
UPDATE `wp_posts` SET `post_content` = replace(post_content, 'http://old.domain.com', 'https://new.domain.com');
UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, 'http://old.domain.com', 'https://new.domain.com');
UPDATE `wp_options` SET `option_value` = replace(option_value, 'http://old.domain.com', 'https://new.domain.com');
UPDATE `wp_comments` SET `comment_content` = replace(comment_content, 'http://old.domain.com', 'https://new.domain.com');
UPDATE `wp_comments` SET `comment_author_url` = replace(comment_author_url, 'http://old.domain.com', 'https://new.domain.com');
UPDATE `wp_commentmeta` SET `meta_value` = replace(meta_value, 'http://old.domain.com', 'https://new.domain.com');
UPDATE `wp_users` SET `user_url` = replace(user_url, 'http://old.domain.com', 'https://new.domain.com');