Category Archives: php

Hide Widgets on Specific WordPress Pages

The new reader centric version of my website launched last night. When I was setting up the new home and about page views, I needed to hide a widget. Normally there is a brief “about me” widget on most pages. This widget allows visitors who came directly to a page or article to learn more about me. Since the about page and homepage have their own dedicated areas for this information, I wanted to hide the “about” widget. Continue Reading

Use PHP to Retrieve All Links on a Page

I was developing a project recently, where I had the need to get all the links on a certain page. I first turned to a JavaScript library, like Jasmine.

I really wanted to do this within my PHP code, so I ended up going with this solution:


$html = file_get_contents('http://www.example.com');

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    echo $url;
}

Test Driven API Development

DotDev has posted a great tutorial on Test Driven API Development. Even though I already develop test driven APIs, I still love reading articles like this to see how others are solving problems.

I have rolled my own Transformers in the past, and I am very excited about the Fractal package they reference in this article. I’m also excited about the API documentation generator, and I think I’m going to give that a try this weekend.

Direct Link

Show Number of Facebook Likes in PHP

If you run a Facebook page, and you are starting to build up some good numbers, you may want to show them off on your website. Facebook allows you to embed a Facebook widget on your homepage, but that means you are using third-party scripts, and you can’t control the design. Here is a simple PHP snippet for displaying your Facebook Likes on your website.


$facebook_page_id = "127012630783508";
$results = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=${facebook_page_id}");
echo $results->page->fan_count;