Skip to content

Instantly share code, notes, and snippets.

View renventura's full-sized avatar

Ren Ventura renventura

View GitHub Profile
@renventura
renventura / js-usd-format.js
Created April 26, 2024 11:23
Javascript code to format a number to USD currency
const formatUSD = (value) => {
return new Intl.NumberFormat(
'en-US',
{
style: 'currency',
currency: 'USD'
}
).format(value);
}
@renventura
renventura / manually-call-wp_maybe_auto_update.php
Created March 6, 2024 15:13
Avoid deactivated plugins when manually calling wp_maybe_auto_update() function
<?php
/**
* Manually calling wp_maybe_auto_update() results in plugins getting deactivated during the update process.
* WP assumes this function is run during cron, and it neither deactivates nor reactivates plugins during cron.
* When run outside of cron, it will deactivate plugins, but it assumes the browser will refresh and reactivate them.
* To prevent deactivated plugins when calling this function, we can trick the upgrader into thinking it's called during cron.
*
* @link https://stackoverflow.com/questions/41541968/plugins-are-getting-deactivated-after-automatic-plugin-update-in-wordpress
*/
@renventura
renventura / wp-create
Created July 29, 2022 17:08
Bash scripts for creating and deleting local WordPress installations (using Laravel Valet)
#!/bin/bash
# Colors
VP_NONE='\033[00m'
VP_RED='\033[01;31m'
VP_GREEN='\033[01;32m'
VP_YELLOW='\033[01;33m'
VP_PURPLE='\033[01;35m'
VP_CYAN='\033[01;36m'
VP_WHITE='\033[01;37m'
@renventura
renventura / mp-delete-user-transaction-failed.php
Created October 4, 2021 13:38
MemberPress user cleanup when transaction fails
<?php
add_action( 'mepr-txn-status-failed', 'my_custom_cleanup_memberpress_txn_failed', 15 );
/**
* Runs a "cleanup" when a MemberPress transaction fails.
* This checks the transaction's user to see if they have any completed transactions.
* If the user only has incomplete transactions, it deletes the user.
*
* @param object $txn MeprTransaction object
*
@renventura
renventura / gravity-forms-block-spam-words.php
Created April 22, 2021 12:59
Prevent Gravity Forms entries and notifications that contain spammy words (e.g. spam)
<?php
add_action( 'gform_pre_send_email', 'keywords_check' );
/**
* Check the message field of the entry for bad words.
* If any are detected, silently discard the email and entry.
*
* @param array $email Email data
* @param string $format Email format (text or HTML)
* @param array $notification Notificaiton data
@renventura
renventura / rest-authors-query.php
Created August 28, 2020 16:12
Modifies the "authors" included in the REST response for /wp/v2/users/?who=authors
<?php
add_filter( 'rest_user_query', 'my_custom_rest_authors', 25, 2 );
/**
* Modifies the "authors" included in the REST response for /wp/v2/users/?who=authors
*
* @param array $args Query args
* @param object $request REST request
*
* @return array
@renventura
renventura / woocommerce-custom-redirects-redirect_url.php
Created May 19, 2020 20:46
Filters the Custom Redirects for WooCommerce redirect URL (requires http://wc-redirects.com/)
<?php
add_filter( 'woocommerce_custom_redirects_redirect_url', 'wcr_filter_redirect' );
/**
* Filters the custom redirect URL.
* Requires Custom Redirects for WooCommerce.
* @link http://wc-redirects.com/
*
* @param string $redirect Redirect URL
* @param string $type Redirect type (add-to-cart or after-purchase)
@renventura
renventura / wpforms-programmatically-create-entry.php
Created May 3, 2020 15:50
Snippet for programmatically creating entries in WPForms.
<?php
// Create entry.
$entry_id = wpforms()->entry->add( array(
'form_id' => absint( $form_id ),
'user_id' => absint( $user_id ),
'fields' => wp_json_encode( $fields ),
'ip_address' => sanitize_text_field( $user_ip ),
'user_agent' => sanitize_text_field( $user_agent ),
'date' => $date,
@renventura
renventura / members-add-custom-capability-label.php
Created April 28, 2020 13:57
Registers a custom capability in Members.
<?php
add_action( 'members_register_caps', 'cp_members_register_caps' );
/**
* Register a custom capability in Members.
*
* @return void
*/
function cp_members_register_caps() {
members_register_cap( 'capability_key', array( 'label' => 'Capability Label' ) );