Archive | Coding RSS feed for this section

How to remove the width and height attribute from images in wordpress

16 Jun

I run into a problem with fitting big images into a single post page. I thought that just using a max-width in a CSS file to limit the image width will be enough, and the browsers will automajically scale the image to a proper dimension which will fit the available space.

I was wrong. The result was images which which where in the proper width but look stretched in their height. It took some time to figure out that the cause of it is that the height attribute set for the image with the original height.

It took some poking into the wordpress code to understand that the width and height attributes are set when the images are inserted into the editor, so I just wrote some lines of code to remove the attributes when the image is being inserted in its full size.

Too lazy to do a proper plugin out of this, but here is the code


add_filter( 'get_image_tag', 'bg_image_downsize',10,6);

function bg_image_downsize($html, $id, $alt, $title, $align, $size) {

 if ($size=='full') {
$pattern = '#width="([a-zA-Z0-9]+)"#';
$html = preg_replace($pattern, '', $html);
$pattern = '#height="([a-zA-Z0-9]+)"#';
$html = preg_replace($pattern, '', $html);
 }

 return $html;
}

Overriding pluggable functions in a plugin

21 Dec

There is a small detail you need to remember when trying to override a pluggable function in a wordpress plugin – The original function will not be overridden in plugin activation time, which means that the following code will make the plugin activation to fail with a redeclaration error message


function override() {
...
}

What you actually have to do to take care of this situation is to protect the function declaration by checking whether the function is already declared


