/i', $post->post_content, $matches)){; $image_url = $matches [1] [0]; } return $image_url; } //Custom Excerpt Length if(!function_exists( 'optimizer_excerptlength_teaser' ) ){ function optimizer_excerptlength_teaser($length) { return 20; } } if(!function_exists( 'optimizer_excerptlength_index' ) ){ function optimizer_excerptlength_index($length) { return 12; } } if(!function_exists( 'optimizer_excerptmore' ) ){ function optimizer_excerptmore($more) { return '...'; } } function optimizer_excerpt($length_callback='', $more_callback='') { if(function_exists($length_callback)){ add_filter('excerpt_length', $length_callback); } if(function_exists($more_callback)){ add_filter('excerpt_more', $more_callback); } $output = get_the_excerpt(); $output = apply_filters('wptexturize', $output); $output = apply_filters('convert_chars', $output); $output = '

'.$output.'

'; echo $output; } //Get Attachment data function optimizer_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'href' => esc_url(get_permalink( $attachment->ID )), 'src' => $attachment->guid, 'title' => $attachment->post_title ); } //hex to rgb function function optimizer_hex2rgb($hex) { $hex = str_replace("#", "", $hex); if(strlen($hex) == 3) { $r = hexdec(substr($hex,0,1).substr($hex,0,1)); $g = hexdec(substr($hex,1,1).substr($hex,1,1)); $b = hexdec(substr($hex,2,1).substr($hex,2,1)); } else { $r = hexdec(substr($hex,0,2)); $g = hexdec(substr($hex,2,2)); $b = hexdec(substr($hex,4,2)); } $rgb = array($r, $g, $b); return implode(",", $rgb); // returns the rgb values separated by commas } /*Optimizer Color Sanitization*/ function optimizer_sanitize_hex( $color = '#FFFFFF', $hash = true ) { $color = trim( $color ); $color = str_replace( '#', '', $color ); if ( 3 == strlen( $color ) ) { $color = substr( $color, 0, 1 ) . substr( $color, 0, 1 ) . substr( $color, 1, 1 ) . substr( $color, 1, 1 ) . substr( $color, 2, 1 ) . substr( $color, 2, 1 ); } $substr = array(); for ( $i = 0; $i <= 5; $i++ ) { $default = ( 0 == $i ) ? 'F' : ( $substr[$i-1] ); $substr[$i] = substr( $color, $i, 1 ); $substr[$i] = ( false === $substr[$i] || ! ctype_xdigit( $substr[$i] ) ) ? $default : $substr[$i]; } $hex = implode( '', $substr ); return ( ! $hash ) ? $hex : '#' . $hex; } // allow script & iframe tag within posts function optimizer_allow_html( $allowedposttags ){ global $allowedposttags; $allowedposttags['script'] = array( 'type' => true, 'src' => true, 'height' => true, 'width' => true, ); $allowedposttags['form'] = array( 'id' => true, 'class' => true, 'action' => true, 'method' => true, 'name' => true, 'style' => true, 'target' => true, 'novalidate' => true, ); $allowedposttags['input'] = array( 'id' => true, 'class' => true, 'name' => true, 'style' => true, 'placeholder' => true, 'tabindex' => true, 'type' => true, 'value' => true, ); $allowedposttags['button'] = array( 'id' => true, 'class' => true, 'name' => true, 'style' => true, 'tabindex' => true, 'type' => true, 'value' => true, ); return $allowedposttags; } add_filter('wp_kses_allowed_html','optimizer_allow_html', 1); //**Return an ID of an attachment by searching the database with the file URL (Inexpensive query)**// function optimizer_attachment_id_by_url( $url ) { $parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url ); $this_host = str_ireplace( 'www.', '', parse_url( esc_url(home_url()), PHP_URL_HOST ) ); $file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) ); if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) { return; } global $wpdb; $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) ); return $attachment[0]; } //Get Image alt from image src function optimizer_image_alt( $attachment ) { $imgid = optimizer_attachment_id_by_url($attachment); if($imgid){ $imgaltraw = wp_prepare_attachment_for_js($imgid); $imgalt = $imgaltraw['alt']; if(!empty($imgalt)){ $imgalt = 'alt="'.$imgaltraw['alt'].'"'; } }else{ $imgalt = ''; } return $imgalt; }