Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / state-list.php
Created February 6, 2014 16:08
PHP: Return list of states as an array
function state_list() {
return array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
@petenelson
petenelson / rename-submenu-items.php
Created February 13, 2014 14:49
WordPress: Rename submenu items
// from: http://code.tutsplus.com/articles/customizing-your-wordpress-admin--wp-24941
// bookmarking it for later
function edit_admin_menus() {
global $menu;
global $submenu;
$menu[5][0] = 'Recipes'; // Change Posts to Recipes
$submenu['edit.php'][5][0] = 'All Recipes';
$submenu['edit.php'][10][0] = 'Add a Recipe';
@petenelson
petenelson / validate-nonce-from-custom-wp-list-table.php
Created February 13, 2014 19:01
WordPress: how to validate a nonce generated by a custom WP_List_Table
<?php
class Custom_List_Table extends WP_List_Table() {
// custom list table code here
}
$list_table = new Custom_List_Table();
$list_table->prepare_items();
// verify the nonce field generated near the bulk actions menu
@petenelson
petenelson / set-a-cookie.php
Created February 17, 2014 18:24
PHP: Set a cookie
@petenelson
petenelson / admin-hook-on-plugin-load.php
Created February 19, 2014 19:59
WordPress: Admin hook for plugin loading
// can be run inside of an admin_init hook
global $pagenow;
global $plugin_page;
add_action('load-' . get_plugin_page_hook($plugin_page, $pagenow), 'my_function' );
@petenelson
petenelson / login-cookie-timeout.php
Last active August 29, 2015 13:56
WordPress: change login cookie timeout
@petenelson
petenelson / get-term-by-value.php
Created February 28, 2014 21:22
WordPress: Advanced Custom Fields, find taxonomy term by custom value
function get_term_by_value($taxonomy, $field, $value) {
// probably a better way to do this, but this works for now
// get_field is part of Advanced Custom Fields
if (function_exists('get_field')) {
foreach (get_terms( $taxonomy ) as $term) {
$field = get_field($field, $term);
if ($value == $field)
return $field;
}
@petenelson
petenelson / custom-post-type-columns-array-insert.php
Last active August 29, 2015 13:58
WordPress: Custom post type columns with array insert
function array_insert($array, $values, $offset) {
// http://stackoverflow.com/questions/1783089/array-splice-for-associative-arrays/11114956#11114956
return array_slice($array, 0, $offset, true) + $values + array_slice($array, $offset, NULL, true);
}
function admin_columns($columns) {
// this is the 'manage_edit-{post-type}_columns' filter
// add_filter( 'manage_edit-' . self::$post_type . '_columns', array($this, 'admin_columns'), 10, 1 ) ;
$columns = array_insert($columns, array('sales-channel' => 'Sales Channel'), 2);
return $columns;
<?php
/*
Plugin Name: GGA - git branches
Description: Dashboard Widget
Author: Pete Nelson
Version: 1.0
*/
if (!defined( 'ABSPATH' )) exit('restricted access');
@petenelson
petenelson / filter_large_image_dimensions.php
Created December 2, 2014 16:13
WordPress: Prevent large image uploads by dimension
<?php
add_filter( 'wp_handle_upload_prefilter', 'gga_filter_large_image_dimensions' );
function gga_filter_large_image_dimensions( $file ) {
$maxsize = 2000;
if ( false !== strpos($file['type'], 'image/') ) {
$size = getimagesize( $file['tmp_name'] );