Created
February 21, 2013 00:09
-
-
Save staylor/5000868 to your computer and use it in GitHub Desktop.
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 | |
function get_content_link( &$content, $remove = false ) { | |
if ( empty( $content ) ) | |
return; | |
$lines = explode( "\n", $content ); | |
$line = trim( array_shift( $lines ) ); | |
if ( 0 === stripos( $line, 'http' ) ) { | |
if ( $remove ) | |
$content = trim( join( "\n", $lines ) ); | |
return esc_url_raw( $line ); | |
} else { | |
if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', $content, $matches ) ) | |
return false; | |
return esc_url_raw( $matches[1] ); | |
} | |
} | |
function get_the_link( $id = 0 ) { | |
$post = empty( $id ) ? get_post() : get_post( $id ); | |
if ( empty( $post ) ) | |
return; | |
if ( has_post_format( 'link', $post ) ) { | |
$meta = get_post_format_meta( $post->ID ); | |
if ( ! empty( $meta['url'] ) ) | |
return esc_url_raw( $meta['url'] ); | |
} | |
if ( ! empty( $post->post_content ) ) | |
return get_content_link( $post->post_content ); | |
} | |
function the_link() { | |
echo get_the_link(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment