Skip to content

Instantly share code, notes, and snippets.

@zugrina
zugrina / 0_reuse_code.js
Created May 31, 2014 19:04
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
@zugrina
zugrina / display_favorite_tweets.php
Last active December 16, 2015 15:49
Display your Favorite Tweets (WordPress Transients API – Practical example - http://www.tailored4wp.com/get-a-better-performance-with-wordpress-transients-api-501/)
<?php
$transient = get_transient( 'twitter_favorites' );
if ( empty( $transient ) ){
$username = 'youracc';
$number_of_favorites = 10;
$response = wp_remote_retrieve_body(wp_remote_get("https://api.twitter.com/1/favorites.json?count={$number_of_favorites}&screen_name={$username}"));
$data = json_decode($response);
$html_output = '<ul>';
foreach ( $data as $tweet ) {
$html_output .= '<li>'.$tweet->user->name.': '.$tweet->text.'</li>';
@zugrina
zugrina / get_image_id_by_url.php
Last active December 16, 2015 15:49
Retrieve Attachment ID from Image URL
<?php
function get_image_id_by_url($image_url) {
global $wpdb;
$prefix = $wpdb->prefix;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM " . $prefix . "posts" . " WHERE guid='%s';", $image_url ));
return $attachment[0];
}
@zugrina
zugrina / is_page_template_by_id.php
Created April 25, 2013 10:07
Check if some page has template
function is_page_template_by_id( $template = '', $page_id ) {
if ( ! is_page() )
return false;
$page_template = get_page_template_slug( $page_id );
if ( $template == $page_template )
return true;
if ( 'default' == $template && !$page_template )
return true;
return false;
}
@zugrina
zugrina / top_parent.php
Last active December 16, 2015 15:49
Return top parent ID of page/post
function top_parent(){
global $post;
if ($post->post_parent) {
$ancestors = get_post_ancestors( $post->ID );
$post_parent_id = array_pop( $ancestors );
return $post_parent_id;
}else{
return $post->ID;
}
}
function add_menu_parent_class( $items ) {
$parents = array();
foreach ( $items as $item ) {
if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
$parents[] = $item->menu_item_parent;
}
}
foreach ( $items as $item ) {
if ( in_array( $item->ID, $parents ) ) {