Skip to content

Instantly share code, notes, and snippets.

View wpscholar's full-sized avatar
😀
Happy

Micah Wood wpscholar

😀
Happy
View GitHub Profile
@wpscholar
wpscholar / 404.php
Last active January 9, 2018 16:50
Force WordPress to return a 404 status
<?php
/**
* Force WordPress to return a 404 status
*/
global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();
@wpscholar
wpscholar / generate-list.php
Last active September 20, 2017 23:30
Take a multi-dimensional array in $_POST format and convert into an unordered list.
<?php
/**
*
* Take a multi-dimensional array in $_POST format and convert into an unordered list.
*
* @param $data
* @param array $args
* @return string
*/
@wpscholar
wpscholar / wp-redirect.php
Last active April 14, 2020 15:37
WordPress function for redirecting users on login based on user role
<?php
/**
* WordPress function for redirecting users on login based on user role
*/
function my_login_redirect( $url, $request, $user ){
if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if( $user->has_cap( 'administrator' ) ) {
$url = admin_url();
} else {
@wpscholar
wpscholar / wp-user-last-login.php
Created November 22, 2011 21:12
WordPress - Store timestamp of a user's last login as user meta
<?php
/**
* WordPress - Store timestamp of a user's last login as user meta
*/
function user_last_login( $user_login, $user ){
update_user_meta( $user->ID, '_last_login', time() );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );
@wpscholar
wpscholar / force-ssl.php
Last active September 20, 2017 23:30
Force SSL on pages in WordPress
<?php
/**
* Force SSL on pages in WordPress
*/
function force_ssl(){
if( !is_ssl() ){
$url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
wp_redirect( $url, 301);
exit();
@wpscholar
wpscholar / format-address.php
Last active September 20, 2017 23:30
Format an address
<?php
/**
* Takes an array containing address elements and outputs a formatted address.
*/
function get_formatted_address( $address, $args = array() ) {
$before = isset( $args['before'] ) ? $args['before']: '';
$after = isset( $args['after'] ) ? $args['after']: '';
$format = ( isset( $args['format'] ) && 'block' == $args['format'] ) ? 'block': 'inline';
$formatted_address = '';
@wpscholar
wpscholar / downloadable-csv.php
Last active June 7, 2020 04:16
Takes an array of data and create a csv file that will automatically download
<?php
/**
* Takes an array of data and creates a csv file that will automatically download
*/
function downloadable_csv( $data, $filename = 'data_csv' ){
/*
Sample Data Format:
array(
'headings' => array(),
@wpscholar
wpscholar / show-custom-post-types.php
Last active September 20, 2017 23:29
Show WordPress custom post types on the main blog and archive pages
<?php
/**
* Show WordPress custom post types on the main blog and archive pages
*
* @param WP_Query $query
**/
function show_custom_post_types( $query ) {
// Show all custom post types on main blog and archive pages
@wpscholar
wpscholar / jQuery.limitInput.js
Last active October 2, 2015 19:28
A jQuery plugin that places a character limit on text inputs
/**
* jQuery plugin that limits the number of characters that can be used in a text field.
*/
(function( $ ){
$.fn.limitInput = function( limit ) {
return this.each( function() {
$(this).keypress( function() {
if( $(this).val().length > limit ) {
$(this).val( $(this).val().substring(0, limit) );
}
@wpscholar
wpscholar / is_plugin_active.php
Created May 14, 2012 18:01
Test if a WordPress plugin is active
<?php
/**
* Test if a WordPress plugin is active
*/
if ( is_plugin_active('plugin-directory/plugin-file.php') ) {
// the plugin is active
}