Skip to content

Instantly share code, notes, and snippets.

View mtruitt's full-sized avatar

Mark Truitt mtruitt

  • Maryland
View GitHub Profile
@mtruitt
mtruitt / README-Template.md
Created May 3, 2024 13:48 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@mtruitt
mtruitt / acf_get_field.php
Created April 20, 2024 15:10
A custom function designed to safely retrieve field values from the Advanced Custom Fields (ACF) plugin, ensuring seamless operation and preventing fatal errors in case of plugin deactivation. Additionally, it includes built-in functionality to display a site notice, alerting administrators if the ACF plugin is disabled.
<?php
/**
* Retrieves a custom field value using Advanced Custom Fields (ACF) plugin.
*
* @param string $field_name The name of the ACF field.
* @param array $options Optional. An array of options to pass to the `get_field()` function. Default is an empty array.
* @return mixed The value of the ACF field, or an empty string if ACF plugin is disabled.
*/
function custom_get_acf_field($field_name, $options = '') {
if ( function_exists('get_field') ) {
@mtruitt
mtruitt / yoast.php
Created October 15, 2019 14:46
Override Yoast breadcrumb links
<?php
add_filter( 'wpseo_breadcrumb_links', 'override_yoast_breadcrumb_links' );
function override_yoast_breadcrumb_links( $links ) {
global $post;
if ( is_singular( 'post' ) ) {
$breadcrumb[] = array(
'url' => get_permalink( get_option( 'page_for_posts' ) ),
'text' => 'Blog',
@mtruitt
mtruitt / wpdb_example.php
Last active October 15, 2019 14:44
Used to strip CC/Bank information from a Formidable form error where it was getting stored.
<?php
add_action('wp_head' , 'remove_cc_information');
function remove_cc_information() {
global $wpdb;
$db_table_name = $wpdb->prefix . 'frm_item_metas';
$results = $wpdb->get_results("SELECT id, meta_value FROM {$db_table_name} WHERE field_id = '83'", ARRAY_A);
// Loops through and unserializes data to strip out CC/CVC numbers then updates the DB
foreach ( $results as $result) {
@mtruitt
mtruitt / widgets.php
Last active June 26, 2019 13:31
Allow WordPress editor to access widgets
<?php
// Elevate caps since theme options are required to access widgets
$role = get_role('shop_manager');
$role->add_cap('edit_theme_options');
// Allow Editor widget access but remove themes, menu options and customizer.
function custom_admin_menu() {
$user = new WP_User(get_current_user_id());
@mtruitt
mtruitt / update_postmeta_for_products.php
Created April 25, 2019 18:07
Example of how to update postmeta on a new product.
<?php
function add_this_to_new_products( $product_get_id ) {
$productType = WC_Product_Factory::get_product_type( $product_get_id);
if( $productType == 'registrations') {
update_post_meta( $product_get_id, '_tax_status', 'none' );
}
}
<?php
function deleteProduct($id, $force = FALSE) {
$product = wc_get_product($id);
if(empty($product))
return new WP_Error(999, sprintf(__('No %s is associated with #%d', 'woocommerce'), 'product', $id));
// If we're forcing, then delete permanently.
if ($force) {
if ($product->is_type('variable')) {
@mtruitt
mtruitt / delete_variations_based_on_date.php
Last active August 19, 2019 04:48
Delete Variations from Products
<?php
function delete_variations(){
// Lets grab the product type
$product_id = get_the_ID();
$product_type = WC_Product_Factory::get_product_type( $product_id );
// Lets skip everything if its not a product.
if( $product_type == false ) return;
// Lets make sure we are working with registrations
@mtruitt
mtruitt / variation_attribute_options_html.php
Created April 25, 2019 17:59
Alphabetical order for variation dropdowns
<?php
add_filter('woocommerce_dropdown_variation_attribute_options_html', function ($html) {
$xml = simplexml_load_string($html);
$select = array('id' => (string)$xml->attributes()['id'],
'name' => (string)$xml->attributes()['name'],
'attribute_name' => (string)$xml->attributes()['data-attribute_name'],
'show_option_none' => (string)$xml->attributes()['data-show_option_none']);
$options = array();
@mtruitt
mtruitt / disable_gateways.php
Last active February 21, 2019 17:22
Woocommerce - Disable/allow payment gateways based on user role.
<?php
// Filter to remove/add payment gateways at checkout
add_filter( 'woocommerce_available_payment_gateways', 'mt_disable_gateways' );
function mt_disable_gateways( $available_gateways ) {
// Get current user
$user = wp_get_current_user();
// Array to handle roles you want to allow this for.
$allowed_roles = array( 'administrator');