Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
ricardobrg / enqueue.php
Last active July 18, 2023 09:11
Enqueue scripts in WordPress with defer or async
<?php
// This code is based in Mathew Horne blog post: https://matthewhorne.me/defer-async-wordpress-scripts/
//function to add async attribute
function add_async_attribute($tag, $handle) {
$scripts_to_async = array('my-js-handle-async', 'another-handle-async');
//check if this script is in the array
if (in_array($handle, $scripts_to_async)){
//return with async
return str_replace(' src', ' async="async" src', $tag);
@ricardobrg
ricardobrg / maxlength.html
Created June 6, 2018 03:06
input number maxlength with javascript oninput
<input type="number" step="1" min="1" max="999" maxlength="3" name="number" inputmode="numeric" oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);">
#!/usr/bin/env node
console.log('Olá mundo2!')
@ricardobrg
ricardobrg / woocommerceinstalments.php
Last active November 23, 2020 13:29
Add instalments in product page
<?php
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;display:none">%s %s</div>','Total do Produto','<span class="price">'.$product->get_price().'</span>');
echo sprintf('<div id="cart_total_price" style="margin-bottom:20px;display:none">%s %s</div>','Total do Carrinho','<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
@ricardobrg
ricardobrg / post.md
Created March 12, 2020 23:03 — forked from vinicius73/post.md
["LÓGICA DE PROGRAMAÇÃO" É BOBAGEM, e explicarei porquê.]

#["LÓGICA DE PROGRAMAÇÃO" É BOBAGEM, e explicarei porquê.]

Se preparem que o texto é longo.

Várias vezes chegam novatos aqui perguntando como começar, e a galera diz "estuda lógica primeiro, depois vai pra linguagem X". Vivo dizendo que é bobagem. Ontem, em particular, falei isso, e vieram várias pessoas por inbox me perguntar porquê (e uma pra me xingar, achando que falei por arrogância).

Pra facilitar, eu vou escrever uma boa explicação de porquê "lógica de programação" é furada, doa a quem doer, e postar na APDA e no fórum da EnergyLabs (para futuras referências, porque esse assunto vai voltar, ctz).

@ricardobrg
ricardobrg / price_qtd.php
Created January 27, 2020 21:06
Woocommerce price quantities
add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
// Change and return $price_html variable using the $price and weight amount
function wb_change_product_html( $price ) {
$price_html = '<span class="amount">' . $price . ' per kg </span>'; // change weight measurement here
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart' );
// Change the cart prices with $price variable and weight amount
function is_happening_now(talk) {
// take talk.datetime in ms from epoch
let talkTime = new Date(talk.datetime).getTime();
// calculate talkend in ms from epoch
let talkEnd = talkTime + (talk.duration * 60000);
// take current epoch time in browser`s timezone
let now = new Date().getTime();
// Return boolean true if talk is happening now or false.
return now > talkTime && now < talkEnd;
}
@ricardobrg
ricardobrg / logcat
Created April 3, 2019 13:37
Android 7.1.2 Google Pixel XL in AWS Device Farm
--------- beginning of main
04-02 11:02:32.310 5549 7959 I PTCommittedOperation: Receive new configuration for com.google.android.gms.magictether
04-02 11:02:32.341 5549 7679 E MagicTether: [MagicTetherInitializer] Invalid account list.
04-02 11:02:32.398 5581 7947 W ModuleInitIntentOp: Dropping unexpected action com.google.android.gms.phenotype.COMMITTED
04-02 11:02:32.402 5549 8068 I PTCommittedOperation: Receive new configuration for com.google.android.metrics
04-02 11:02:32.438 5549 7170 E MagicTether: [TetherListenerService] Invalid account list.
04-02 11:02:32.511 5581 7946 W ModuleInitIntentOp: Dropping unexpected action com.google.android.gms.phenotype.COMMITTED
04-02 11:02:32.518 5549 8068 I PTCommittedOperation: Receive new configuration for com.google.android.gms.walletp2p#config
04-02 11:02:32.598 5581 8028 W ModuleInitIntentOp: Dropping unexpected action com.google.android.gms.phenotype.COMMITTED
04-02 11:02:32.648 5549 8228 I PTCommittedOperation: Receive new configuration for
@ricardobrg
ricardobrg / sql_import.php
Last active January 15, 2019 15:43 — forked from b4oshany/sql_import.php
WPDB SQL File import
<?php
/* NOTICE
* This script imports SQL commands "as is".
* Double check the SQL commands before using it
* and BACKUP YOUR DATABASE.
* It needs some improvement to use wpdb->prepare.
*/
class WPSQLImporter{
/**
* Loads an SQL stream into the WordPress database one command at a time.
@ricardobrg
ricardobrg / checkboxes.js
Last active December 2, 2018 12:59
jQuery checkboxes handling based on ids and classes with indeterminate property
jQuery(document).ready(function($){
$(":checkbox").click(function(){
let isChecked = $(this).prop('checked');
let id = $(this)[0].id;
let itemClasses = $(this).attr('class');
if (id != undefined && id != ""){
$("."+id).prop('checked',isChecked);
}
if (!isChecked){
if (itemClasses != undefined){