Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / page-api.php
Created February 6, 2013 19:14
WordPress: Create a REST API endpoint
<?php
/*
* Sample code for using WordPress as a REST API endpoint (vs AJAX Admin)
* Author: Pete Nelson @GunGeekATX
*
* 1) Create a page called API in WordPres
* 2) Create a file called page-api.php in your theme directory
* 3) Add code as-needed
*
*/
@petenelson
petenelson / chart-image.php
Created April 24, 2013 21:17
PHP - generate a date range progress bar chart as an image
<?php
// some defaults
$start = new DateTime('2013-04-11');
$end = new DateTime('2013-05-09');
$current = new DateTime('2013-04-19');
if (isset($_REQUEST['s']))
$start = new DateTime($_REQUEST['s']);
@petenelson
petenelson / custom-post-type-title-placeholder.php
Created May 2, 2013 14:51
WordPress - Change custom post type "Enter Title Here" placeholder
// via http://www.paulund.co.uk/change-the-enter-title-text-for-custom-post-types
// just saving it here for later reference
function change_default_title( $title ){
$screen = get_current_screen();
if ( $screen->post_type == 'product' ) {
return 'Enter New Product Here';
}
}
add_filter( 'enter_title_here', 'change_default_title' );
@petenelson
petenelson / add-to-header.php
Created May 9, 2013 19:23
WordPress: Add custom CSS file to a post/page
// add to header.php prior to wp_head()
$custom_css_file = get_post_meta( get_the_id(), $key = 'custom-css-file', $single = true );
if (false !== $custom_css_file && !empty($custom_css_file))
wp_enqueue_style( get_the_id() . '-custom-css-file', get_template_directory_uri() . '/' . $custom_css_file, array(), $version_tag_here );
@petenelson
petenelson / gga-twitter-card.php
Created June 22, 2013 23:08
WordPress: Twitter Card
<?php
/*
Plugin Name: GGA Twitter Card
Description: Adds Twitter card meta tags to the header
Author: @GunGeekATX
*/
add_action('wp_head', 'gga_twitter_card_wp_head');
function gga_twitter_card_wp_head() {
@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
@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 / 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 / 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 / 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