Skip to content

Instantly share code, notes, and snippets.

@nirendra
nirendra / 0_reuse_code.js
Created November 27, 2013 21:33
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@nirendra
nirendra / getPageTitle
Created November 27, 2013 23:21
Get page title based on id
function webspec_get_title($my_id) {
$post_id_5369 = get_post($my_id);
$title = $post_id_5369->post_title;
$title = apply_filters('the_title', $title);
$title = str_replace(']]>', ']]>', $title);
@nirendra
nirendra / getPageContent
Created November 27, 2013 23:23
Get page content based on id
function webspec_get_content($my_id){
$post_id_5369 = get_post($my_id);
$content = $post_id_5369->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
@nirendra
nirendra / getFeaturedImage.txt
Created November 27, 2013 23:24
Get featured image based on id
function webspec_get_featured_img($id){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $id), 'single-post-thumbnail' );
echo $image[0];
}
@nirendra
nirendra / getExcerpt
Created November 27, 2013 23:28
Get excerpt outside the loop based on id
Method 1:
function get_excerpt_outside_loop($post_id) {
$post_excerpt = get_page($post_id) -> post_excerpt;
echo $post_excerpt;
}
@nirendra
nirendra / pathToTheme
Created November 27, 2013 23:33
Path to theme directory (add to links and img tags e.t.c)
<?php echo get_template_directory_uri(); ?>
@nirendra
nirendra / Sanitize.php
Created November 27, 2013 23:39
Sanitize Database Inputs
// Function for stripping out malicious bits
function cleanInput($input) {
$search = array(
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
@nirendra
nirendra / distanceCalculation.php
Created November 27, 2013 23:43
Calculate distance between two points
The function below use the latitude and longitude of two locations, and calculate the distance between them in both miles and metric units.
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
@nirendra
nirendra / emailValidation.php
Created November 27, 2013 23:48
Email validation snippet in PHP
$email = $_POST['email'];
if(preg_match("~([a-zA-Z0-9!#$%&amp;'*+-/=?^_`{|}~])@([a-zA-Z0-9-]).([a-zA-Z0-9]{2,4})~",$email)) {
echo 'This is a valid email.';
} else{
echo 'This is an invalid email.';
}
@nirendra
nirendra / Base64Encode-Decode.php
Created November 27, 2013 23:49
Base64 Encode and Decode String in PHP
function base64url_encode($plainText) {
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/=', '-_,');
return $base64url;
}
function base64url_decode($plainText) {
$base64url = strtr($plainText, '-_,', '+/=');
$base64 = base64_decode($base64url);
return $base64;