Skip to content

Instantly share code, notes, and snippets.

View Inzman's full-sized avatar

Indrek Palm Inzman

  • Tartu, Estonia
View GitHub Profile
@Inzman
Inzman / gist:a97a18cf44a5dfe28f6a7d0c1997999d
Created June 11, 2020 06:24
Filter Woocommerce short description
// Remove empty paragraphs
function filter_woocommerce_short_description2( $post_post_excerpt ) {
$post_post_excerpt = str_replace("<p></p>","",$post_post_excerpt);
$post_post_excerpt = str_replace("<p>&nbsp;</p>","",$post_post_excerpt);
return $post_post_excerpt;
};
add_filter('woocommerce_short_description', 'filter_woocommerce_short_description2', 10, 1);
@Inzman
Inzman / gist:b47e2d5fd2d632876c9c290290245503
Created April 24, 2020 11:04
If content is really empty
function empty_content($str) {
return trim(str_replace('&nbsp;','',strip_tags($str))) == '';
}
@Inzman
Inzman / gist:a5bab79c7257dee00caf447a3c08817b
Created February 28, 2020 10:23
Generate random string
// https://stackoverflow.com/questions/4356289/php-random-string-generator
function generateRandomString($length = 10) {
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
echo generateRandomString(); // OR: generateRandomString(24)
@Inzman
Inzman / gist:525ad6512dbf64c6aa7bcfcc2915f041
Created February 10, 2020 11:31
Sort woocommerce order items alphabetically
/*
* Filters the $order->get_items() method results to order all line items by
* product name
*/
add_filter( 'woocommerce_order_get_items', function( $items, $order ) {
uasort( $items,
function( $a, $b ) {
return strnatcmp( $a['name'], $b['name'] );
}
@Inzman
Inzman / gist:781c6496b1167df8b567db7cadf61fc8
Created January 9, 2020 15:25
Wordpress - Delete auto-drafts
if (is_admin()){
if ( !get_option('db_last_auto_cnl') || (get_option('db_last_auto_cnl') < time()- 86400) ) {
global $wpdb;
$del= $wpdb->query("DELETE FROM $wpdb->posts WHERE post_status = 'auto-draft'");
$del= $wpdb->query("DELETE FROM $wpdb->posts WHERE post_type = 'revision'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_rss%'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_rss_%'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_feed_%'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_feed_%'");
//if you want, you can uncomment these too
@Inzman
Inzman / gist:b515afaff3dbe55d52d2ef5ba38fe99a
Created January 8, 2020 07:58
Get last segment path in URL
function getLastPathSegment($url) {
$path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
$pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
$pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash
if (substr($path, -1) !== '/') {
array_pop($pathTokens);
}
return end($pathTokens); // get the last segment
}
@Inzman
Inzman / gist:00983bd2385710767b17e04716195663
Created November 20, 2019 12:22
This code rewrites the slug of the attachment upon uploading to add a prefix (or suffix) to the slug. In that case you have less chance of getting in the way of page slugs in the future.
add_action('add_attachment', function($postId) {
// get the attachment post object
$attachment = get_post($postId);
// get the slug for the attachment
$slug = $attachment->post_name;
// update the post data of the attachment with an edited slug
wp_update_post(array(
'ID' => $postId,
'post_name' => 'media-'.$slug, //adds a prefix
//'post_name' => $slug.'-photo', //adds a suffix
@Inzman
Inzman / gist:b41a53072ec41eda091cca7ab7327ada
Created October 17, 2019 13:37
Hierarchical terms list
/**
* Recursively sort an array of taxonomy terms hierarchically. Child categories will be
* placed under a 'children' member of their parent term.
* @param Array $cats taxonomy term objects to sort
* @param integer $parentId the current parent ID to put them in
*/
function sort_terms_hierarchicaly(Array $cats, $parentId = 0)
{
$into = [];
foreach ($cats as $i => $cat) {
@Inzman
Inzman / gist:97a1ab062b97e4c487674043613f485b
Created September 20, 2019 06:17
Woocommerce get variation product price in select
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$id = $product->get_id();
@Inzman
Inzman / gist:8abd6ee4656f0fc4068448c194f08190
Last active September 10, 2019 12:19
Get Wordpress post terms
// wp_get_object_terms() always queries the database
/* If you’re looping over WP_Query results, you should prefer get_the_terms() instead.
It’s pretty much the same for most use cases, but it uses the object cache,
which by default gets populated with the terms for the posts matching your query — unless you specifically
set update_post_term_cache as false when instantiating WP_Query.
*/
// Get terms for post
if ( ! function_exists( 'storefront_post_terms' ) ) {
function storefront_post_terms(){