Skip to content

Instantly share code, notes, and snippets.

View hearvox's full-sized avatar

Barrett Golding hearvox

View GitHub Profile
@hearvox
hearvox / g-form-sheet-email.js
Created October 29, 2022 15:12
Upon Google Form submission, send response values to respondent in an emailed PDF attachment
/**
* Process form data: make PDF, send email; trigged on Form Submit.
*
* Documentation:
* https://docs.google.com/document/d/1O4Y-3VqhhnL-kXPXM3S2XDoQebevIH7UoXQUxpjjtuo/edit
*
* @param {e} obj Form data (includes user-submitted field values).
*
* @return void
*/
@hearvox
hearvox / wp_2020_custom.php
Last active September 17, 2022 16:34
WordPress Twenty Twenty theme custimizations, via custom plugin (e.g., mod date in post meta)
<?php
/**
* Adds modified date to displayed post meta.
*
* Uses action in theme's /inc/template-tags.php file.
* Param values passed by the action hook.
* @param int $post_id The ID of the post.
* @param array $post_meta An array of post meta information.
* @param string $location The location where the meta is shown.
*/
@hearvox
hearvox / wp-post-status.php
Last active April 13, 2020 20:36
WordPress custom post status function (none for Block editor yet)
<?php
/*******************************
CUSTOM POST STATUS
******************************/
/**
* Register custom post status.
*
* @return object
*/
function my_custom_post_status(){
@hearvox
hearvox / wp-sort-limit-meta.php
Created September 16, 2019 17:58
Filter post query results by one one post-meta value; sort results by another post-meta value.
<?php
/**
* Sort query results by one meta value; filter by another.
* For example, post are 'cities':
* Sort cities with >40K population ('city_pop') by average income ('city_income').
*/
$args = array(
'post_type' => 'cities',
'posts_per_page' => 1000,
'meta_query' => array(
@hearvox
hearvox / get_string_between.php
Created January 11, 2019 20:30
Get a string between two specified strings (can parse XML, HTML, or other code/text).
<?php
// @ see: http://www.justin-cook.com/2006/03/31/php-parse-a-string-between-two-strings/
function get_string_between( $string, $start, $end){
$string = " " . $string; // Convert to string.
$ini = strpos( $string, $start ); // Position of start text.
if ($ini == 0) {
return "";
}
$ini += strlen( $start ); // Length of start text.
@hearvox
hearvox / wp-media-lib-size-col.php
Created November 26, 2018 18:26
Add image data to new column to Media Library: file-size (bytes), image dimensions (pixels), and optional ratio between the two.
<?php
/* Add image data column to Media Library */
/**
* Add columns (file-size) to the Media Library list table.
*
* @param array $posts_columns Existing array of columns displayed in the Media list table.
* @return array Amended array of columns to be displayed in the Media list table.
*/
function my_media_columns( $posts_columns ) {
$posts_columns['size'] = __( 'Size', 'headecon' );
@hearvox
hearvox / wp-css-site-selector.php
Last active November 26, 2018 21:23
WordPress: Make CSS class name from site URL, e.g., to CSS select Stage and Dev sites
<?php
// Make CSS class name from site URL.
$urlparts = parse_url( site_url() );
$class_domain = 'site-' . str_replace( '.', '-', $urlparts [host] );
?>
<body <?php body_class( $class_domain ); ?>>
?>
/*
Example of adding CSS badge over site logo only at Staging site:
@hearvox
hearvox / wp_new_post-dates.php
Created July 19, 2018 16:45
WordPress: update post dates based on post meta value (used for radio series to change dates from rerun airdate to original airdate)
<?php
// Get category posts.
$query_args = array(
'category_name' => 'episode', // Name of category.
'posts_per_page' => 50, // Do in bathces to avoid overloading server.
'offset' => 0, // Skip previous batch by incrementing by 50 each time run.
);
$query = new WP_Query( $query_args );
@hearvox
hearvox / wp-excerpt-char-count.php
Last active May 7, 2019 01:31
Add a Character Counter to Excerpt box in Edit Post screen (with suggested range).
<?php
/**
* Add a Character Counter to Excerpt box in Edit Post screen.
*
* Includes suggested range.
* Char-count number is color red when outside range and green within.
*
* @link http://wpsites.org/add-character-counter-excerpt-box-10503/
*/
function my_excerpt_count_js() {
@hearvox
hearvox / wp_parent_page_family_list_shortcode.php
Last active March 25, 2018 15:30
WordPress shortcode to list pages based on top-parent of current page (or user-specified parent).
<?php
/* Shortcode to list page family tree, for current parent.
*
* Optional shortcode attributes: parent (ID), a depth, and title.
* [my_page_family_list parent="123" depth="2" title="My List Title"]
*
* @param array $atts Array of settings for wp_list_pages().
*
* @return string $list HTML list (nested) of page-family links.
*/