Skip to content

Instantly share code, notes, and snippets.

@jeweltheme
jeweltheme / get_ip_address_php
Last active May 5, 2017 19:28 — forked from mrkpatchaa/get_ip_address_php
Get user ip address in php
<?PHP
function getUserIP()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
@jeweltheme
jeweltheme / Simple Ajax Login Form.php
Created May 11, 2017 04:07 — forked from cristianstan/Simple Ajax Login Form.php
Wordpress: Simple Ajax Login Form
<?php
//Simple Ajax Login Form
//Source: http://natko.com/wordpress-ajax-login-without-a-plugin-the-right-way/
//html
<form id="login" action="login" method="post">
<h1>Site Login</h1>
<p class="status"></p>
<label for="username">Username</label>
<input id="username" type="text" name="username">
@jeweltheme
jeweltheme / gist:0b60a01c09c0fbc9318fb776b7c1a2ed
Created May 13, 2017 19:51 — forked from mattboon/gist:1275191
WordPress - Query Custom Post Type and taxonomy term by ID
<?php
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
array(
@jeweltheme
jeweltheme / Video generator
Created May 15, 2017 20:31 — forked from jimmylatreille/Video generator
Youtube, Vimeo and Dailymotion script
<?php
if(preg_match('/vimeo/', $data['url'])){
$url = '//player.vimeo.com/video/'.end(explode('/', $data['url']));
echo "<iframe src='$url' width='560' height='315' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";
echo "<img src='".unserialize(file_get_contents("http://vimeo.com/api/v2/video/".end(explode('/', $data['url'])).".php"))[0]['thumbnail_medium']."' />";
@jeweltheme
jeweltheme / gist:72c51fa5639bd29dd3570e68ffe1f876
Created May 17, 2017 04:18 — forked from wwdboer/gist:4353522
WordPress: Twitter time ago
<?php
// Twitter date format example: "created_at": "Mon Jun 27 19:32:19 +0000 2011"
function from_apachedate( $date ) {
list( $D, $M, $d, $h, $m, $s, $y ) = sscanf( $date, "%s %s %2d %2d:%2d:%2d +0000 %4d" );
return strtotime( "$d $M $y $h:$m:$s" );
}
// human_time_diff is a WordPress function
echo human_time_diff( from_apachedate( $item->created_at ), current_time( 'timestamp' ) );
@jeweltheme
jeweltheme / gist:1106d5845db0e81a43c32e57ebd2d1ab
Created May 17, 2017 04:29 — forked from wwdboer/gist:4730303
WordPress: Get image attachments
function get_image_attachments( $size = 'thumbail', $ID ) {
$featured_image_id = get_post_thumbnail_id( $ID );
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $ID,
'exclude' => $featured_image_id
/**
* Wrap embed html with bootstrap responsive embed div
*/
function bootstrap_embed( $html, $url, $attr ) {
if ( ! is_admin() ) {
return "<div class=\"embed-responsive embed-responsive-16by9\">" . $html . "</div>";
} else {
return $html;
}
}
function get_vimeoid( $url ) {
$regex = '~
# Match Vimeo link and embed code
(?:<iframe [^>]*src=")? # If iframe match up to first quote of src
(?: # Group vimeo url
https?:\/\/ # Either http or https
(?:[\w]+\.)* # Optional subdomains
vimeo\.com # Match vimeo.com
(?:[\/\w]*\/videos?)? # Optional video sub directory this handles groups links also
\/ # Slash before Id
@jeweltheme
jeweltheme / gist:fb80063d7aa8e59c99f1cc27890fc10a
Created May 17, 2017 04:33 — forked from wwdboer/gist:4353758
PHP: Get YouTube ID from URL
<?php
function get_ytid( $url ) {
preg_match( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches );
return $matches[0];
}
?>
@jeweltheme
jeweltheme / gist:1aa535d1d211be91e223eb909ad96699
Created May 17, 2017 04:34 — forked from wwdboer/gist:4353695
WordPress: Template redirect
<?php
// In functions.php
add_action( 'template_redirect', 'redirect_template' );
function redirect_template() {
global $post;
if ( is_single() && is_post_type( 'custompost' ) ) {
wp_redirect( home_url() . '/custompost/' );