Skip to content

Instantly share code, notes, and snippets.

View webdados's full-sized avatar

Marco Almeida webdados

View GitHub Profile
@webdados
webdados / percentage_woocommerce_sale_flash.php
Last active March 22, 2019 08:03
Discount percentage on the WooCommerce "on sale" badge
<?php
add_filter( 'woocommerce_sale_flash', 'percentage_woocommerce_sale_flash', 10, 3 );
function percentage_woocommerce_sale_flash( $html, $post, $product ) {
if ( $html!='' ) {
$perc = round( 100 - ( $product->sale_price * 100 / $product->regular_price ) );
if ( $perc>0 ) $html = '<span class="onsale">-'.$perc.'%</span>';
}
return $html;
}
@webdados
webdados / woocommerce_localisation_address_formats.php
Last active November 17, 2016 11:30
Change WooCommerce address format for Portugal
<?php
add_filter('woocommerce_localisation_address_formats','custom_woocommerce_localisation_address_formats');
function custom_woocommerce_localisation_address_formats($formats) {
//$formats['default']="{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}";
$formats['PT']="{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}";
return $formats;
}
?>
@webdados
webdados / style_cmb2_copy_fields_as_readonly.php
Last active January 11, 2017 09:41
When using CMB2 and WPML it may be useful to style "copy" fields differently, on translated posts edit screen, so the user knows it makes no sense to edit them.
<?php
add_action( 'admin_head', 'style_cmb2_copy_fields_as_readonly', 9999 );
function style_cmb2_copy_fields_as_readonly() {
$screen = get_current_screen();
//var_dump($screen);
if ( $screen->base == 'post' ) {
global $post, $sitepress;
if ( isset($post) && isset($sitepress) ) {
@webdados
webdados / excerpt-as-instant-article-subtitle.php
Created January 11, 2017 13:37
Use WordPress post excerpt as Facebook Instant Article subtitle
<?php
/* Instant Articles (https://wordpress.org/plugins/fb-instant-articles/) - Use post excerpt as subtitle */
add_filter( 'instant_articles_subtitle', 'hf_instant_articles_subtitle' );
function hf_instant_articles_subtitle( $subtitle ) {
$excerpt = get_the_excerpt();
if ( trim($excerpt)!='' ) $subtitle = trim($excerpt);
return $subtitle;
}
<?php
/*
* When migrating an old website to WordPress, you should store pages/posts/... URLs in a custom field
* Example: /studio.php
* You can create a metabox to manually insert the URL, on the post edit scree, or have it imported directly if you are exporting/importing from the old CMS to WordPress
* If some visitor lands on the old URL, you then hook into the template_redirect, check if it's a 404, and then try to redirect him to the correct page/post/...
* This is of extreme importance SEO wise
*/
@webdados
webdados / custom_nav_menu_item_title.php
Last active February 28, 2018 10:52
Change WooCommerce "My Account" item menu title when the user is logged in
<?php
/* If you want to limit it to a specific theme location */
add_filter( 'nav_menu_item_title', 'custom_nav_menu_item_title', 10, 3 );
function custom_nav_menu_item_title( $title, $item, $args ) {
//Menu item should be set with the text you want to use for "logged out" users
//Logged in?
if ( is_user_logged_in() ) {
//Correct menu location
$theme_locations = array( 'acc-menu', 'mobile-secondary-menu' ); //Update to your own locations here
if ( isset( $args->theme_location ) && in_array( $args->theme_location, $theme_locations ) ) {
@webdados
webdados / defer_woocommerce_transactional_emails.php
Created April 14, 2017 09:11
Re-enable WooCommerce deferred email sending (added in 3.0 and turned off on 3.0.3)
<?php
//Code goes in theme functions.php.
add_filter( 'woocommerce_defer_transactional_emails', '__return_true' );
@webdados
webdados / grep_string.php
Last active April 21, 2017 14:20
Search the whole hosting account for files containing specific string(s)
<?php
//Add the strings to search
$strings = array(
'_0xaae8',
);
//Exclude file extensions
$extensions_ignore = array(
'jpg',
'png',
@webdados
webdados / apg_sms_procesa_el_telefono.php
Created September 14, 2017 12:25
New "apg_sms_procesa_el_telefono" function with filters to override processing Raw
<?php
function apg_sms_procesa_el_telefono( $pedido, $telefono, $servicio, $propietario = false, $envio = false ) {
if ( apply_filters( 'apg_sms_procesa_el_telefono_processar', true, $pedido, $telefono, $servicio, $propietario, $envio ) ) { //Filter added by Marco Almeida - Should return false if no processing should be done
$numero_de_pedido = is_callable( array( $pedido, 'get_id' ) ) ? $pedido->get_id() : $pedido->id;
$billing_country = is_callable( array( $pedido, 'get_billing_country' ) ) ? $pedido->get_billing_country() : $pedido->billing_country;
$shipping_country = is_callable( array( $pedido, 'get_shipping_country' ) ) ? $pedido->get_shipping_country() : $pedido->shipping_country;
$prefijo = apg_sms_prefijo( $servicio );
$telefono = str_replace( array( '+','-' ), '', filter_var( $telefono, FILTER_SANITIZE_NUMBER_INT ) );
if ( !$propietario ) {
if ( ( !$envio && $billing_country && ( WC()->countries->get_base_country() != $billing_country ) || $prefijo ) ) {
@webdados
webdados / localize_js_wpml_language.php
Last active September 19, 2017 15:29
Localize own Javascript with WPML current language (icl_vars was removed in 3.8)
<?php
add_action('wp_enqueue_scripts', 'my_scripts');
function my_scripts() {
// Add your JS file
wp_enqueue_script( 'my_script', get_template_directory_uri().'/js/functions.js' );
// Localize your script with server side data
global $sitepress;
wp_localize_script( 'my_script', 'my_var', array(
'wpml_current_language' => $sitepress->get_current_language(), //On javascript use my_var.wpml_current_language to get this value