Skip to content

Instantly share code, notes, and snippets.

View faurelia's full-sized avatar

F aur faurelia

View GitHub Profile
@faurelia
faurelia / functions.php
Created August 17, 2021 10:17
WordPress: Cache DB Result
<?php
function get_book_detail( $book_id ) {
$cache_key = 'book_' . $book_id;
$data = get_transient( $cache_key );
if ( false === $data ) {
$data = get_post( $book_id );
@faurelia
faurelia / functions.php
Created August 17, 2021 10:13
WordPress: Clear Transient on Save Post
<?php
add_action('save_post_{post_type}', 'fn_delete_transient', 10, 3);
function fn_delete_transient( $post_id, $post, $update ) {
delete_transient( 'cache_key' . $post_id );
}
@faurelia
faurelia / functions.php
Last active August 10, 2021 06:39
WordPress: Change href and other attributes of nav menu with filters
<?php
// check for role
function wp_check_user_role($role_slug) {
$user = wp_get_current_user();
return is_user_logged_in() && isset( $user->roles ) && is_array( $user->roles ) && in_array( $role_slug, $user->roles );
}
// modify the url or title
@faurelia
faurelia / functions.php
Created March 10, 2021 08:20
Woocommerce: Show Cross Sells in Product Details
<?php
add_action('woocommerce_product_meta_end', 'show_cross_sell_in_single_product', 30);
function show_cross_sell_in_single_product(){
$crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true);
if(empty($crosssells)){
return;
@faurelia
faurelia / functions.php
Created March 10, 2021 08:17
Woocommerce: Change Billing details label to Personal Information on Checkout
<?php
// change Billing details to Personal Information
function wc_billing_field_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Billing details' :
$translated_text = __( 'Personal Information', 'woocommerce' );
break;
}
return $translated_text;
@faurelia
faurelia / functions.php
Created March 10, 2021 08:12
Woocommerce: Hide coupon field on cart
<?php
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
function hide_coupon_field_on_cart( $enabled ) {
if ( is_cart() ) {
$enabled = false;
}
@faurelia
faurelia / functions.php
Created March 10, 2021 06:15
Woocommerce: Add New Order Status
<?php
add_action( 'init', 'register_awaiting_shipment_order_status' );
function register_awaiting_shipment_order_status() {
register_post_status( 'wc-awaiting-shipment', array(
'label' => _x('Ready for Delivery', 'Order status', 'woocommerce'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
@faurelia
faurelia / functions.php
Created March 10, 2021 06:12
WooCommerce: Enable roles to access Invoice, Packing Slips & more
<?php
/**
* Required plugin:
* https://wordpress.org/plugins/print-invoices-packing-slip-labels-for-woocommerce/
*/
add_filter('wf_pklist_alter_admin_print_role_access','wt_pklist_add_print_role');
function wt_pklist_add_print_role($role_arr)
{
@faurelia
faurelia / functions.php
Created March 10, 2021 06:02
Woocommerce: Add Payment Method on Admin Orders List
<?php
// Add a new custom column to admin order list
add_filter( 'manage_edit-shop_order_columns', 'add_payment_shop_order_column',11);
function add_payment_shop_order_column($columns) {
$reordered_columns = array();
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_total' ){
@faurelia
faurelia / functions.php
Created November 7, 2020 07:01
WordPress: Translate Strings with TranslatePress Programatically
<?php
$trp = TRP_Translate_Press::get_trp_instance();
$render = $trp->get_component( 'translation_render' );
$setting = $trp->get_component( 'settings' );
// List of strings to translate
$strings = array(
'home' => 'Home',
'about' => 'About Us',