Skip to content

Instantly share code, notes, and snippets.

View bookchiq's full-sized avatar

Sarah Lewis bookchiq

View GitHub Profile
@bookchiq
bookchiq / extra-pages.sql
Created March 21, 2024 00:58
Find duplicate, empty, non-Beaver Builder WordPress pages
SELECT wp1.ID, wp1.post_title, wp1.post_parent, parent_post.post_title AS parent_post_title
FROM `wp_posts` AS wp1
INNER JOIN `wp_posts` AS wp2 ON wp1.post_title = wp2.post_title AND wp1.ID != wp2.ID
LEFT JOIN `wp_posts` AS parent_post ON wp1.post_parent = parent_post.ID
WHERE wp1.post_type = "page"
AND wp1.post_status != "trash"
AND wp1.ID NOT IN (
SELECT DISTINCT post_id
FROM `wp_postmeta`
WHERE meta_key = "_fl_builder_draft" OR meta_key = "_fl_builder_data"
@bookchiq
bookchiq / list-siblings-and-children.php
Last active January 25, 2024 19:40
A shortcode to list the siblings and children of the current page (or any other hierarchical post type)
<?php
/**
* Add a shortcode to list the siblings and children of the current page.
* Usage: [list-siblings-and-children]
*
* @param array $atts Shortcode attributes.
* @return string The HTML code to render.
*/
function yoko_list_siblings_and_children( $atts ) {
global $post;
@bookchiq
bookchiq / retry-wp_remote_request.php
Created September 1, 2023 17:02
A wrapper for WordPress's wp_remote_request() that retries after common cURL errors with exponential backoff
<?php
/**
* Wrapper for wp_remote_request() with retry logic.
*
* @param string $url The URL to make the request to.
* @param array $args Optional. An array of HTTP request arguments.
* @param array $retry_curl_codes Optional. An array of cURL error codes to trigger a retry.
* @param int $max_retries Optional. The maximum number of retries.
* @return array|\WP_Error The response or WP_Error on failure.
@bookchiq
bookchiq / update_buddypress_avatar_from_external_url.php
Created June 5, 2023 17:46
Update a BuddyPress/BuddyBoss avatar from an external URL
<?php
/**
* Updates the avatar of a user based on the user's ID.
*
* This function downloads an image from an external URL and saves it
* as the user's avatar. If the image is not square, it crops it to a
* square shape before saving.
*
* @since 1.0.0
* @access public
@bookchiq
bookchiq / delete_orphaned_featured_image_meta.sql
Last active September 29, 2021 19:27
Sometimes when using the WordPress importer, featured images aren't _quite_ imported but report that they were, throwing off has_post_thumbnail() results. This SQL checks if the "connected featured image" actually exists, and if not, removes the half-there connection.
@bookchiq
bookchiq / make-shadow-metadata.php
Created September 17, 2021 20:28
ACF/WordPress action to help with sorting serialized custom fields by meta_value
<?php
/**
* ACF's serialized data can't be used for sorting posts,
* so we're making a more useful copy for the purpose.
*
* @param int|string $post_id The ID of the post being edited.
* @return void
*/
function happyhumans_save_plaintext_metadata( $post_id ) {
@bookchiq
bookchiq / bible-book-abbreviations.php
Created September 8, 2021 01:24
Bible books with their abbreviations
<?php
$bible_books = array(
'Genesis' => array(
'Gen',
'Gn',
'Ge',
),
'Exodus' => array(
'Exo',
@bookchiq
bookchiq / add-territories-to-gravity-forms.php
Created May 6, 2021 21:09
Add US Territories to the list of US States in Gravity Forms
<?php
/**
* Add US Territories to the list of US States in Gravity Forms.
*
* @param array $states The default list of states.
* @return array
*/
function filter_us_states( $states ) {
// Add American Samoa.
@bookchiq
bookchiq / wordpress_create_menu_from_page_structure.php
Last active April 28, 2021 17:20
One-time autogenerate a menu from WordPress page structure
<?php
/**
* Generate a menu with content and hierarchy that match the page structure.
* Trigger with ?autogenerate=true in the URL; note that it won't update a menu, so you'll need to delete and recreate if necessary.
*
* @return void
*/
function yoko_child_theme_create_menu_from_page_structure() {
$menu_name = 'Auto-generated menu from page structure';
@bookchiq
bookchiq / get-woocommerce-product-reviews-pro-transient.sql
Created March 17, 2021 17:12
Force WooCommerce Product Reviews Pro to recalculate the number of reviews
# The plugin relies on a transient for the review count, but it seems like it's not always current.
# See if there's a transient saved for post ID 1575 (then delete it to force the recalculation next time the count is displayed).
SELECT *
FROM `wp_options`
WHERE option_name = "_transient_wc_product_reviews_pro_review_count_1575";