Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
LinzardMac / better-verbose-rewrite-rules.php
Created March 31, 2017 18:44
Better "verbose" rewrite rules for pages?
/**
* Verbose rewrite rules are required when there is a potential conflict between the slug of a page and the slug of
* another content type like a post type.
*
* WordPress needs to know first and foremost that they do not need to seek out a page when the front portion of a url
* has a value that matches the 'rewrite slug' of a CPT.
* We can avoid this by building the page rewrite rules to specifically ignore all instances where these match with the
* below code:
**/
@LinzardMac
LinzardMac / get-web-properties.ga.js
Created February 9, 2017 00:31
Custom Google Analytics - get web properties
/*
* initialize google analytics
*
*/
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
@LinzardMac
LinzardMac / gist:b8af908beb323907e8525119aeb815cb
Last active January 24, 2017 01:13
Find base64 images embedded in the post_content, download the image, sideload the image, and replace the base64 with the image file path and update the post_content
global $wpdb;
$requested_posts = $wpdb->get_results( "SELECT * FROM `wp_posts` WHERE `wp_posts`.`post_status` = 'publish' AND `wp_posts`.`post_type` = 'post' AND post_content LIKE '%base64%' LIMIT 9999999");
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
);
add_action( 'register_form', 'add_my_field' );
function add_my_field(){
$custom_field = ( ! empty( $_POST['custom_field'] ) ) ? trim( $_POST['custom_field'] ) : '';
echo '<label for="custom_field"'. _e( 'Custom Field Label =', 'textdomain' ) .'</label><br />
<input type="text" name="custom_field" id="custom_field" class="input" value="'. echo esc_attr( wp_unslash( $custom_field ) ) .'" size="25" />
';
}
@LinzardMac
LinzardMac / Test json endpoint response
Created July 9, 2016 01:15
Chrome Dev Tools Snippets
var xmlhttp = new XMLHttpRequest();
var url = "";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
@LinzardMac
LinzardMac / wp-rest-api-action.php
Created July 8, 2016 20:48
initialize the REST API methods in a plugin
add_action('plugins_loaded', 'bs_initialize_rest_api');
function bs_initialize_rest_api(){
add_action( 'rest_api_init', 'bs_add_custom_rest_fields' );
}
var windowWidth = jQuery(window).innerWidth(),
// Get the width of the slider space
containerWidth = jQuery('#slideContainment').width(),
// Get the height of the slider space
containerHeight = jQuery('.variable-width').height(),
// rename function arguments (actual image dimensions) for clarity
imageWidth = width,
imageHeight = height;
// Padded height calculated to account for additional of social media links
paddedHeight = containerHeight - 50;
@LinzardMac
LinzardMac / doc-bot.md
Last active August 25, 2016 01:23
##WordPress IRC Room Bot - a place for WordPress professionals to get and give support.

A support room isn't a support room without a solid IRC Bot. Here is a list of the current ##WordPress IRC Bot Commands

Command Description Usage Syntax
.help Asking for help in the room ----
.ignore Ignore a user .ignore SomeNick
.unignore Unignore a user .unignore SomeNick
.c / .codex Search the codex .c template heirarchy > SomeNick
.g Searches google .g your search terms > SomeNick
.y / .youtube Searched YouTube .y search terms > SomeNick
function alter_my_meta( $check, $object_id, $meta_key, $meta_value, $prev_value )
{
// Avoid infinite running of filter
remove_filter( current_filter(), __FUNCTION__ );
// Do whatever to alter the meta value
$meta_value = strtolower( $meta_value );
// Update with new value
update_post_meta( $object_id, $meta_key, $meta_value, $prev_value );
@LinzardMac
LinzardMac / functions.php
Last active November 9, 2015 18:25
Get a specified number of images in a post's gallery by post ID
function get_first_images($post, $count){
$post = get_post($post);
if ( ! has_shortcode( $post->post_content, 'gallery' ) ){
return array();
}
$galleries = array();
// find and separate out what kind of shortcode we are looking at
if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $shortcode ) {