Last active
December 14, 2015 04:39
-
-
Save oterox/5029592 to your computer and use it in GitHub Desktop.
Worpdress extra functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PXHelper by Javier Otero | |
* | |
* GNU General Public License, version 2 | |
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
* | |
*/ | |
// --------------------------------------------------- | |
// Get post by title | |
// --------------------------------------------------- | |
function px_get_post_by_title($page_title, $output = OBJECT) { | |
global $wpdb; | |
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title )); | |
if ( $post ) | |
return get_post($post, $output); | |
return null; | |
} | |
// --------------------------------------------------- | |
// Get page ID by Slug | |
// --------------------------------------------------- | |
function px_get_pageID_by_slug($page_slug) { | |
$page = get_page_by_path($page_slug); | |
if ($page) { | |
return $page->ID; | |
} else { | |
return null; | |
} | |
} | |
// --------------------------------------------------- | |
// Get permalink by Slug | |
// --------------------------------------------------- | |
function px_get_page_permalink_by_slug($page_slug) { | |
$page = get_page_by_path($page_slug); | |
if ($page) { | |
return get_permalink($page->ID); | |
} else { | |
return null; | |
} | |
} | |
// --------------------------------------------------- | |
// Limit string words | |
// --------------------------------------------------- | |
function px_limit_words($string, $word_limit) | |
{ | |
$words = explode(' ', $string, ($word_limit + 1)); | |
if(count($words) > $word_limit) | |
array_pop($words); | |
return implode(' ', $words); | |
} | |
// --------------------------------------------------- | |
// Get ID for page using a specific template | |
// --------------------------------------------------- | |
function px_get_page_id_by_template( $template_file ){ | |
$pages = get_pages(array( | |
'meta_key' => '_wp_page_template', | |
'meta_value' => $template_file, | |
'hierarchical' => 0 | |
)); | |
return $pages[0]->ID ; | |
} | |
// --------------------------------------------------- | |
// Get link for page using a specific template | |
// --------------------------------------------------- | |
function px_get_page_by_template( $template_file ){ | |
$pages = get_pages(array( | |
'meta_key' => '_wp_page_template', | |
'meta_value' => $template_file, | |
'hierarchical' => 0 | |
)); | |
return get_permalink($pages[0]->ID) ; | |
} | |
// --------------------------------------------------- | |
// Check for subpages | |
// --------------------------------------------------- | |
function px_is_subpage( $template_file ) { | |
global $post; | |
$pages = get_pages(array( | |
'meta_key' => '_wp_page_template', | |
'meta_value' => $template_file | |
)); | |
if ( is_page() && $post->post_parent == $pages[0]->ID ) { // test to see if the page has a parent | |
return true; // return the ID of the parent post | |
} else { // there is no parent so ... | |
return false; // ... the answer to the question is false | |
} | |
} | |
// --------------------------------------------------- | |
// Get Gravatar url | |
// --------------------------------------------------- | |
function px_get_gravatar_url( $email, $size ) { | |
$hash = md5( strtolower( trim ( $email ) ) ); | |
return 'http://gravatar.com/avatar/' . $hash . '?s=' . $size . '&d=404'; | |
} | |
// --------------------------------------------------- | |
// Get first image from post | |
// --------------------------------------------------- | |
function px_get_first_image() { | |
global $post, $posts; | |
$first_img = ''; | |
ob_start(); | |
ob_end_clean(); | |
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); | |
$first_img = $matches[1][0]; | |
if(empty($first_img)) { | |
$first_img = "/path/to/default.png"; | |
} | |
return $first_img; | |
} | |
// --------------------------------------------------- | |
// Generates the breadcrumb | |
// --------------------------------------------------- | |
function px_get_breadcrumb() { | |
global $post; | |
$trail = ' » '; | |
$page_title = get_the_title($post->ID); | |
if($post->post_parent) { | |
$parent_id = $post->post_parent; | |
while ($parent_id) { | |
$page = get_page($parent_id); | |
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a> » '; | |
$parent_id = $page->post_parent; | |
} | |
$breadcrumbs = array_reverse($breadcrumbs); | |
foreach($breadcrumbs as $crumb) $trail .= $crumb; | |
} | |
$trail .= '<span>'. $page_title . '</span>'; | |
$trail .= ' '; | |
return $trail; | |
} | |
// --------------------------------------------------- | |
// Returns the english ordinal for a number | |
// --------------------------------------------------- | |
function px_ordinal( $number ) { | |
$ends = array('th','st','nd','rd','th','th','th','th','th','th'); | |
if (($number %100) >= 11 && ($number%100) <= 13) | |
$abbreviation = $number. 'th'; | |
else | |
$abbreviation = $number. $ends[$number % 10]; | |
return $abbreviation; | |
} | |
// --------------------------------------------------- | |
// twitter feeds | |
// --------------------------------------------------- | |
function px_twitter_feed( $twitter_user, $limit){ | |
$tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/" . $twitter_user . ".json?count=".$limit)); | |
echo '<ul class="tweet">'; | |
for ($i=1; $i <= $limit; $i++){ | |
echo '<li><p>'.$tweet[($i-1)]->text.'</p>'; | |
echo '<a href="http://www.twitter.com/' . $twitter_user . '" target="_parent">'; | |
echo '<span class="time">Posted by ' . $twitter_user. ', '. date("M d",strtotime($tweet[($i-1)]->created_at)).'</span>'; | |
echo '</a>'; | |
echo '</li>'; | |
} | |
echo '<ul>'; | |
echo '<a href="http://www.twitter.com/' . $twitter_user . '" class="sprites-blue small-arrow-box linkbutton" target="_blank"><span>@'.$twitter_user.'</span></a>'; | |
} | |
// --------------------------------------------------- | |
// flickr feeds | |
// --------------------------------------------------- | |
function px_flickr_feed( $flickr_id, $limit){ | |
if( '' != $flickr_id ){ | |
$flickr_json = file_get_contents("http://api.flickr.com/services/feeds/photos_public.gne?id=". $flickr_id ."&format=json&nojsoncallback=1"); | |
} else { | |
$flickr_json = file_get_contents("http://api.flickr.com/services/feeds/photos_public.gne?id=38386919@N07&format=json&nojsoncallback=1"); | |
} | |
if ($flickr_json === false) { | |
echo '<p>Flickr API error.Try later.</p>'; | |
} else { | |
$flickr = json_decode($flickr_json); | |
$flickr_url = $flickr->link; | |
echo '<ul>'; | |
for ($i=1; $i <= $limit; $i++){ | |
$img = $flickr->items[($i-1)]->media->m; | |
$img_s = str_replace('_m.jpg','_s.jpg',$img); | |
$img_b = str_replace('_m.jpg','_b.jpg',$img); | |
$img_title = $flickr->items[($i-1)]->title; | |
echo '<li><a class="fancybox" href="' . $img_b . '" title="'.$img_title.'"><img src="' . $img_s . '" alt="'.$img_title.'" width="37" height="37" /></a></li>'; | |
} | |
echo '</ul>'; | |
} | |
echo '<a href="'.$flickr_url.'" class="sprites-blue small-arrow-box linkbutton"><span>our photo stream</span></a>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment