Skip to content

Instantly share code, notes, and snippets.

@mahdi-alavi
mahdi-alavi / gist:13e5022f3c0386e180e2f9f154734bc8
Last active October 30, 2018 19:36
wordpress: custom pagination with wpdb get_results
<?php
global $wpdb;
$sql = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
@mahdi-alavi
mahdi-alavi / gist:f41ead3ba3b7353a8d5b84a44b873825
Created October 18, 2018 18:09
wordpress: Bootstrap 4.1 pagination
/* Bootstrap pagination.
=========================================================================== */
function itl_paging_nav() {
global $wp_query, $wp_rewrite;
// Setting up default values based on the current URL.
$pagenum_link = html_entity_decode( get_pagenum_link() );
$url_parts = explode( '?', $pagenum_link );
// Get max pages and current page out of the current query, if available.
@mahdi-alavi
mahdi-alavi / gist:8081f2f322e4cec7aedb69f24cfc0537
Last active October 1, 2018 06:33
Trello RTL style for stylebot
/*
* install stylebot from Chrome Webstore: https://chrome.google.com/extensions/detail/oiaejidbmkiecgbjeifoejpgmdaleoha
* import this style from backup (Stylebot Options)
*/
{{"https://trello.com/b/":{"_enabled":true,"_rules":{"at1":{"at":true,"text":"@font-face {\n \n font-family: 'vazir-font';\n \n font-weight: normal;\n \n font-style: normal;\n src: url('https://cdn.rawgit.com/rastikerdar/vazir-font/v19.0.0/dist/Vazir.woff') format('woff');\n \n font-display: swap;\n}","type":"@font-face"},"#board":{"padding":"0 15px"},"body":{"background":"#216f95"},"body, button, html, input, select, textarea":{"font-family":"'vazir-font', 'Segoe UI', sans-serif","font-size":"16px"},".window-header:after, .window-module-title:after":{"display":"block","content":"''","clear":"both"},"#board, textarea, .checklist-item-row, .card-detail-fake-text-area, .checklist-item-details-text, .phenom.mod-comment-type .phenom-reactions, .phenom-desc, .action-comment p":{"direction":"rtl","text-align":"right"},"pre, code"
@font-face {
font-family: 'vazir-font';
font-weight: normal;
font-style: normal;
src: url('https://cdn.rawgit.com/rastikerdar/vazir-font/v19.0.0/dist/Vazir.woff') format('woff');
font-display: swap;
}
#board {
padding: 0 15px;
}
@mahdi-alavi
mahdi-alavi / gist:30b35994013e0c803674ee81d7245d2c
Last active September 29, 2018 09:53
wordpress: Convert string duration to ISO 8601 duration format
/* Convert string duration to ISO 8601 duration format.
=========================================================================== */
function itl_iso8601_duration( $seconds ) {
$hours = floor( $seconds / 3600 );
$seconds = $seconds % 3600;
$minutes = floor( $seconds / 60 );
$seconds = $seconds % 60;
$iso8601 = sprintf( 'PT%s%s%s',
@mahdi-alavi
mahdi-alavi / gist:1aceb2a1cc026b60ee0cb8ff0044a584
Created March 6, 2018 06:43
wordpres: Add async Attributes to Enqueue Scripts where Needed
/* Add async Attributes to Enqueue Scripts where Needed.
=========================================================================== */
function itl_script_tag_defer( $tag, $handle ) {
if ( is_admin() ) {
return $tag;
}
if ( strpos($tag, '/wp-includes/js/jquery/jquery') ) {
return $tag;
}
@mahdi-alavi
mahdi-alavi / gist:d2c89d0c62156688d0c2459640220c60
Created January 18, 2018 18:43
Add Custom Fee to Woocommerce Cart for Physical Products
/* Add Custom Fee to Woocommerce Cart for Physical Products.
=========================================================================== */
function itl_woocommerce_custom_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$is_physical = false;
@mahdi-alavi
mahdi-alavi / gist:b50e89e9d1564e581a7b228c91872428
Created July 3, 2017 12:54
WP_Query orderby include Argument for tax_query (taxonomy)
// Let’s presume we have a ‘product’ post type, with a ‘names’ taxonomy.
/* The posts_orderby filter.
=========================================================================== */
function itl_edit_posts_orderby( $orderby_statement, $wp_query ) {
if ( isset( $wp_query->query['orderby'] ) && 'include' == $wp_query->query['orderby'] ) {
$include = implode( ',', array_map( 'absint', $wp_query->query['include'] ) );
$orderby_statement = "FIELD( term_taxonomy_id, $include )";
}
@mahdi-alavi
mahdi-alavi / gist:6bcb6a24805107f2d127fea083050dea
Last active June 28, 2017 06:00
Piwik & WooCommerce - tracking code and woocommerce order tracking
// in line 61 insert your piwik url
// source code: wordpress.org/plugins/woocommerce-piwik-integration/
/* Get encoded categories by product.
=========================================================================== */
function getEncodedCategoriesByProduct( $product ) {
$categories = get_the_terms( $product->get_id(), 'product_cat' );
if ( ! $categories ) {
$categories = array();
@mahdi-alavi
mahdi-alavi / gist:1be04ad4247c7f0e4afb51284c627dfc
Created April 1, 2017 04:33
Woocommerce: limit customer to only one category per transaction
// original source http://stackoverflow.com/a/35072168
function itl_is_product_the_same_cat( $valid, $product_id, $quantity ) {
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' ); // get the current items