Skip to content

Instantly share code, notes, and snippets.

View undfine's full-sized avatar

Dustin W undfine

  • Wilmington, NC
View GitHub Profile
@undfine
undfine / wp_clear_cache_for_property_archive.php
Created October 9, 2024 19:54
Force Wordpress to update (and clear cache) on certain posts when other posts are updated
/** Force another page or post (like an archive) to update when any post (of post-type) is saved
* @param Hook save_post_{$post_type}
* @param Callback
*/
add_action('save_post_property', 'clear_cache_for_property_archive', 10, 2);
function clear_cache_for_property_archive($post_id, $post){
$pages_to_clear = [87,91]; // list of page/post ids that should be updated
foreach($pages_to_clear as $page_id){
$page = get_post($page_id, 'OBJECT');
@undfine
undfine / findStringFromArray.js
Created October 2, 2024 17:27
Different ways of checking if an item from an array exists within a string
// Iterates over an array and returns true if any are found
function itemInStr(str, arr) {
return arr.some(item => str.includes(item));
}
// uses Regex to test if a string is in the array, returns true if found
function testItemInStr(str,arr){
let regex = new RegExp(arr.join("|"), "i");
return regex.test(str);
}
@undfine
undfine / filter_featured_img_by_taxonomy.php
Created September 24, 2024 20:05
Filters the output of the featured image for a custom post type, based on a taxonomy with a custom image field.
@undfine
undfine / CustomUploadsHandler.php
Created September 23, 2024 20:40
Uploads Handler for Wordpress to alter the directory based on Post type and/or Filetype (extension)
<?php
/**
* Filters the uploads directory to use a custom structure for select post types
*/
class Custom_Uploads_Handler {
public function __construct() {
// initialize upload filters
@undfine
undfine / get_post_id_by_slug.php
Created September 5, 2024 21:17
Wordpress function to get Post ID from slug
function get_post_id_by_slug( $post_name, $post_type = 'post' ) {
global $wpdb;
// Prepare the SQL query to retrieve the post ID by post_name and post_type
$query = $wpdb->prepare( "
SELECT ID
FROM $wpdb->posts
WHERE post_name = %s
AND post_type = %s
AND post_status = 'publish'
@undfine
undfine / hubspot-form-a11y-polyfill.js
Created August 14, 2024 17:23 — forked from derekcavaliero/hubspot-form-a11y-polyfill.js
HubSpot Embedded Form Accessibility Polyfills
/**
* HubSpot Embedded Form Accessibility Pollyfills
*
* This script fixes the HubSpot HTML blunders that make their embedded forms inaccessible for assistive technology.
* - Replaces/removes improper use of <fieldset>, <legend>, <label>, and role attributes.
* - Note - this can cause forms configured for mulitple column field layouts to break - you will need to adjust your CSS accordingly.
**/
hubspotFormA11y = {
@undfine
undfine / arrayToObject.js
Created August 10, 2024 16:41
Convert array of objects to a single object
function arrayToObject(arr) {
return arr.reduce((acc, curr) => {
acc[curr.name] = curr.value;
return acc;
}, {});
}
// Example usage:
const inputArray = [
{ name: "firstName", value: "John" },
@undfine
undfine / elementor_password_form_page.php
Created June 6, 2024 23:13
Custom Password Protected page with Elementor Template
function elementor_password_form_page() {
// Elementor Template ID
$template_id = 3785; // Replace with your Elementor template ID
// Check if Elementor is active and the template exists
if (defined('ELEMENTOR_VERSION') && \Elementor\Plugin::$instance->templates_manager->get_source('local')->get_item($template_id)) {
// Render the Elementor template
return \Elementor\Plugin::$instance->frontend->get_builder_content_for_display($template_id, true);
}
@undfine
undfine / observeElementAddedToDOM.js
Created April 30, 2024 18:02
JS - Observer for Elements added to the DOM
function observeElementAddedToDOM(elementId, callback) {
// Select the target node
var targetNode = document;
// Options for the observer (which mutations to observe)
var config = { childList: true, subtree: true };
// Callback function to execute when mutations are observed
var observerCallback = function(mutationsList, observer) {
for(var mutation of mutationsList) {
@undfine
undfine / pods_orderby_meta_value_num.php
Last active April 19, 2024 14:55 — forked from logoscreative/functions.php
Pods Shortcode orderby 'meta_value_num'
<?php
/**
* WordPress stores all meta fields as strings, which causes problems for ordering by numbers. When using Pods::find() directly, this issue can be addressed by casting the field as a decimal, as shown here: https://github.com/pods-framework/pods-code-library/blob/master/example/classes/Pods/find/examples/orderby-number.php
*
* In shortcodes, this strategy is not possible, as MySQL functions can not be used in the WordPress post editor. Instead, you can use the "pods_shortcode_findrecords_params" params filter, as shown below:
*/
/**
* Example to order by a price field properly.
*/