Skip to content

Instantly share code, notes, and snippets.

@vovadocent
vovadocent / natural_sort_alphanums.php
Created June 28, 2023 10:40
WP Query order by alphanumeric meta value
<?php
function orderbyReplace($orderby)
{
global $wpdb;
// use this if you have stable literal prefixes (e.g. af102, af56, af2569)
$new = str_replace($wpdb->prefix . 'postmeta.meta_value ASC', 'LENGTH(mt1.meta_value) ASC, mt1.meta_value ASC', $orderby);
// use this if you have something like (102bch, 56dbg, 2569, 12ea)
$new = str_replace($wpdb->prefix . 'postmeta.meta_value ASC', 'CAST(mt1.meta_value as UNSIGNED) ASC, mt1.meta_value ASC', $orderby);
return $new;
}
@vovadocent
vovadocent / cpt.php
Last active February 10, 2023 16:08
WP Register custom post types/taxonomies from arrays
<?php
/**
* Custom Post Types and Taxonomies
*/
function vd_register_custom_post_type()
{
$post_types = [
'faq' => [
'name' => 'FAQ',
@vovadocent
vovadocent / remove_nofollow_attribute_from_internal_links_comment.php
Last active June 11, 2021 08:59
Remove rel="nofollow" attribute from internal links into comment text
@vovadocent
vovadocent / image_bg_color_nav_menu_item_ACF.php
Created September 20, 2018 14:27
Add background-image or background-color to nav menu item using ACF
<?php
//add background-image or background-color to nav menu item using ACF
add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
function new_nav_menu_items($items, $args) {
preg_match_all('/menu-item-([0-9]{1,10})"/ ', $items, $matches);
if (isset($matches[0]) && isset($matches[1])) {
foreach ($matches[0] as $k => $repl) {
$post_id = $matches[1][$k];
@vovadocent
vovadocent / reorder_woocommerce_attribute_taxonomies.php
Last active June 11, 2021 09:03
Reorder Woocommerce Attribute Taxonomies
<?php
// reorder woocommerce attribute taxonomies
function css_woocommerce_attribute_taxonomies( $taxonomies ) {
$out = array();
foreach($taxonomies as $t){
$out[$t->attribute_id] = $t;
}
ksort($out);
return $out;
};
@vovadocent
vovadocent / getTaxTermsByOtherTaxTerm.php
Last active October 4, 2017 14:00
Get Taxonomy Terms By Other Taxonomy Term
<?php
function getTaxTermsByOtherTaxTerm($taxonomy_args, $out_taxonomy, $post_type = NULL) {
global $wpdb;
$tax_q = $taxonomy_ids = array();
$post_type_q = !empty($post_type_q) ? "AND p.post_type = '$post_type'" : "";
foreach ($taxonomy_args as $tax => $term_id) {
$sql = "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt
INNER JOIN $wpdb->term_taxonomy t ON (t.term_id = tt.term_id AND tt.taxonomy = '$tax' AND t.term_id = $term_id)";
$taxonomy_ids = array_merge( $taxonomy_ids, $wpdb->get_col($sql) );
@vovadocent
vovadocent / specialWholesaleTaxClass.php
Created August 28, 2017 09:42
Woocommerce Apply Different Tax Class Rate Based On User Role
<?php
function specialWholesaleTaxClass( $tax_class, $product ) {
global $current_user;
if (in_array('wholesale_customer', $current_user->roles))
$tax_class = 'Special Wholesale';
else
$tax_class = 'Zero Rate';
return $tax_class;
@vovadocent
vovadocent / wp_order_posttype_by_taxonomy.php
Last active July 18, 2017 09:20
WP order post type by taxonomy name
<?php
//$oargs = array('orderby' => 'post__in', 'post__in' => get_order_tax_ids('events', 'region'));
//$args = array_merge($args, $oargs);
function get_order_tax_ids($post_type, $taxonomy) {
global $wpdb;
$ids = [];
$sql = "SELECT p.ID FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON (tr.object_id = p.ID)
@vovadocent
vovadocent / fast_image_download.php
Created July 6, 2017 12:03
Fast file download from remote url, using CURL
<?php
//$imurl - віддалений url файла
//$picpath - path до локального файла з новим іменем
function save_image($imurl, $picpath) {
$ch = curl_init();
$fp = fopen($picpath, 'w');
$ch = curl_init($imurl);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
@vovadocent
vovadocent / custom_slug_and_category_slugs_in_product_url.php
Last active June 26, 2017 13:50
Custom slug and category slugs in product url.php
<?php
/* filter to get "services" in product categories, child categories, and singe product pages correctly */
// /services/%product_cat%/ - Product base permalink settings
// services - Product Category base permalink settings
add_filter('rewrite_rules_array', function( $rules ) {
$new_rules = array(
'services/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
'services/([^/]*?)/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[2]&paged=$matches[3]',
'services/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]',