Saturday, October 12, 2019

JQuery count number of divs with a certain class?


var numItems = $('.item').length;

Get all text field value located in a specific class


$(".test .text-field").each(function() {
    alert($(this).val());
});

Note : test and text-field is class of text field

Friday, June 7, 2019

Remove Menu From Wordpress Admin

add_action('admin_init', 'remove_menu_from_admin');

function remove_menu_from_admin()

    // Remove unnecessary menus
    $menus_to_stay = array(
        // Job Listing
        'edit.php?post_type=job_listing',
    );
    foreach ($GLOBALS['menu'] as $key => $value) {         
        if (!in_array($value[2], $menus_to_stay)) remove_menu_page($value[2]);
    }
}

Thursday, April 18, 2019

PHP Code to Remove Everything Except Number


$mobileNumber = "(111) 2568-5698";

preg_replace('/\D/', '', $mobileNumber);

OR,

preg_replace('/[^0-9]/', '', $mobileNumber);

Monday, April 1, 2019

Encrypt and Decrypt a PHP String

function decEnc($action, $string) {
$output = false;

$encrypt_method = "AES-256-CBC";
$secret_key = 'Secret key';
$secret_iv = 'Secret iv';

// hash
$key = hash('sha256', $secret_key);

// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);

if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}

return $output;
}

Encryption Example:
$string = "hanzala";
decEnc("encrypt", $string);

Decryption  Example:
$string = "eU15RFhTZGN5SWJzTGgrRSs4SW9zQT09";
decEnc("decrypt", $string)

Sunday, March 31, 2019

How to get the value of selected radio button in a group using jQuery

    $(document).ready(function(){
        $("input[type='button']").click(function(){
            var radioValue = $("input[name='gender']:checked").val();
            if(radioValue){
                alert("Your are a - " + radioValue);
            }
        });
       
    });

Allow Number to insert into text box

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

onkeypress="return isNumber(event)"

Friday, March 29, 2019

How to: enable CORS in express.js (node.js)

Express.js is one of the most popular node.js frameworks for serving website or build APIs.

Steps to use CROS in node.js

1. npm install cors
2. Use the following code
   var express = require('express')
   var cors = require('cors')
   var app = express()

app.use(cors())

app.get('/userList', function (req, res, next) {
  res.json({msg: 'Enabled the CROS'})
})

app.listen(80, function () {
  console.log('Enabled CORS listening on port 80')
}) 

Friday, March 22, 2019

Saturday, March 16, 2019

How to Import and Export Databases


EXPORT
To Export a database, open up terminal, making sure that you are not logged into MySQL and type :

mysqldump -u [username] -p [database name] > [database name].sql

Import

source filepath.../filename.sql;


Thursday, February 28, 2019

Check Mobile Version in PHP


function isMobile() {
        return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
    }

Tuesday, February 19, 2019

AES Encryption And Deception

   function dec_enc($action, $string) {
            $output = false;
       
            $encrypt_method = "AES-256-CBC";
            $secret_key = 'This is my secret key';
            $secret_iv = 'This is my secret iv';
       
            // hash
            $key = hash('sha256', $secret_key);
           
            // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
            $iv = substr(hash('sha256', $secret_iv), 0, 16);
       
            if( $action == 'encrypt' ) {
                $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
                $output = base64_encode($output);
            }
            else if( $action == 'decrypt' ){
                $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
            }
       
            return $output;
        }