Skip to content

Instantly share code, notes, and snippets.

View mrbobbybryant's full-sized avatar

Bobby Bryant mrbobbybryant

View GitHub Profile
@mrbobbybryant
mrbobbybryant / parse_shortcode_atts.php
Created September 8, 2015 18:07
Extract Shortcode Attributes for a given shortcode on save_post.
<?php
/**
* Extracts Shortcode Attributes from the post content
* @param $content
*
* @uses get_shortcode_regex
* @uses shortcode_parse_atts
*
* @return array
*/
<?php
function autosearch_metabox_callback() {
$output = '<div id="added-posts"><ul><input type="hidden" value=" " id="autosearch_posts" name="autosearch_posts" /></ul></div>';
$output .= '<label for="autosearch" id="autosearch-label" >';
$output .= '<input type="text" id="autosearch" name="autosearch" size="80" />';
echo $output;
}
add_action( 'add_meta_boxes', 'wp_autosearch_add_metabox' );
var acs_action = 'myprefix_autocompletesearch';
$("#autosearch").autocomplete({
source: function(req, response){
$.getJSON(AUTOSEARCH.ajaxurl+'?callback=?&action='+acs_action, req, response);
},
select: function(event, response) {
$('#added-posts').append( '<li id="' + response.item.id + '">' + response.item.label + '<i class="fa fa-pencil"></i><i class="fa fa-times"></i></li>' );
$("#autosearch_posts").attr( "value", response.item.id );
$(this).val(''); return false;
},
@mrbobbybryant
mrbobbybryant / limit_text.php
Created July 30, 2015 22:38
Limit text length
<?php
function limit_ticker_length( $title, $length ) {
if ( strlen( $title ) > $length ) {
$title = substr( $title, 0, ( $length -3 ) );
$title = substr( $title, 0, strrpos( $title,' ' ) ) . '&#8230;';
}
@mrbobbybryant
mrbobbybryant / array_map.php
Last active April 22, 2016 06:50
array_map tutorial
<?php
/**
* Template Name: array_map()
*
* @package WordPress
* @subpackage Twenty_fifteen
*/
/**
*
<?php
$permalink = 'http://www.youtube.com';
$id = 2;
$title = "My book title";
$pub_date = 'January 15th, 2015';
$city = "New York";
$zipcode = '28475';
$name = 'Bobby';
$like = 'WordPress';
@mrbobbybryant
mrbobbybryant / gist:2a68c60ce8dd5d98eee4
Last active August 29, 2015 14:21
array_filter example
<?php
//Allows you to difine your own callback function to uniquely filter an array.
function endswithy($value) {
return (substr($value, -1) == 'y');
}
//This example returns an array of people who's names end in "y"
$people = array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
$withy = array_filter($people, "endswithy");
var_dump($withy);
?>
@mrbobbybryant
mrbobbybryant / gist:8a59d9bbe6f754155d4d
Created May 27, 2015 14:34
Array_intersect, array_diff, and array_merge examples
<?php
$toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes");
$toppings2 = array("Ham", "Cheese", "Peppers");
$inttoppings = array_intersect($toppings1, $toppings2);
$difftoppings = array_diff($toppings1, $toppings2);
$bothtoppings = array_merge($toppings1, $toppings2);
echo '<p> array_intersect returns an array consisting of varables that both original array had in common.</p>';
var_dump($inttoppings);
@mrbobbybryant
mrbobbybryant / gist:238578ac4d4fb500bf63
Created April 19, 2015 20:18
Querying all taxonomies for all post types
<?php
function agency_wp_test() {
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
$taxonomy_names = get_object_taxonomies( $post_type );
$terms = get_terms( $taxonomy_names, array( 'hide_empty' => false ));
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<h5>'.$post_type.'</h5>';
jQuery(document).ready(function($){
var custom_uploader;
$('#upload_image_button').click(function(e) {
e.preventDefault();