Skip to content

Instantly share code, notes, and snippets.

View webdados's full-sized avatar

Marco Almeida webdados

View GitHub Profile
@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 / 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
@webdados
webdados / apg_sms_message_bank_transfer.php
Last active September 20, 2017 10:28
Special Bank Transfer instructions for New Order emails no APG SMS (using the %bacs_instructions% tag on the sms template)
<?php
// Bank Transfer details
add_filter('apg_sms_message', 'apg_sms_message_bank_transfer', 999, 2);
function apg_sms_message_bank_transfer( $message, $order_id ) {
$inst = '';
$order = new WC_Order($order_id);
$status = is_callable( array( $order, 'get_status' ) ) ? $order->get_status() : $order->status; //WC >=3.0 or older?
//New order? (Not the English synthpop band...)
if ( $status == 'on-hold' || $status == 'pending' ) {
$payment_method = is_callable( array( $order, 'get_payment_method' ) ) ? $order->get_payment_method() : $order->payment_method; //WC >=3.0 or older?
@webdados
webdados / fb_og_image_additional.php
Created September 26, 2017 09:25
Using the fb_og_image_additional filter on Facebook Open Graph, Google+ and Twitter Card Tags for WordPress
<?php
add_filter( 'fb_og_image_additional', 'extra_preview_images' );
function extra_preview_images( $fb_image_additional ) {
//You could/should use your own logic to decide which images to return. This example only shows you the structure of the array you should return.
if ( !is_array( $fb_image_additional ) ) {
$fb_image_additional = array();
}
$fb_image_additional[] = array(
'fb_image' => 'https://domain.com/wp-content/uploads/og_image2.jpg',