0

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.

1
  • preg_match_all() returns the number of matches or FALSE on errror, your link will be in the $match array not in $link.
    – totels
    Commented Nov 10, 2012 at 1:04

2 Answers 2

2

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
        )
)
2
  • 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
    Commented Nov 10, 2012 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
    Commented Nov 10, 2012 at 14:25
0

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.

1
  • Yeah, I could/should do an of these. But I've got 500+ posts/links in my bookmark manager now from years ago.
    – saltcod
    Commented Nov 10, 2012 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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