How to Return the First Link in WordPress Post

x theme
(Last Updated On: July 10, 2014)

With the help of the following code snippet, you will be able to get or return the first URL in the post meta (in case link exists) in the post content. This code snippet is perfect for the post format “Link”.

 

If no URL found, it will fall back to the post permalink.

function return_link_url() {
   //Store the Post Content in a variable
   $content = get_the_content();

   //Check if URL exists
   $check_url = get_url_in_content($content);

  //Return the results
  return($check_url)? $check_url : apply filters(
               'the_permalink', get_permalink());
}

Related posts:

About the author

admin

Leave a Reply

Your email address will not be published. Required fields are marked *