Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to improve a bookmarks manager/theme I've built (http://bookmarkie.waterstreetgm.org).

Right now, each post is simply a link:

<a href="http://tumblr.everlane.com/page/4">Everlane Tumblr</a>. ie: that one single link is the entire content of the_content().

Easy enough and works great. Next, I want to be able to get a screenshot of the site via WordPress.com's mShots.

Problem is, I can't seem to extract that link from the_content() and feed it into the <img src="">.

If that wasn't clear, here's what I'm trying to do:

<img src="http://s.wordpress.com/mshots/v1/the-link-extracted-from-the_content()/?w=1024" />

I've tried severalvariations on this:

$content = get_the_content();
$link = preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $match);

But I can't achieve the basic task of extracting that link from the_content(). At the moment, I simply get a formatted link output. I can't find a way to grab the url instead of grabbing the whole link.

Thanks for any suggestions.

share|improve this question
    
preg_match_all() returns the number of matches or FALSE on errror, your link will be in the $match array not in $link. –  totels Nov 10 '12 at 1:04
add comment

2 Answers

up vote 2 down vote accepted

You're just looking in the wrong place:

php > $txt = '<a href="http://tumblr.everlane.com/page/4">Everlane Tumblr</a>';
php > $matches = array();
php > echo preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $txt, $matches);
1
php > print_r($matches);
Array
(
    [0] => Array
        (
            [0] => http://tumblr.everlane.com/page/4
        )

    [1] => Array
        (
            [0] => 4
        )
)
share|improve this answer
    
So close! This seems to be outputting the href with the number 1 in front of it. Like this: 1http://google.com. I can't figure out where it's coming from. –  saltcod Nov 10 '12 at 1:33
    
Turns out that number '1' was a lesson in return vs echo. The 1 was being echoed, while what I wanted was to return it to hand it to another function. Thanks for the help with this one! –  saltcod Nov 10 '12 at 14:25
add comment

Instead of putting a link in the content area why don't you just put the URL in a custom field. Then you can construct the heading for the post and the tag for screenshot image within your template.

Even better would be to create a custom post type that didn't have a title or content area and just a custom meta box with a field for the URL.

share|improve this answer
    
Yeah, I could/should do an of these. But I've got 500+ posts/links in my bookmark manager now from years ago. –  saltcod Nov 10 '12 at 0:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.