if (!function_exists('override') {

function override() {
...
}

}

How to remove_action which activates an object method

24 Oct

add_action/add_filter can accept either a function name or an object and a method name as the ‘function’ parameter. The problem is that remove_action/remove_filter can remove only actions which use a function name or a static class methos as their ‘function’, object methods type of ‘function’ can not be removed at least in the 2.8 version.

There is no official way to remove, but if needed you can apply the following hack which tries to find the ‘function’ in the internal structures, by looping over all the functions while trying to find the best match and remove it.


// assuming this was the add_action being used

$f =& new Custom_Image_Header();
add_action('admin_menu', array(&$f, 'init'));

// this code will remove it

foreach ($GLOBALS['wp_filter']['admin_menu'][10] as $f => $v) {
  if (substr($f, 0, 23) == 'Custom_Image_Headerinit') {
    remove_action('admin_menu', $f);
  }
}

This code has two important drawbacks

  1. It may remove the wrong object if more then one object of the same class ware added as a function
  2. Similar class names might confuse the code. In this case it might remove call to the ‘nit’ method of the ‘Custom_Image_Headeri’ class

Preventing php/perl script execution of a user uploaded files while keeping them downloadable

16 Sep

You wrote a rather large PHP script which you would like to share with the world, lets say a small wordpress plugin. If you just upload it via the wordpress post publishing interface, you will find out that when you click the link in order to download it, instead of getting the browser’s download interface, the script is actually being executed on the server.\

This is why even small scripts are usually distributed as ZIP files, but if your site is running over an Apache web server there is a small and fast cure for the problem – just add the following line to the .htaccess file (you might need to create it) at the root of your uploaded file directory hierarchy ( [wordpress installation path]/wp-content/uploads in wordpress) :

AddType application/octet-stream .pl .cgi .php

This line will make Apache know that the PHP/PERL/CGI files at that directory should be treated as a simple stream of bytes and it will not try to execute them. On the other hand, the browser will know that it should not try to display the files, and will start a download process.

This is actually a kind of security feature. If you are running a site which lets other users upload files (a wordpress with multiple authors), you don’t want them to be able to upload executable scripts.

What content plugins should genrate in an RSS feed?

11 Sep

Plugins should add content to the RSS feed but only if that is there main function, otherwise they probably should not. Why not? because the main functionality of the RSS feed is to notify a subscriber about new content, while giving him a glimpse of what the content is about.

Most of the plugins which add something to the content displayed on the web page, such as the social bookmarks icons, do not add any new content to the post itself, and therefor they should not be adding anything to the RSS feed.

A typical social bookmarking plugin code looks like

function handle_content($content) {

 $content = $content.'<div><a href="url1"><img src="src1" /></a><a href="url2"><img src="src2" /></a></div>
';
 return $content;
}

Which looks good on the web page, but in the RSS the result will depend on the number of words in the original post. If the original post contained less than 55 words, the extra html which was added by the plugin will be sent as part of the RSS.

The result might be (due to some cleaning that WordPress does)


Original content

<a href="url1"><img src="src1" /></a>
<a href="url2"><img src="src2" /></a>

Which adds two links which are not part of the content. Actually it can be even worse with the extra content being split up and the result is something that resembles junk. That is why the plugin code should look like


function handle_content($content) {

 if  (is_feed()) // do nothing if we are generating a feed
   return $content;

 $content = $content.'<div><a href="url1"><img src="src1" /></a><a href="url2"><img src="src2" /></a></div>
';
 return $content;
}

This way the changes made by the plugin are not seen in the RSS feed. But there always should be an exception, and if the RSS has the full post maybe it does make sense to add the same content to the feed and the post. Add a check of the value of the rss_use_excerpt option, which indicates whether the RSS is full, and the code becomes


function handle_content($content) {

 if  (is_feed() && (get_option('rss_use_excerpt') == 1)) // do nothing if we are generating a feed of excerpts
   return $content;

 $content = $content.'<div><a href="url1"><img src="src1" alt="" /></a><a href="url2"><img src="src2" alt="" /></a></div>
';
 return $content;
}

But what should be done with shortcodes?

The not so awesome bar

25 Aug

I was very excited when the awesome bar was introduced in firefox 3.0, I thought that it will change the way I use my browser – no more automatic googling for sites in which I have been rarely before, but rather first doing a local search in the browser history which at least should be faster.

I was right. The knowledge of the existence of the awesomebar have actually changed the way I use the browser. I no more attempt to guess the initial character in the name of the site but rather some part of it, or an outstanding word in the title. On the other hand, the awesome bar caused me a lot of misery every time I have misspelled something which had no match in the history DB as it would practically freeze the input to the address bar while it was conducting its useless search.

FF 3.5.2 have changed things to the better, as even when there was no match the perceived responsiveness have improved*, but now mozilla guys have publicized an hack which improves the performance of the awesome bar by essentially optimizing its DB. This shows a really sloppy coding practizes by mozilla. If you are going to use a DB embedded in your product, you better have someone how understands the pitfalls of using such a DB. The obvious pitfall in this case is that from time to time the DB has to be optimized in order to have fast responses from the DB, which means in the embedded DB case, that the embedding software have to do the cleanup automatically.

I have read some excuses about the optimization taking long time in which the GUI is frozen, but those are mere excuses as the optimization can be moved to the background as most of the time the browser is just seating idle, or to the startup or shutdown parts of the code. In the worst case FF should at least try to use some heuristic to calculate if an optimization is required and prompt the user that it is about to run one. I find it hard to believe that anyone will complain if this will happen once in two months as upgrades to new dot releases are being pushed in a higher frequency (and maybe it should be combined?) and I haven’t heard any complains about that.

* As a side note, I think that with FF 3.5 mozilla got away without getting the amount of criticism that a commercial company would get for releasing an uncomplete product. 3.5.2 solved a major usability bugs of the awesome bar and the startup time, bugs which were surly known at the time 3.5 was developed but waited another two “dot” releases to be at least partially fixed.

ob_gzhandler should not be used to compress big output as it cuases apache to fail?

25 Aug

Not having any inclanation to look into the PHP and APACHE source code I will have to qualify the statement in the title that it is coming from an observation and should not be treated as a consistent fact.

To make a long story short, I have moved from a shared hosting environment into an unmanaged VPS, which have a relativity small amount of RAM memory – 256M. One of the things that I am running on this VPS is a gregarious based RSS aggregator (pity that this project is virtually dead), the rest were my hebrew wordpress based blog, this wordpress mu based installation, and a mail server.

Once in several days the swap usage of the server will sky rocket and I will have to manually reboot the VPS. Though I have tried to find a common reason to this phenomenon, I could not fins one and I assumed That I am just over using my memory.

I have tried to do various change to the apache parameter controling the number of instances and the time it takes before they restart themselves, but altough there was an improvment, the server will still freeze from time to time.

One day the aggregator stopped giving any output at all. I have made some debugging and found out that what was killing it was an output compression done by calling ob_start(‘ob_gzhandler’) since when I have removed the call everything started working fine.

Then I have spent some time thinking about the “why” since while the prices of bandwidth have fallen through the years it is still nice to save if it is not too difficult. Finally it struck me that the only way the ob_gzhandler handler can work is by allocating memory which is in some way proportional to the amount of output generated, and for a memory starved machine and large output this can be a real killer since, as every software developer should know, there is not a single software on earth which can gracefully deal with shortage of memory resources.

Anyway, since most major web servers include now the capability of compressing the output, maybe it is better to leave it to them instead of implementing it in software, as they mught have a better dedicated code then PHP, and when they perform the compression they have probably cleaned the memory used by the PHP code, so they have more resources.

Wait a moment!, shouldn’t this line of reasoning be applicable to the usage of ob_start in any case? IMHO the answer is yes, and ob_start should be used for the shortest time, and the shortest output you can reasonably manage to get, unless you have idle memory and you don’t want it to feel neglected.