Skip to content

Instantly share code, notes, and snippets.

View melvinstanly's full-sized avatar
🌪️
Fast and Furious

Melvin Stanly melvinstanly

🌪️
Fast and Furious
View GitHub Profile
@melvinstanly
melvinstanly / worpress-file-upload-error.php
Created December 3, 2018 07:54
wordpress file upload
//How to fix “this file type is not permitted for security reasons” in WordPress
function enable_extended_upload ( $mime_types =array() ) {
// The MIME types listed here will be allowed in the media library.
// You can add as many MIME types as you want.
$mime_types['gz'] = 'application/x-gzip';
$mime_types['zip'] = 'application/zip';
$mime_types['rtf'] = 'application/rtf';
$mime_types['ppt'] = 'application/mspowerpoint';
@melvinstanly
melvinstanly / selectjs.txt
Last active February 13, 2019 07:24
jQuery select2
/* Dynamically add options to select field */
// Assuming 'custom-select' is the class of select field
<select class="custom-select">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
@melvinstanly
melvinstanly / regex.txt
Created December 28, 2018 11:03
Regular expression tricks
* To replace all runs of white spaces in a string with ( _ ) underscore => /\s+/g
-> Replace adjacent white spaces with a single underscore
* To replace whitespace in a string with underscore => / /g
-> Replace each white space with a single underscore. Each adjacent white spaces will be replaced
@melvinstanly
melvinstanly / multi-string-replace.js
Last active February 13, 2019 07:24
Replace multiple strings in a single run in js
// Replace multiple strings with multiple values
var str = "I have a cat, a dog, and a goat.";
var mapObj = {
cat:"monkey",
dog:"goat",
goat:"cat"
};
str = str.replace(/cat|dog|goat/gi, function(matched){
@melvinstanly
melvinstanly / file-preview.js
Last active February 8, 2019 09:54
File Preview Before Upload jQuery
function readURL(input){
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
//e.target.result ->contain image url in encoded form
//Do action with e.target.result here. Dont place outside onload function. won't work
};
reader.readAsDataURL(input.files[0]);
}
}
@melvinstanly
melvinstanly / html data attributes.js
Last active January 23, 2019 11:38
html data attribute manipulation function
// Remove all data attributes of an element
$.each( $("element-selected").data(), function(i){
$("div").removeAttr("data-" + i);
});
@melvinstanly
melvinstanly / woo-email-classes.php
Last active February 13, 2019 07:17
WooCommerce Email Related Codes
<?php
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
$emails = wc()->mailer()->emails; // Contains all WooCommerce email classes with details
?>
@melvinstanly
melvinstanly / woo-product-titles.php
Created February 15, 2019 04:37
Woocommerce Product titles
<?php
// Change product title for products in shop page
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
function change_product_title() {
$additional_text = ' More info';
echo '<h2 class="woocommerce-loop-product__title">' . get_the_title() .$additional_text.'</h2>';
}
add_action('woocommerce_shop_loop_item_title','change_product_title');
@melvinstanly
melvinstanly / price.php
Created February 20, 2019 11:00
Remove Price Symbol from string
<?php
$val = preg_replace('/&.*?;/', '', $val);
@melvinstanly
melvinstanly / slice-php.php
Created March 12, 2019 12:27
Php equivalent for .slice() of jquery
<?php
function strSlice($str, $start, $end) {
$end = $end - $start;
return substr($str, $start, $end);
}
$name = 'migthegreek';
// Get substr from index 3 to 6
echo strSlice($name, 3, 6);