Skip to content

Instantly share code, notes, and snippets.

View scarstens's full-sized avatar
🌟
Automating workflows.

Seth Carstens scarstens

🌟
Automating workflows.
View GitHub Profile
@scarstens
scarstens / function.valsort.php
Last active August 29, 2015 14:02
Value Sorting multi-dimensional arrays
/**
* Sorts an array by the value defined in $key
* @author Seth Carstens [seth@sethmatics.com]
*
* example array:
* $array[1]['regularvalue'->'sample1','selectedsortvalue'->'a comes before b']
* $array[2]['regularvalue'->'sample2','selectedsortvalue'->'c comes after b']
* ex array after usort($array, valsort('selectedsortvalue'));:
* $array[2]['regularvalue'->'sample2','selectedsortvalue'->'c comes after b']
* $array[1]['regularvalue'->'sample1','selectedsortvalue'->'a comes before b']
@scarstens
scarstens / wordpress.cssjs-per-page-loader.php
Created June 19, 2014 18:55
Per page CSS and JS loader for wordpress that loads CSS and JS using the URL slug. Loads if it exists, doesn't load if it doesn't exist. Its like get_template_part actions for CSS and JS files! Keywords: this, page, stylesheet, js, javascript, wordpress
//load styles and scripts for this page
add_action( 'wp_enqueue_scripts', 'this_page_stylesheet' );
add_action( 'wp_enqueue_scripts', 'this_page_js' );
function this_page_stylesheet() {
$ss_filename = str_replace('php', 'css', basename(__FILE__));
wp_enqueue_style( 'page-style', get_stylesheet_directory_uri().'/appearance/'.$ss_filename);
}
function this_page_js() {
@scarstens
scarstens / php_smart_session_start.php
Created May 29, 2014 23:50
PHP Session Start only if Session does not exist.
isset($_SESSION) || session_start();
@scarstens
scarstens / wordpress.disable_registration_.php
Created May 29, 2014 20:32
Disable default registration page in WordPress by redirecting away. Has filter for custom redirection. Used to secure registrations that do not follow standard WordPress registration workflows.
add_action('init','disable_default_registration');
function disable_default_registration(){
global $pagenow;
if( 'wp-login.php' == $pagenow && $_REQUEST['action'] == 'register') {
$registration_redirect = apply_filters('disable_default_registration_redirect', site_url());
wp_redirect($registration_redirect);
exit();
}
}
@scarstens
scarstens / destroy_php_session.php
Created May 5, 2014 17:14
Destroy PHP session
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
@scarstens
scarstens / class.array2xml.php
Last active August 29, 2015 14:00
Array to XML Using XDOMDocument
<?php
class XDOMElement extends DOMElement {
function __construct($name, $value = null, $namespaceURI = null) {
parent::__construct($name, null, $namespaceURI);
}
}
class XDOMDocument extends DOMDocument {
@scarstens
scarstens / wordpress.link-twitter-useranames.php
Created April 28, 2014 17:47
Automatically link twitter usernames in content and comment areas of WordPress.
@scarstens
scarstens / wordpress.different-langauges.php
Created April 28, 2014 17:45
Setup different languages for WordPress admin and front end
// setup one language for admin and the other for theme
// must be called before load_theme_textdomain()
function set_my_locale($locale) {
$locale = ( is_admin() ) ? "en_US" : "it_IT";
setlocale(LC_ALL, $local );
return $locale;
}
add_filter( 'locale', 'set_my_locale' );
@scarstens
scarstens / wordpress.text_widget_enable_shortcodes.php
Created December 5, 2013 23:35
Enable shortcodes in the default text widget, making custom widgets obsolete. Save hours of programming time by allowing shortcodes in the sidebars.
<?php
add_filter('widget_text', 'do_shortcode');
?>
@scarstens
scarstens / wordpress.add-admin-javascript.php
Created September 29, 2013 16:53
Script that adds or enqueues javascript to a wp-admin page (a specified page, not all of them)
function my_enqueue($hook) {
if( 'users.php' != $hook && 'user-meta-manager' != $_GET['page'])
return;
wp_enqueue_script( 'jquery-ui-sortable');
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );