Created
March 19, 2017 13:20
-
-
Save 5A5K1A/5dffd789f24b5c255f07324aeb23134b to your computer and use it in GitHub Desktop.
WordPress usefull utils / extra methods
This file contains hidden or 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 | |
/** | |
* Strips url from obsolete stuff, for prettier display | |
* @param string $url The url | |
* @return string The cleaned up & pretty url for displaying | |
*/ | |
function studio_strip_url( $url ) { | |
return rtrim( str_replace(array( 'https://', 'http://', 'mailto:', 'tel:', 'www.' ), '', $url), '/' ); | |
} | |
/** | |
* Adds a <em> for easy styling (emphasized text should be between asterisks) | |
* @param string $string The string | |
* @return string The styled string | |
*/ | |
function studio_emphasize( $string ) { | |
return preg_replace( '#\*(.*?)\*#', '<em>$1</em>', $string ); | |
} | |
/** | |
* Creates a link with attributes (if provided) => no more dirty HTML in php | |
* @param string $url The url (can be post id too) | |
* @param string $text The text | |
* @param array $attr The attribute | |
* @return string The compiled <a href...> | |
*/ | |
function studio_get_link( $url, $text, $attr = NULL ) { | |
// early exit on no values | |
if( empty($url) || $url == 'mailto:' || $url == 'tel:' ) { return; } | |
// check if url is just a post id | |
if( is_int($url) ) { | |
$post_id = $url; | |
$url = get_the_permalink( $post_id ); | |
// early exit | |
if( empty($url) ) { return; } | |
} | |
// add prettified url as text, if none provided | |
if( empty($text) ) { | |
$text = studio_strip_url( $url ); | |
} | |
// setup start of a href | |
$html = '<a href="'.str_replace( ' ', '', $url ).'"'; | |
// add attributes | |
foreach( $attr as $name => $value ) { $html .= ' '.$name.'="'.$value.'"'; } | |
// finish off the link | |
$html .= '>'.$text.'</a>'; | |
// and return | |
return $html; | |
} | |
function studio_link( $url, $text, $attr = NULL ) { | |
echo studio_get_link( $url, $text, $attr = NULL ); | |
} | |
/** | |
* Adds an extra class on content p's | |
* @param string $content The content | |
* @param string $class The class | |
* @return string Pretty content with added class | |
*/ | |
function studio_add_content_class( $content, $class = NULL ) { | |
$pretty_content = apply_filters( 'the_content', $content ); | |
return str_replace( '<p>', '<p class="'.$class.'">', $pretty_content ); | |
} | |
/** | |
* Gets the pretty/ formatted date | |
* @param integer $post_id The post identifier | |
* @param integer $limit The limit of days ago | |
* @return string Prettified date display | |
*/ | |
function studio_get_the_timetoshow( $post_id = NULL, $limit = 10 ) { | |
// get current post ID if none provided | |
if( $post_id == NULL ) { $post_id = get_the_ID(); } | |
// early return if no ID | |
if( empty($post_id) ) { return; } | |
// get post date | |
$post_date = get_the_date( 'Y-m-d H:i:s', $post_id ); | |
// get limit date | |
$max_date = date( 'Y-m-d H:i:s', strtotime('-'.$limit.' days', strtotime('now')) ); | |
// check if postdate is within limit | |
if( $max_date < $post_date ) { | |
// show number of days ago | |
$current_date = current_time('timestamp'); | |
$days_ago = human_time_diff( get_the_time('U', $post_id), $current_date ); | |
$time_toshow = $days_ago.' '.__('geleden', 'od'); | |
} else { | |
// show date | |
$time_toshow = get_the_time( get_option('date_format'), $post_id ); | |
} | |
return $time_toshow; | |
} | |
function studio_the_timetoshow( $post_id = NULL, $limit = 10 ) { | |
echo studio_get_the_timetoshow( $post_id, $limit ); | |
} | |
/** | |
* Formats the number to a pretty format | |
* @param integer $number The number | |
* @param integer $decimals The number of decimals | |
* @return string Formatted number with dot as thousands separator and comma as decimals separator | |
*/ | |
function studio_get_pretty_number( $number, $decimals = 0 ) { | |
return number_format($number, $decimals, ',', '.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment