Skip to content

Instantly share code, notes, and snippets.

View petenelson's full-sized avatar

Pete Nelson petenelson

View GitHub Profile
@petenelson
petenelson / get-pagination-counts.php
Last active May 6, 2024 17:07
WordPress: get_pagination_counts()
<?php
/**
* Gets pagination counts for a query.
*
* @param WP_Query $query The query, defaults to global wp_query.
* @return array
*/
function get_pagination_counts( $query = false ) {
global $wp_query;
@petenelson
petenelson / get-term-ids.php
Last active March 28, 2024 19:45
WordPress: Get term IDs, creates terms if they don't exist
/**
* Gets the term IDs for the term names. Creates the terms if they do not exist.
* TODO update the project_name_term_id_created filter when you copy/paste this function.
*
* @param string $term_names The term name (or names separated with pipes).
* @param string $taxonomy The taxonomy.
* @param string $by Get term by 'name' or 'slug'?
* @param bool $create Create the term if it doesn't exist?
* @return array
*/
@petenelson
petenelson / wp-multi-level-parse-args.php
Created March 28, 2024 19:43
WordPress Multi Level Parse Args
<?php
/**
* wp_parse_args() with support for multi-level arrays.
*
* @param array $a Arrays to be parsed
* @param array $b Defaults for the arrays.
* @return array
*/
function multi_level_wp_parse_args( &$a, $b ) {
@petenelson
petenelson / _tribe_blocks_recurrence_rules.json
Last active March 4, 2024 21:04
Sample _tribe_blocks_recurrence_rules for The Event Calendar
[
{
"type": "weekly",
"all_day": false,
"multi_day": false,
"start_date": "2024-03-11",
"_start_date_input": "March 11, 2024",
"_start_date_obj": "2024-03-11T05:00:00.000Z",
"start_time": "08:00:00",
"_start_time_input": "8:00 am",
@petenelson
petenelson / object-cache.php
Last active February 21, 2024 22:40
object-cache.php for memcached
<?php
/**
* Adds a value to cache.
*
* If the specified key already exists, the value is not stored and the function
* returns false.
*
* @link http://www.php.net/manual/en/memcached.add.php
*
* @param string $key The key under which to store the value.
@petenelson
petenelson / activate-memcached.sh
Created February 21, 2024 22:38 — forked from strategio/activate-memcached.sh
Setup memcached in a Local by Flywheel site
#!/usr/bin/env bash
PHP_VERSION=$1
CONF_FOLDER="/usr/local/etc/php/${PHP_VERSION}/"
PHP_INI="${CONF_FOLDER}php.ini"
cd ${CONF_FOLDER}
git clone --depth 1 https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached
/usr/local/bin/phpize
@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 / output-css-classes.php
Last active February 7, 2024 15:30
WordPress: Output CSS classes
/**
* Outputs a list of sanitized CSS class names.
*
* @param array|string $classes List of class names (array or string with
* class names separated by spaces or commas).
* @param bool $echo Echo the list of class names (defaults to true).
* @return void|array
*/
function output_css_classes( $classes, $echo = true ) {
@petenelson
petenelson / custom-permalink-from-meta.php
Created November 5, 2012 21:32
WordPress permalink for custom post type based on post meta
@petenelson
petenelson / ssh-mysql-tunnel-test.php
Created July 13, 2017 16:10
WordPress: MySQL PDO command via SSH tunnel
<?php
/**
* Plugin Name: SSH Remote Test
*/
add_action( 'admin_init', 'ssh_remote_test', 1 );
function ssh_remote_test() {