Wp
redirect if attachment page
// Reindirizza alla homepage la pagina dell'immagine
function reindirizza_homepage() {
if ( is_attachment() ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'template_redirect', 'reindirizza_homepage' );sostituire link immagini con filtro; link + controllo htaccess
add_filter('wp_get_attachment_url', function ($url)
{
return str_replace('rmstore.idv', 'removestore.it', $url);
});
// Replace srcset paths
add_filter('wp_calculate_image_srcset', function($sources)
{
foreach($sources as &$source)
{
if(!file_exists($source['url']))
{
$source['url'] = str_replace('rmstore.idv', 'new-domain.it', $source['url']);
}
}
return $sources;
});per htaccess link
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^[^/]+/([^/]+)/(?:.*?\.)?(\d+\.(gif|jpg|png))$ https://new-domain.it/$1/$2 [NC,L]
</IfModule>aggiungere cronJob
add_filter('cron_schedules', 'isa_add_every_three_minutes');
function isa_add_every_three_minutes($schedules)
{
$schedules['every_three_minutes'] = array(
'interval' => 180,
'display' => __('Every 3 Minutes', 'textdomain')
);
return $schedules;
}
// Schedule an action if it's not already scheduled
if (!wp_next_scheduled('isa_add_every_three_minutes')) {
wp_schedule_event(time(), 'every_three_minutes', 'isa_add_every_three_minutes');
}
// Hook into that action that'll fire every three minutes
add_action('isa_add_every_three_minutes', 'every_three_minutes_event_func');
function every_three_minutes_event_func()
{
// do something
}aggiungere intervallo personalizzato cronjob
function cron_add_everysixhours($schedules)
{
$schedules['everysixhours'] = array(
'interval' => 21600,
'display' => __('Every 6 hours')
);
return $schedules;
}
add_filter('cron_schedules', 'cron_add_everysixhours');WooCommerce
send cancelled_order mail to customer too
function wc_cancelled_order_add_customer_email( $recipient, $order ){
return $recipient . ',' . $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );set metadata su tutti i prodotti
/** Woocommerce: set metadada to all products */
function puk_add_offer_metadata_to_all_products()
{
$products = wc_get_products(array(
'limit' => -1,
'status' => 'publish',
'type' => 'simple',
));
foreach ($products as $product) {
$mdata_arr = array('10% di sconto ', 'Offerte nuove');
$product->update_meta_data('metadata_name', $mdata_arr);
$product->update_meta_data('metadata_name_2', 'FantaOfferte');
$product->save();
}
}
add_action('admin_init', 'puk_add_offer_metadata_to_all_products');set image based on product title
add_action('admin_init', 'associate_img_products');
function associate_img_products() {
global $wpdb;
$titles = ['Bilancia Cucina Digitale', ...];
foreach ($titles as $title) {
$product = get_page_by_title($title, OBJECT, 'product');
if ($product) {
$product_id = $product->ID;
for ($i=1; $i < 11; $i++) {
$fname = $title . '_' . $i;
// search file in media library
#$attachment = get_page_by_title($fname, OBJECT, 'attachment');
$attachments = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '%$fname%' AND post_type = 'attachment' ", OBJECT );
if(!$attachments) continue;
$attachment = $attachments[0];
if ($attachment) {
$attachment_id = $attachment->ID;
if($i == 1) {
// set product image
set_post_thumbnail($product_id, $attachment_id);
} else {
// add gallery image
$gallery = get_post_meta($product_id, '_product_image_gallery', true);
$gallery = explode(',', $gallery);
$gallery[] = $attachment_id;
$gallery = array_filter($gallery);
$gallery = implode(',', $gallery);
update_post_meta($product_id, '_product_image_gallery', $gallery);
}
}
}
}
}
}