Adding html to your WordPress widget title

I was recently working on a custom WordPress widget where I wanted to include an image in the widget title. For security reasons, WordPress sanitizes the html so it was not working for me. I searched and searched on the internets but ran into solutions that were bulky and didn’t work for me. After looking into the text widget source code which allows html, I found this useful filter:

apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );

I adapted it to my WordPress plugin for the widget like so:

$instance['title'] = '<img src="https://www.some_website.com/images/logo.png">My Logo';
apply_filters( 'widget_text', $instance['title'] );

And that does it!

Parsing XML data from hostip.info API service with PHP

If you ended up here, then you most likely experienced what I did in trying to figure out how to parse the XML data from the hostip.info web API service.

Basically, it is down to the namespaces that are not accessible by normal means.

Here is the basic XML response from a typical hostip.info call such as http://api.hostip.info/?ip=[IP-ADDRESS]

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
  3.    <gml:description>This is the Hostip Lookup Service</gml:description>
  4.    <gml:name>hostip</gml:name>
  5.    <gml:boundedBy>
  6.       <gml:Null>inapplicable</gml:Null>
  7.    </gml:boundedBy>
  8.    <gml:featureMember>
  9.       <Hostip>
  10.          <ip>12.215.42.19</ip>
  11.          <gml:name>Sugar Grove, IL</gml:name>
  12.          <countryName>UNITED STATES</countryName>
  13.          <countryAbbrev>US</countryAbbrev>
  14.          <!-- Co-ordinates are available as lng,lat -->
  15.          <ipLocation>
  16.             <gml:pointProperty>
  17.                <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
  18.                   <gml:coordinates>-88.4588,41.7696</gml:coordinates>
  19.                </gml:Point>
  20.             </gml:pointProperty>
  21.          </ipLocation>
  22.       </Hostip>
  23.    </gml:featureMember>
  24. </HostipLookupResultSet>

Continue reading Parsing XML data from hostip.info API service with PHP

Sorting 2D associative arrays in PHP

Surprisingly, it took me a long time to find this solution so I decided to post it for anyone who had a situation similar to mine.

Let’s say you have the following 2D associative array in PHP of fruits and their corresponding prices and you want to have them sorted by price:

$fruits = array
(
    [0] => array
        (
            ['product'] => 'banana',
            ['price']     => 2.99
        ),
    [1] => array
        (
            ['product'] => 'apple',
            ['price']     => 1.99
        ),
    [2] => array
        (
            ['product'] => 'durian',  //these smell by the way - i do not know how people can like them =)
            ['price']     => 19.99
        ),
    [3] => array
        (
            ['product'] => 'starfruit', //not too stinky but still stinky nonetheless
            ['price']     => 5.99
        )
);

Continue reading Sorting 2D associative arrays in PHP

How to convert MySQL timestamp to PHP date type

Let’s say you have the following PHP code that extracts the date from the times table in your MySQL database. The date is of timestamp type which has the following format: ‘YYYY-MM-DD HH:MM:SS’ or ‘2008-10-05 21:34:02.’

  1. $res = mysql_query("SELECT date FROM times;");
  2. while ( $row = mysql_fetch_array($res) ) {
  3.    echo $row['date'] . "<br />";
  4. }

Continue reading How to convert MySQL timestamp to PHP date type