Skip to content

Instantly share code, notes, and snippets.

View alordiel's full-sized avatar
🛶
Požuri polako

a.vasilev alordiel

🛶
Požuri polako
View GitHub Profile
@alordiel
alordiel / username-by-email.php
Created August 16, 2018 12:30
WordPress - create unique username by provided email
<?php
function create_unique_user_name_by_email ($email) {
$email_name = explode( '@', $email );
if ( ! get_user_by( 'login', $email_name[0] ) ) {
$username = $email_name[0];
} else {
$username = uniqid( $email_name[0], false );
}
@alordiel
alordiel / add_new_column_to_wp_db.php
Last active May 5, 2018 07:40
Adding a new column to the custom database table in WordPress
<?php
function add_new_columns_to_easyexam_students_exams () {
global $wpdb;
$table_name = $wpdb->prefix . 'table_name';
// Check if column exists
$column_exist = $wpdb->query("SHOW COLUMNS FROM $table_name LIKE 'column_name'");
if ($column_exist == false) {
// alter the table with the new column name
$wpdb->query("ALTER TABLE $table ADD column_name VARCHAR(255) NOT NULL AFTER some_other_column_name");
}
@alordiel
alordiel / crypto.php
Created February 21, 2018 13:56
PHP custom crypt and decrypt function
//Source: https://stackoverflow.com/questions/15194663/encrypt-and-decrypt-md5
function encryptIt( $q ) {
$cryptKey = 'блалалаѝк777амайка';
$qEncoded = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
return( $qEncoded );
}
function decryptIt( $q ) {
$cryptKey = 'блалалаѝк777амайка';
@alordiel
alordiel / markdown-link.js
Created January 31, 2018 12:42
[JS] to replace a markdown for link with actual HTML link element
@alordiel
alordiel / cart.php
Created January 24, 2018 08:52
[WooCommerce 3.2.6] Get product variations from cart items (used in cart.php)
<?php
//Loop through each item from the cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//get the current product ID
$product_id = $cart_item['product_id'];
//first - check if product has variations
if(isset($cart_item['variation']) && count($cart_item['variation']) > 0 ){
//get the WooCommerce Product object by product ID
$current_product = new WC_Product_Variable($product_id);
//get the variations of this product
@alordiel
alordiel / woocommerce-custom-shipping-method.php
Last active December 7, 2023 18:04
Adding custom shipping method for WooCommerce (v. 3.2.6)
<?php
//Works with WooCommerce 3.2.6
add_action( 'woocommerce_shipping_init', 'econt_shipping_method' );
function econt_shipping_method() {
if ( ! class_exists( 'WC_Econt_Shipping_Method' ) ) {
class WC_Econt_Shipping_Method extends WC_Shipping_Method {
public function __construct( $instance_id = 0 ) {
$this->instance_id = absint( $instance_id );
$this->id = 'econt';//this is the id of our shipping method
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
.verb-wrapper{
width:400px;
border: 1px solid green;
@alordiel
alordiel / types-conversions.js
Created July 20, 2017 09:05
JS Convering one type of variable to another type
var str = "string";
var num = 123;
var boo = false;
var obj = {h:3};
console.log(String(str)); // "string"
console.log(Number(str)); // NaN
console.log(Boolean(str)); // true
console.log(Object(str)); // "string"
@alordiel
alordiel / comparing-types.js
Created July 20, 2017 08:44
JS types comparing
console.log(null == undefined); // TRUE
console.log(null === undefined); // FALSE
console.log(null == ""); // FALSE
console.log(null == 0); // FALSE
console.log(undefined == ""); // FALSE
console.log(undefined == 0); // FALSE
console.log("" == 0); // TRUE
console.log("" === 0); // FALSE
console.log(false == 0); // TRUE
console.log(false == ""); // TRUE
@alordiel
alordiel / variable-types.js
Created July 20, 2017 08:30
JS varaible types and definitions
var a;
var b = "";
var c = 0;
var d = null;
var e = [];
var f = {};
var g = false;
console.log("a: " + typeof a); // "undefined"