Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@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 / set-a-cookie.php
Created February 17, 2014 18:24
PHP: Set a cookie
@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 / 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 / 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 / gga-set-featured-image-from-url.php
Created February 5, 2014 16:01
WordPress: Download and set featured image for a post from a URL
@petenelson
petenelson / ajax-posts-by-taxonomy.php
Last active January 4, 2016 12:49
WordPress: AJAX action to return posts by taxonomy term
<?php
/*
Plugin Name: GGA - AJAX Posts by Taxonomy
Author: Pete Nelson (@GunGeekATX)
Description: <a href="http://petenelson.com/wp-admin/admin-ajax.php?action=gga_posts_by_taxonomy&post_type=post&taxonomy=post_tag&term=android">Try it out</a>
Version: 1.0
*/
/*
http://petenelson.com/wp-admin/admin-ajax.php?action=gga_posts_by_taxonomy&post_type=post&taxonomy=post_tag&term=android
@petenelson
petenelson / api.php
Last active January 4, 2016 11:28
Force all browsers to reload on any attached browser's page reload
<?php
// just using a file, could easily be replaced by some other storage method
if (isset($_REQUEST['new']) && $_REQUEST['new'] == '1') {
$ver = new stdClass();
$ver->ver = time();
file_put_contents('ver.json', json_encode($ver));
}
else {
@petenelson
petenelson / gga-import-pages.php
Created January 17, 2014 17:50
WordPress: Simple bulk page importer
<?php
/*
Plugin Name: GGA Import Pages
Description: Quick plugin to bulk import a list of pages <a href="admin-ajax.php?action=gga_import_pages">[Import Now]</a>
*/
add_action( 'wp_ajax_gga_import_pages', 'gga_import_pages' );
function gga_import_pages() {
global $post;
@petenelson
petenelson / wp-localize-test-data.php
Last active December 22, 2015 07:18
WordPress: wp_localize_script with a stdClass
$test_data = array();
$test_data[] = new stdClass();
$test_data[0]->field1 = 'hello';
$test_data[0]->field2 = 'world';
$test_data[0]->my_object = new stdClass();
$test_data[0]->my_object->field3 = 'more hello world';
wp_localize_script( 'gmec-intranet', 'test_data', $test_data );
// outputs the following Javascript