Skip to content

Instantly share code, notes, and snippets.

View laurelstreng's full-sized avatar

Laurel laurelstreng

View GitHub Profile
@laurelstreng
laurelstreng / functions.php
Created July 24, 2022 19:08
WordPress: Enabling custom image sizes for WordPress post content/blocks
// Add a custom image size to your theme, the usage is limited only to your theme
add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode
add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode
add_image_size( ‘slide-thumb’, 256, 148, true, array( ‘center’, ‘center’ ) ); // Cropped Center
// Make the custom image sizes available for the post author to use within the post content/blocks
function lstreng_custom_image_sizes( $size_names ) {
$new_sizes = array(
'homepage-thumb' => 'Homepage Thumbmail',
@laurelstreng
laurelstreng / functions.php
Created July 29, 2021 14:20
Custom function to extract the src from ACF oEmbed iframe. For use with Timber/Twig
<?php
/***
* Custom function to extract the src from ACF oEmbed iframe
* Example: fn('ls_extract_oembed_url', item.youtube_video)
**/
function ls_extract_oembed_url($string){
preg_match('/src="(.+?)"/', $string, $match);
$url = null;
if(isset($match[1])){
@laurelstreng
laurelstreng / functions.php
Last active September 3, 2020 15:50
Function to update all Gravity Forms 'State' dropdown field from full state name values to abbreviated state values.
<?php
/**
* Function to update all Gravity Forms 'State' dropdown field (based on field id #6), from full state name values to abbreviated state values if logged into the site.
* Use once, then remove the code.
*/
function lstreng_update_all_gravity_forms() {
// only load for admin panel
if ( is_admin() ) {
// load all forms
$forms = GFAPI::get_forms();
@laurelstreng
laurelstreng / functions.php
Last active July 22, 2020 14:11
If using the Wordpress author template to show user posts, change the main query via the pre_get_posts filter hook to show all types of posts by the author.
<?php
function lstreng_author_any_post_types $query ) {
// apply changes only for author archive page
if ( ! is_author() || ! $query->is_main_query() )
return;
$query->set('post_type', 'any');
}
add_action( 'pre_get_posts', 'lstreng_author_any_post_types' );
@laurelstreng
laurelstreng / deactivate-plugins.php
Created June 1, 2020 15:19
Wordpress mu-plugin to deactivate specific plugins on your local environment if defined in a wp-config-local.php file.
<?php
/*
Plugin Name: Deactivate Plugins Locally
Description: Deactivate specific plugins on your local environment if defined in a wp-config-local.php file.
Version: 1.0
*/
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
if ( defined( 'LOCAL_DISABLED_PLUGINS' ) ) { // If defined in wp-config-local.php
$plugins_to_disable = unserialize( LOCAL_DISABLED_PLUGINS );
@laurelstreng
laurelstreng / wordpress-query--postID-metaValue.php
Last active May 13, 2020 15:11
How to get Post ID by Meta Value
<?php
// Example: Query to show pages that have a meta_value that contains a string (not exact)
// https://rudrastyh.com/wordpress/meta_query.html
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'value' => '[contact-form-7 id="13127" title="Alliance',
'compare' => 'LIKE'
)
@laurelstreng
laurelstreng / template-query.php
Created February 24, 2020 16:27
Wordpress query to see what published pages are using a specific template.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-parts/full-width.php' // Update the value to the page template of your choosing.
)
@laurelstreng
laurelstreng / acf-select-ninja-forms.php
Last active June 23, 2024 11:48
Populate an ACF select field with created Ninja Forms & display the selected Ninja Form in template.
<?php
// https://www.advancedcustomfields.com/resources/acfload_field/
// Create your ACF select field & get it's key for use below.
add_filter('acf/load_field/key=field_5e4972aa1335c', function( $field ){
// Where new values will live
$field['choices'] = array();
// Dynamic data we want to populate with
// Ninja Forms - https://developer.ninjaforms.com/codex/settings/
// Get array of all forms
@laurelstreng
laurelstreng / ninja-forms-get-data.php
Created February 16, 2020 17:29
Get all ninja forms, each form ID and its title.
<?php
// Ninja Forms - https://developer.ninjaforms.com/codex/settings/
// Get array of all forms
$ninja_forms = Ninja_Forms()->form()->get_forms();
foreach ( $ninja_forms as $form ) {
// Get the form ID
$form_id = $form->get_id();
// Get the form Title
$form_name = $form->get_setting( 'title' );
@laurelstreng
laurelstreng / archive.php
Last active March 13, 2023 18:53
WordPress filtering content with multiple dropdowns, 1 taxonomy with different children, using ajax.
<?php
/**
* The template for displaying archive pages
*/
get_header();
global $wp_query;
?>
<form id="misha_filters" action="#">