Monday, July 23, 2018

wordpress - Woocommerce when deleting a product, also delete the product gallery and product image automatically

1. FOR Remove Attached Image with Product in Woocommerce 

add_action( 'before_delete_post', 'delete_product_images', 10, 1 );
function delete_product_images( $post_id )
{
    $product = wc_get_product( $post_id );
    if ( !$product ) {
        return;
    }
    $featured_image_id = $product->get_image_id();
    $image_galleries_id = $product->get_gallery_image_ids();
    if( !empty( $featured_image_id ) ) {
        wp_delete_post( $featured_image_id );
    }
    if( !empty( $image_galleries_id ) ) {
        foreach( $image_galleries_id as $single_image_id ) {
            wp_delete_post( $single_image_id );
        }
    }
}

2. FOR Remove Attached Image with Post And Page


add_action("before_delete_post","wdm_delete_post_images",10,1);
function wdm_delete_post_images($post_id)
{
 global $wpdb;
          $args = array(
                'post_parent' => $post_id,
                'post_type'   => 'attachment',
                'numberposts' => -1,
                'post_status' => 'any'
        );
        $childrens = get_children( $args);
        if($childrens):
            foreach($childrens as $attachment):
             wp_delete_attachment( $attachment->ID, true );
             $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = ".$attachment->ID);
             wp_delete_post($attachment->ID,true );
            endforeach;
        endif;
}
loading...

Tuesday, May 29, 2018

Download Database Back_up using Codeigniter

//Backup manually
    public function CI_db_backup() {
        // Load the DB utility class
        $this->load->dbutil();
        $this->load->database();
        $dbName = $this->db->database;
        $prefs = array(
            "format" => 'sql', // gzip, zip, txt
            "filename" => $dbName.'.sql', // File name 
        );

// Backup your entire database and assign it to a variable
        $backup = $this->dbutil->backup($prefs);

        $folder = FCPATH . "/storage_db/db_backup/";
        $date = date("m-d-Y-H-i-s", time());
        $filename = $dbName . "_" . $date . ".sql";
// Load the file helper and write the file to your server
        $this->load->helper('file');
        write_file($folder . $filename, $backup);

// Load the download helper and send the file to your desktop
        $this->load->helper("download");
        force_download($filename, $backup);
    }

Friday, May 25, 2018

PHP Mail Function Working with Bluehost server

Simple code for sending emails that worked is as follows:



<?php
$emailto = 'to@domain.com';
$emailfrom = 'from@domain.com';
$subject = 'Email Subject';
$messagebody = 'Hello.';
$headers =
'From: ' . $emailfrom.' . "\r\n" .
'Reply-To: ' . $emailto . ' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($emailto, $subject, $message, $headers);
?>
PHP mail code that works with Bluehost



<?php
$emailto = 'to@domain.com';
$toname = 'TO NAME';
$emailfrom = 'from@domain.com';
$fromname = 'FROM NAME';
$subject = 'Email Subject';
$messagebody = 'Hello.';
$headers =
'Return-Path: ' . $emailfrom . "\r\n" .
'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'X-Priority: 3' . "\r\n" .
'X-Mailer: PHP ' . phpversion() .  "\r\n" .
'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: 8bit' . "\r\n" .
'Content-Type: text/plain; charset=UTF-8' . "\r\n";
$params = '-f ' . $emailfrom;
$test = mail($emailto, $subject, $messagebody, $headers, $params);
// $test should be TRUE if the mail function is called correctly
?>

Product Category Demo in laravel

https://drive.google.com/file/d/1kuyeT3LA22IuN3o_kypOyXesEXMLv31e/view?usp=sharing