Thursday, 26 September 2024

How can i put "posted x minutes ago on my posts?

 Just place this code in your functions.php

function time_ago( $type = 'post' ) {
    $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';

    return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');

}

To use it anywhere in your theme (for example, on single.php), place this code where you want the "time ago" feature displayed:

<?php echo time_ago(); ?>  

Monday, 29 April 2024

CSS code to make a submenu open when hovering over the main menu item

 <ul> 

  <li> 

    <a href="#">Menu Item 1</a> 

    <ul class="submenu"> 

      <li><a href="#">Submenu Item 1</a></li> 

      <li><a href="#">Submenu Item 2</a></li> 

      <li><a href="#">Submenu Item 3</a></li> 

    </ul> 

  </li> 

  <li> 

    <a href="#">Menu Item 2</a> 

    <ul class="submenu"> 

      <li><a href="#">Submenu Item 1</a></li> 

      <li><a href="#">Submenu Item 2</a></li> 

      <li><a href="#">Submenu Item 3</a></li> 

    </ul> 

  </li> 

</ul>



<style>

ul ul.submenu { 

  display: none; 

}

ul li:hover > ul.submenu { 

  display: block; 

</style>

Wednesday, 20 March 2024

Create wordpress admin user using wp query

 INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, 

`user_status`)

 VALUES ('admin999', MD5('password999'), 'firstname lastname', 'email@example.com', '0');

 

 INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) 

 VALUES (NULL, (Select max(id) FROM wp_users), 

 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');

 

 INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) 

 VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');

Wednesday, 21 February 2024

Video play and end point redirec to other URL

    jQuery('.playaudio').on('click',function(){

   //alert('onclick');
 if (localStorage.getItem("hasCodeRunBefore1") === null) {

   jQuery('#myModal').show();

         document.getElementById("video1").play();

        localStorage.setItem("hasCodeRunBefore1", true);

   

           video = document.getElementById('video1');

            video.addEventListener('ended',function() {   

            window.location.href = 'https://americanalliance.dgquick.in/guest/welcome.php';});

    }

   });

Monday, 27 November 2023

Shipping option update on cart and checkout page using ajax

 


add_action( 'wp_footer', 'refresh_shipping_js' );

function refresh_shipping_js() {

    // Only on checkout

    if( is_checkout() && ! is_wc_endpoint_url() ):

    ?>

    <script type="text/javascript">

    jQuery( function($){

        if (typeof wc_checkout_params === 'undefined') 

            return false;


        var refresh = 'yes';


        $.ajax({

            type: "POST",

            url: wc_checkout_params.ajax_url,

            data: ({

                'action': 'updating_shipping',

                'refresh_shipping': refresh,


            }),

            success: function(response) {

                if( response === '1' ) {

                    $(document.body).trigger('update_checkout');

                    console.log('Success: '+response); // For testing (to be removed)

                } else {

                    console.log('Failled: '+response); // For testing (to be removed)

                }

            },

            error:function(error) {

                console.log('Error: '+error); // For testing (to be removed)

            }

        });

    });

    </script>

    <?php

    endif;

}


// function that gets the Ajax data

add_action( 'wp_ajax_updating_shipping', 'updating_shipping' );

add_action( 'wp_ajax_nopriv_updating_shipping', 'updating_shipping' );

function updating_shipping() {

    if ( isset($_POST['refresh_shipping']) && $_POST['refresh_shipping'] === 'yes' ){

        WC()->session->set('refresh_shipping', '1' );

    } else {

        WC()->session->set('refresh_shipping', '0' );

    }

    echo  WC()->session->get('refresh_shipping');

    die(); // Alway at the end (to avoid server error 500)

}


// Function that refresh session shipping methods data

add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods', 10, 1 );

function refresh_shipping_methods( $post_data ){

    if ( WC()->session->get('refresh_shipping' ) === '1' ) {

        foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){

            WC()->session->set( 'shipping_for_package_' . $package_key, false );

        }

        WC()->cart->calculate_shipping();

    }

}

Thursday, 14 September 2023

How to skip file when you update the wordpress plugin

function disable_plugin_updates( $value ) {

      if ( isset($value) && is_object($value) ) {

        if ( isset( $value->response['woocommerce/woocommerce.php'] ) ) {

          //unset( $value->response['woocommerce/woocommerce.php'] );

          unset( $value->response['woocommerce/templates/emails/email-order-items.php'] );

        }

      }

      return $value;

    }

    add_filter( 'site_transient_update_plugins', 'disable_plugin_updates' ); 

Sunday, 30 July 2023

Backup and Restore MySQL database in PHP

 Script to backup using Php


<?php
define("BACKUP_PATH", "/home/abdul/");

$server_name   = "localhost";
$username      = "root";
$password      = "root";
$database_name = "world_copy";
$date_string   = date("Ymd");

$cmd = "mysqldump --routines -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";

exec($cmd);
?>

Script to restore

<?php

$restore_file  = "/home/abdul/20140306_world_copy.sql";
$server_name   = "localhost";
$username      = "root";
$password      = "root";
$database_name = "test_world_copy";

$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} < $restore_file";
exec($cmd);

?>