Some WordPress themes don't support featured images and post thumbnails. Other themes proudly claim to support them, but when you try to display your images the functionality provided is somewhat broken. So the best thing you can do is to implement this feature by yourself. Let's see how.
Showing posts with label php. Show all posts
Object-oriented programming with Wordpress: utopia?
A long debate among Wordpress developers is essentially about the possibility of using truly object-oriented code in their themes and plugins by exploiting the latest features of PHP 5. Most developers who don't are accustomed to OOP in PHP think that OOP in Wordpress is rather difficult to accomplish and, in a certain sense, even counterproductive. But if you take a closer look at the inner structure of the Wordpress core, you'll surely notice the abundance of classes and most of the OOP features we're all used to see in other frameworks. This post is written in the hope of stimulating a renewed interest in OOP with Wordpress.
PHP: converting RSS to JSON
Converting RSS to JSON requires only a sufficient knowledge of the json_encode() function and one of the XML libraries and extensions that come shipped together with PHP. In this post I'll show you a basic routine to convert an RSS feed to a JSON file using the PHP's DOM extension.
PHP: creating your own JSON API
The following video shows how to create and implement your own public API using JSON as the default data exchange format. The core of your API will be made up by PHP. This is a really useful tutorial.
Introduction to the Zend Framework
Many PHP developers who start using the Zend Framework for the first time get always scared of the complexity of this framework. I'd like to say that there's nothing to be worried about. Yes, the Zend Framework is quite complex, but it's perhaps the best documented framework available on the web. This means that there's a reference guide, an online documentation and, above all, a myriad of high-level tutorials available online. In short: all of your present and future questions about this framework have already been answered.
PHP: Twitter widget
I decided to create a PHP widget for Twitter because I was honestly tired of using JavaScript widgets which have the major drawback of causing accessibility problems in those browsers that don't support JavaScript at all. Two things came to my attention: how to replace text URLs with HTML links and how to properly calculate the time elapsed from your latest tweet. I addressed the first problem by using regular expressions, while the second was quickly solved thanks to a careful reading of some good tutorials on the subject. So I created the TwitterWidget class, which is as follows:
Firefox and Wordpress PHP widgets
Today I've tested a Wordpress site in Firefox and I found out that a PHP widget on the sidebar didn't work. More precisely, Firefox showed the raw PHP code instead of the normal HTML generated by PHP. The theme used on this site is Mystique, but I don't think it's caused by the theme, because it worked just fine in all other browsers. I tested it with Firefox for Mac. I don't understand how this is possible, because the page's loading was complete.
jQuery: RSS reader
Fetching a remote RSS feed with jQuery is one of the most frequently asked questions in a million. jQuery can't handle a remote feed by itself due to the same-domain policies of the AJAX standard. For that reason, we need a server-side script which accepts two parameters, namely the absolute URL of the feed and the number of items you want to display. In our example we'll use PHP, obviously after making sure that the passed URL is a valid URL and that we're actually dealing with an RSS feed. Here's the script:
PHP: basic MySQL database class
Before digging into the details of PHP design patterns (including the Singleton pattern), I used to define my database classes as abstract to avoid instantiation. This is a rough solution and it's not really suitable for a MVC environment. However, in this post I'm going to show you a really basic implementation of a basic class that uses a MySQL database.
PHP: cross-platform email validation
An important part of email validation is to check if an host is alive or not. On a Unix platform this can be easily achieved with the PHP's getmxrr() function. Unfortunately, this function is not available on Windows. For that reason, I ran an extensive search on the web to find a script that performs this task in a cross-platform fashion and I found the following script by Setec Astronomy (released under the GPL license):
PHP: Zend framework and include path
Using the Zend framework implies a radical change in the way we usually handle the file structure of our projects. In fact, the Zend framework is based on a strict separation between the core application files and your front-end files, where front-end files means the files that will be served to web browsers. In older versions of PHP web applications, that is, prior to Zend and the MVC design pattern, the file hierarchy was basically contained within the document root of your web server. Supposing that your document root is called public_html, then the old hierarchy was something like the following:
PHP: a DOMNodeList is not an array
Most developers who start using the PHP's DOM extension sometimes get confused by the concept of DOMNodeList. In the DOM terminology, a node list is an ordered collection of nodes which has a length and an item property. The former returns the number of elements contained within the node list, while the latter accepts an integer as its value (starting from 0) by which you can access a given node within that list. The DOM specifications (created by the W3C) state that a node list is actually an interface, not an array. PHP implements this component as a class (object), not as an array (thus following the DOM specifications).
PHP: image gallery widget
When I created this widget, which automatically generates an image gallery from a given path, I was thinking about a possible integration with Wordpress because the public method GalleryWidget::outputGallery() returns a string representation of the HTML contents of the gallery. It's a small, lightweight class that you can also include in your template system with a minimum effort. It uses regular expressions to match all the most popular image extensions, including JPEG, GIF and PNG. Anyway, if anyone knows a way to include it in Wordpress, please let me know. In the meantime, feel free to test it and suggest every possible improvement.
Download
jQuery: AJAX image slideshow
In this post I'm going to show you how to create an image slideshow where all the images are fetched via AJAX. To accomplish this, we'll create a PHP script that loops through all the images of a directory, stores them into an array, sorts this array by date and finally returns a JSON object. If you're not accustomed to PHP programming, don't worry: it's sufficient to say that the main work is done by the json_encode() function combined with the appropriate HTTP header (here set to application/json) and that the PHP script must be put in the same directory of your images. Said that, here's the core of our slideshow:
PHP: autoload and include path
After running an extended web search for the PHP __autoload() magic method, I was struck by the fact that none of the most useful resources found ever mentioned a basic aspect of this method, namely the underlying include path settings. Either we're talking about basic tutorials or
more advanced strategies, nobody seems to take this setting into account. In short, __autoload() works by searching for files with a given class name in your include path. If it doesn't find a file in your include path, it raises an error.
Google Prettify and PHP
I've just finished to make Google Prettify support all the reserved keywords of PHP. Since Prettify already supports PHP, I had to create a new language handler called prepro so that its corresponding CSS class will be lang-prepro. Here's an example:
PHP: handling SimpleXML errors
Some consuming web platforms, such as Twitter, offer a great opportunity to use the SimpleXML library to fetch results from remote using XML files. However, sometimes things go wrong and all you get is a bunch of errors. In the case of SimpleXML, this is mainly due to the fact that a web service may be down so that it cannot satisfy your request. In the case of Twitter, this happens too often. So one day you get your latest tweets or news and the other one you get a PHP warning containing (that's too bad) also some sensitive information about your PHP environment.
PHP: Twitter feed tutorial
In this tutorial I will explain you in details how to parse and format a Twitter RSS feed with your latest tweets. What we need to know is the basic structure of a Twitter feed, its URL and how to convert into HTML links all the plain text URLS contained within the feed. First of all, the basic structure of a Twitter feed URL is
http://twitter.com/statuses/user_timeline/ followed by your Twitter ID and ended with .rss. To get your Twitter ID, simply visit this site and enter your Twitter username into the form. Done that, you're ready to work with a Twitter feed.
Formatting a Wordpress post date in the RSS format
Wordpress automatically formats the post_date field in the wp_posts table using various date formats, including the RSS and Atom ones. The default data type for this field is datetime, which means that the date stored in this field has the format YYYY-MM-DD HH:MM:SS. However, sometimes we may need to format this data type directly, for example if something goes wrong with our RSS or Atom feeds and we actually have to manually create a physical feed. Here's how we can do:
PHP: AJAX and the MVC pattern
If you run a search for AJAX and the MVC pattern, you'll surely notice that the overwhelming majority of results explain you how to create an AJAX-based MVC system but not how to integrate AJAX into an existing one. AJAX is already a three-component system, being based on the interaction between browsers, JavaScript and a web server. The problem with AJAX is that we actually don't know where to put it within an MVC pattern. Do we need an AJAX Model? Do we need an AJAX Controller? Do we need an AJAX View? Since AJAX works in the web browser, and the web browser handles only the View part of our design pattern, it seems obvious that we only need an AJAX View. In other words, we only need that our components return an output that can be handled by JavaScript and AJAX. But here a problem arises: what if our HTML template system is not set to handle such situations?