Posts Tagged ‘PHP’

FavIcons, Apple Touch Icons, Site Thumbnail Images, and WordPress

Saturday, 26 February 2011

While someone somewhere has surely written a nice clean, unified explanation on associating favicons, Apple Touch icons, and site thumbnail images with WordPress 'blogs, I've not seen it; and, when it comes to supporting site thumbnail images in WordPress themes, I have seen an awful lot of discussion that is unclear, and PHP code that is inflexible, breakable, or already broken. Perhaps I'm now about to contribute to the problem, but I'm here going to make my own attempt at discussion.

[This entry was written at the suggestion of the Woman of Interest, but she's really not to blame for how it turned-out.]

FavIcons, Apple Touch Icons, and Site Thumbnail Images

First, I'll try to explain the rôles and invocations of these images without peculiar reference to WordPress or to PHP.

Working Code and How to Use It

I think that a great many people are just going to want code that they can drop into a theme, and a practical explanation of how to use that code, so I'll provide such code, with an explanation of how to use it.

PHP and WordPress Coding Issues

A few hardy or lost souls are going to want to know what motivated that code, how it actually does what it does, or what I see might be done differently. (Some of them might do a rather better job than have I.) For those readers, I have prepared a very boring discussion.

WordPress — Relative Specification of Directory

Tuesday, 19 August 2008

Amongst other things, I've been hacking at the theme for this 'blog, fixing bugs and simply tweaking things more to my liking. While I'm at this task, visitors are occasionally going to find the 'blog either cosmetically flawed or just plainly dysfunctional. (And, tragically, they may find it thus after I'm finished.)

In the course of my hacking, I wanted to use the PHP function file_exists() to check amongst files within the theme directory. One possibility would be to hard-code a relative specification of the theme directory (the theme slug appended to wp-content/themes/), but that approach isn't robust; it would increase the ways in which the theme could be broken by external change or by cloning.

While WordPress has a function call to return a specification of the directory, it does so in the form of an HTTP URL, notwithstanding that there is a call that explicitly requests the theme URL, to the same effect; meanwhile, PHP function file_exists() will choke on a URL. I went prowling around the WordPress documentation, and did some code-diving, but didn't find a function call or global variable for anything more like a relative specification of the theme directory.

However, there is a function call for the URL of the 'blog — get_bloginfo('template_directory'). My hack to get the relative specification, which could preface file-specs that could be usefully handled by file_exists(), was

substr(get_bloginfo('template_url'),strlen(get_bloginfo('url')) + 1)
The + 1 is to account for the fact that returns to queries to get_bloginfo() for directory URLs don't have a terminal slash.

A Useful Bit o' PHP Code, Set Right

Monday, 16 June 2008

I came upon someone's ancient 'blog entry in which he or she attempted to present what would be a useful PHP function. Unfortunately, the code has a few bugs.

A dynamic webpage may seek data from various sources, including data passed by GET and POST methods (which is how web forms normally), persistent data in cookies (stored on the client but provided to the server with each visit), and persistent data on the server.

Towards that end, PHP maintains five associative arrays: $_GET, $_POST, $_COOKIE, $_REQUEST, and $_SESSION. ($_REQUEST combines the contents of $_GET, $_POST, and $_COOKIE.) To access a variable named user sent by POST method, one would refer to $_POST["user"], and so forth.

The 'blog entry in question may have been written before $_REQUEST was introduced; in any event, the author had two good ideas:
  1. Avoid errors resulting from trying to access variables that don't actually exist. If no variable user was passed by POST, then $_POST["user"] throws an error. To avoid that sort of thing, the author checks for the presence of the variable before attempting to access it.
  2. Combine the variables in $_SESSION, as well as those in $_GET, $_POST, and $_COOKIE. Indeed, session data is more analogous to cookie data than is cookie data to data transmitted by $_GET or by $_POST.
The problems with the actual code are these:
  • If the server is not maintaining session data, then the attempt to use $_SESSION will itself cause an error.
  • There is an attempt to get cookie data from the array $_SESSION.
  • In the aforementioned attempt, the array is treated as a function.
Here's a version of the code that fixes those problems:
function getvar($var_name)
  if (array_key_exists($var_name, $_GET) == TRUE) $ret_value = $_GET[$var_name];
  else if (array_key_exists($var_name, $_POST) == TRUE) $ret_value = $_POST[$var_name];
  else if (session_id() != "")
    if (array_key_exists($var_name, $_SESSION) == TRUE) $ret_value = $_SESSION[$var_name];
  else if (array_key_exists($var_name, $_COOKIE) == TRUE) $ret_value = $_COOKIE[$var_name];
  else $ret_value = ""; 
  return $ret_value;
PHP also provides analogous associative arrays for other global variables, but what unites the variable types of the five here is that they are commonly used in session-tracking — keeping data associated with a specific visitor as she moves through one's site. Possibly, getvar would be better named something else, if not distinguished by being made a member of some class of objects.

A Little Illness and a Little PHP

Monday, 31 March 2008

As well as continuing to desire an unusual amount of sleep, I notice that I have a persisting sensation of white noise. I'm definitely ill, though the symptoms remain low-level.

I did, at least, get PHPMailer working for me at That's good, because I've been approached to creäte a small website for a client, which would use such functionality, and FourBucks are probably the folks to host it, as its traffic should be relatively light and as the client apparently only wants one domain.

Crawling Back into Bed

Saturday, 29 March 2008

I may have some low-grade illness. I fell asleep relatively early and quite quickly on Thursday night. Though yester-day morning I awoke from a dream that had left me furious and nauseated (it involved child abuse), I thought that I'd otherwise got enough sleep. But I fell asleep again in mid-after-noon, thinking that I'd nap for perhaps 20 minutes — I left my computer, albeït in its backpack, on the foot of my bed — and, instead, I slept until late at night. And for the last few hours I have been increasingly sleepy.

In other thrilling news, I'm having trouble getting various pieces of PHP code working for me on the FourBucks server that I use for two of my domains, but I'm largely working blind, since I don't have administrative privileges for the operating system itself. One of my subscribers here hasn't been able to add his OpenID to his account here. And I've not been able to get PHPMailer working at