Skip to content

Instantly share code, notes, and snippets.

View RadGH's full-sized avatar

Radley Sustaire RadGH

View GitHub Profile
@RadGH
RadGH / wc-filter-products-on-orders-screen.php
Created February 22, 2019 07:40
WooCommerce: Filter by purchased products on orders screen
<?php
/**
* Allow sorting orders screen by purchased product
* Modified version of https://github.com/wp-plugins/woocommerce-filter-orders-by-product/blob/master/woocommerce-filter-orders-by-product.php
*/
if ( function_exists('WC') && is_admin() ):
add_action( 'restrict_manage_posts', 'aa_product_filter_in_order', 50 );
add_filter( 'posts_where' , 'aa_product_filter_where' );
endif;
@RadGH
RadGH / wc-coaches-menu-tab.php
Last active February 28, 2020 12:26
Add custom woocommerce account page/tab with title, content, and endpoint slug
<?php
if ( !defined( 'ABSPATH' ) ) exit;
// The "endpoint" key used to identify our custom menu.
// If you are adding multiple pages to the account menu, you would want to change this to an array or just get rid of the constant altogether..
define( 'MBC_PROFILE_ENDPOINT', 'coach-profile' );
/**
* Add the Coach Profile link to the list of menu items in the dashboard.
@RadGH
RadGH / wp-restrict-media.php
Last active February 19, 2019 03:14
WordPress - Restrict media uploader to uploads by the current user
<?php
/*
If the user does not have permission to edit pages, restrict the media uploader to only show their own uploads.
*/
function rs_restrict_media_attachments_to_original_author( $media_query = array() ) {
if ( !current_user_can( 'edit_pages' ) ) {
$media_query['author'] = get_current_user_id();
}
@RadGH
RadGH / divi-add-instagram-icon-to-options.php
Last active February 5, 2019 07:58
Divi: Add instagram icon to options
<?php
/*
How to add Instagram (or other social media icons) to Divi's theme options.
Step 1)
Add the following function to your child theme's functions.php.
This will add Instagram icon below the Google icon in the theme options.
You can copy/paste the icon/url fields here to add more social networks.
*/
@RadGH
RadGH / gf-get-user-id-of-entry.php
Last active November 13, 2018 05:54
Gravity Forms: Get the user who created an entry, or the user created by GF Registration Addon
<?php
/**
* Return the user_id who submitted an entry.
* If the user was not logged in, but created an account
* through the form, returns the created user ID instead.
*
* @param $entry_id
*
* @return int|false User ID or FALSE
@RadGH
RadGH / custom-page-template-in-plugin.php
Last active August 26, 2022 21:00
Add custom page template to WordPress through a plugin
<?php
/*
This code will let you have a page template in your plugin, and show that template in the "Page Templates" dropdown.
It can also work for other post type templates, just change the filter "theme_page_templates" as needed.
WP 4.7 only.
Setup:
1. Replace "cppt" with your own plugin prefix.
2. Replace custom_plugin_page_template.php with whatever made-up template name you want.
3. Replace templates/custom-template.php with the real template (and directory) you want to load.
@RadGH
RadGH / full-width-shortcode-for-wordpress.php
Created August 16, 2018 21:29
Full width shortcode for wordpress
<?php
// CSS is also needed, seen below. replace $breakpoint_medium with pixel value if necessary
// [full_width](your content)[/full_width]
function rs_shortcode_screen( $atts, $content = '' ) {
$type = isset($atts['type']) ? $atts['type'] : false;
if ( $type !== 'desktop' && $type !== 'mobile' ) {
if ( current_user_can('edit_posts') ) {
return '<strong>Shortcode "screen" error:</strong> You must provide the option type with a value of either desktop or mobile. For example, [screen type="desktop"]<em>your content</em>[/screen].';
@RadGH
RadGH / woocommerce-add-box-sizes-script.js
Last active August 14, 2018 20:09
JS script to add multiple UPS box sizes to WooCommerce shipping zones settings page automatically
// Run this in your JS console on the UPS settings page for a each shipping zone
var boxes = [{}]; // Your box data here. See: https://docs.google.com/spreadsheets/d/1S9kjnYRSq6DMRi1w_TVYOr-Ss7i1_ATJUarRt9Go-t8/edit?usp=sharing
var $packing = jQuery('#packing_options');
var $add_box_button = $packing.find('.button.insert');
var $box_list = jQuery('#rates');
var currentbox = 0;
var addBoxInterval = false;
@RadGH
RadGH / wc-exclude-outofstock-query.php
Last active July 24, 2022 10:17
Exclude out of stock and hidden products from WooCommerce query with pre_get_posts (plus equivalent filter for Ajax Load More)
<?php
/*
Solution based on a Stack Overflow answer by patrickzdb:
https://stackoverflow.com/a/24514985/470480
Tax query solution for hidden products based on a WordPress Stack Exchange answer by kalle:
https://wordpress.stackexchange.com/a/262628/19105
Please note:
This file includes two functions/filters.
@RadGH
RadGH / pagination-numbers.php
Created June 8, 2018 08:01
Create page numbers for pagination from current page number and total number of pages
<?php
// EXAMPLE USE IN COMMENTS
/**
* Convert current page number and the total number of pages into a number range for pagination.
* Use $edge_number_count to indicate how many numbers should appear to the left or right of the current page.
* If the start or end page is one off of the start or end of the entire set, the range is extended. This prevents page 2 from being the start page, and page N-1 from the end page.
*
* @param $current_page