Linux Add custom bash commands

Step 1 :
Find your .bashrc file

Step 2 :
(.bashrc is found in root directory)
(You can type cd in the terminal and hit enter to get to root and use ls -la to see list of files in directory)
- Edit your .bashrc file. Use whatever editing tool of your choice.
or you can just edit in the terminal with the below command :
nano .bashrc
Once you are in the file proceed to step 3.

Step 3 :
Go to the bottom of you .bashrc file and look for something along the lines of the below :

 
 
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Step 4 :
Add a comment above the snippet indicated above so it looks like this.

#Custom Bash Commands
# Commands will be listed here...

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Step 5 :
Add your commands. 1 per line! use semi-colon ( ; ) to run multiple commands in succession

#Custom Bash Commands
alias my_custom_bash_command="do something in bash code"
alias my_custom_bash_command2="do something in bash code"
alias my_custom_bash_command3="do something in bash code"
alias my_custom_bash_command4="do something in bash code"

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion


WordPress Child Theme Code

  • Create these files first
    • functions.php
    • style.css
    • style-mobile.css
    • script.js
for functions.php
<?php
  function my_theme_enqueue_styles() {
    $parent_style = '../Divi';
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );

    wp_enqueue_style( 'child-styles', get_stylesheet_directory_uri() . '/style.css');
    wp_enqueue_style( 'child-style-mobile', get_stylesheet_directory_uri() . '/style-mobile.css');

    wp_enqueue_script('child-scripts', get_template_directory_uri() . '/script.js' );
  }
add_action("wp_enqueue_scripts","my_theme_enqueue_styles");
?>

for script.js

jQuery(document).ready(function( $ ) {
  console.log('Ready!');
}

for style.css

/*
Theme Name: Divi Child
Description: A dev site
Author: Garth Baker
Template: Divi
*/

for style-mobile.css

@media only screen and (max-width:600px) {
  /*--------------------------------*/
}

Javascript countdown timer for the difference between two dates

let timer = setInterval(function() {

  // The dispatch date is just the variable name. You can change it wo whatever. As long as you change it where ever else it is been used as well.  
  // the format for dispatch_date is ('2018-10-17 17:00:00') which is been pull from an input field with the id of "countdown_timer"
  const dispatch_date = new Date(document.getElementById("countdown_timer").value);
  const today = new Date();
  
  // console.log("dispatch_date : "+dispatch_date);
  // console.log("today : "+today);

  // get the difference
  const diff = dispatch_date - today;

  // math
  let days = Math.floor(diff / (1000 * 60 * 60 * 24));
  let hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = Math.floor((diff % (1000 * 60)) / 1000);

  // display
  document.getElementById("timer").innerHTML =

  //Days - Hours - Minutes - Seconds
    "</div class='timer-p'>This Stock list Expires in </div> \ <div class=\"days\"> \
  <div class=\"numbers\">" + days + "</div>Days</div> \
<div class=\"hours\"> \
  <div class=\"numbers\">" + hours + "</div>Hours</div> \
<div class=\"minutes\"> \
  <div class=\"numbers\">" + minutes + "</div>Minutes</div> \
<div class=\"seconds\"> \
  <div class=\"numbers\">" + seconds + "</div>Seconds</div> \
</div>";

}, 1000);

PHP check if an item is inside a multi dimensional array

Here’s the Stackoverflow link

Here’s the improved answer

//The Function
    function in_array_r($needle, $haystack, $strict = false) {
        foreach ($haystack as $item) {
            if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
                return true;
                $found = 'yes';
            }
        }
        $found = 'no';
        return false;
    }

//The Function in use
        //Set the result as a var so you can check it.
  $found = in_array_r($search_pre_order_date,$dates_array) ? 'found' : 'not found';
  
        //If the result is equal to something do something...
        if ($found == 'found') {
    echo 'Yes this item is in the array';
  }elseif ($found == 'not found') {
                echo 'Nope not in the array';
  }

Change the featured image text on custom post type

add_action( 'admin_head', 'remove_my_meta_boxen' );
function remove_my_meta_boxen() {
    remove_meta_box( 'postimagediv', 'countries', 'side' );
    add_meta_box('postimagediv', __('Country Flag'), 'post_thumbnail_meta_box', 'countries', 'side', 'high');
}

The post type in this example is countries

Simple Back Button

<a href="javascript:history.go(-1)" class="custom_back_button"><i class="fas fa-chevron-left"></i> Back</a>

This example uses Font Awesome. Don’t forget to include Font Awesome lib in your functions.php

Woocommerce Order Notifications

Send an email if there are any woocommerce orders created a before the current Day.

$wp_query = new WP_Query(
    array(
        'posts_per_page'=> -1,
        'post_type'=>'shop_order',
        'post_status'=>'wc-processing',
        // 'post_status'=>'wc-completed',
        // 'post_status'=>'wc-pending',
        // 'post_status'=>'wc-canceled',
    )
);
$message = "These orders are curently processing<br>";
$confirm_mail_send = 0;   
foreach ($wp_query->posts as $order) {
        date_default_timezone_set('Africa/Johannesburg');
        $current_date = strtotime(date('Y-m-d'));
        // echo $current_date."<br>";
        
        $post_date = $order->post_date;
        $post_date_exploded = explode(' ', $post_date);
        $post_date_for_diff = strtotime($post_date_exploded[0]);
        // echo $post_date_for_diff."<br>";

        $diff =  $current_date - $post_date_for_diff;
        // echo $diff."<br>";
        
        function secondsToTime($seconds) {
            $dtF = new \DateTime('@0');
            $dtT = new \DateTime("@$seconds");
            return $dtF->diff($dtT)->format('%a days');
        }
        // echo secondsToTime($diff);
        
        if ($diff > 1) {
            $href = 'https://tjhokopaint.co.za/wp-admin/post.php?post='.($order->ID).'&action=edit'; 
            $message .= '<a href="'.$href.'">Order: '.$order->ID.'</a> ';
            $confirm_mail_send++;
            // echo "True";
        }
}
echo $message;
if ($confirm_mail_send != 0) {
    $to = 'garth@webfootprint.co.za';
    $subject = 'Pending Orders';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail( $to, $subject, $message, $headers);
}

 

Convert Seconds to Time

function secondsToTime($seconds) {
    $dtF = new \DateTime('@0');
    $dtT = new \DateTime("@$seconds");
    return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo secondsToTime(1640467);