Skip to content

Instantly share code, notes, and snippets.

@nirendra
nirendra / Wrapping Text Around Images
Created December 2, 2013 16:13
Wrapping Text Around Images CSS
img.alignright {float:right; margin:0 0 1em 1em}
img.alignleft {float:left; margin:0 1em 1em 0}
img.aligncenter {display: block; margin-left: auto; margin-right: auto}
.alignright {float:right; }
.alignleft {float:left; }
.aligncenter {display: block; margin-left: auto; margin-right: auto}
@nirendra
nirendra / dateValidation.php
Created November 27, 2013 23:51
Date format validation in PHP
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
{
//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
@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;
@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!#$%&'*+-/=?^_`{|}~])@([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 / 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 / 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 / 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 / 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 / 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 / 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);