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;
}

The proper way to add border to images in a link when it is being hovered

2 Jun

I don’t like to add special effects when a link is being hovered, as I think that the reader should be able to identify links without moving the mouse, and when the courser is over a link it should be enough that the cursor changes from an “arrow” to a “hand”.  Some people think otherwise and most of them like to add some kind of border around the link when it is being hovered.

The problem with adding a border around a link is that it expends the area the browser reserves for the link and cause a redraw of the content. Most of the times when the link is a textual link there is no ill effect, but when the link is actually an image the content around it will “jump” to accommodate the new size of the link (image + border).

The best way to avoid the “jump” is to preallocate an empty space around the image which will be used to display the border.

For example if our current CSS rules are


a:hover  {text-decoration:none;}
.entry a:hover {border-bottom:1px dotted; }

Should be changed to


a:hover  {text-decoration:none;}
 .entry a:hover {border-bottom:1px dotted; padding-bottom:0;}
 .entry a {padding-bottom:1px;}

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() {
...
}

}

Author slug plugin

5 Nov

The author slug wordpress plugin adds the ability to control the slug associated with an author, in a similar way in which you can control the slug of a category.

It is not compatible with wordpress MU.

Usage

After activation a new field titled “Slug” will be added in the user profile page when an administrator is editing the profile. To change the slug just enter the new slug and submit.

If the requested slug already exists, a slug will be generated in the form slug-n.

if the slug field is empty, the slug will be reset to the wordpress defualt.

If you find it useful, don’t be to shy to

License

GPLv3

WordPress version compatibility

Was tested against 2.8, but should work with earlier versions

Installation

1. Download the plugin and unzip it (or not, if you are going to use the built in wordpress plugin installer)
2. Upload the unzipped folder authorslug to your plugins directory.
3. Activate the plugin through the ‘Plugins’ menu in WordPress

Changelog

  • 1.0

Initial release.

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

Are child themes the answer to the task of maintaining wordpress mu themes

20 Oct

Being between projects, I am amusing myself by trying to revive the deprecated “almost spring” theme as a child theme of k2. While working on it, suddenly it occurred to me that it might actually be a great idea to convert all the themes that I have at this wordpress mu installation to be children of the k2 theme. Done right it will mean that there is only one theme to upgrade and check when there is a new version of mu, and it will be a lot easier to write plugins which affect all the themes.

write a lot of code, add multitude of options, spice up the interface and win prize?

29 Sep

No disrespect to the efforts of the developers, and the solid reasoning of the judges, but in my humble opinion the winner of the 2009 wordpress plugin competition – the section widget which

provides a way to display section specific content on your WordPress sidebar with an easy to use interface. This new version comes with tabs support, so creating your tabbed section widget is just a few clicks away.

is not good enough to receive the prize.

I only needed to look at the screen shots to decide that I’m not going to recommend this plugin to any one

Who are going to be the users of this plugin? This screenshot alone displays more hardcore wordpress buzzwords than the “all I want is a simple blog” crowd understands, while it might be easier for the WP focused developers to simply add new sidebar for the places where the normal one is not good enough.

Oh, it even worse than that. When I want to hide a widget from the sidebar at a specific page, I just use CSS. It might not be the ideal solution as it wastes some bandwidth and CPU cycles, but your time is probably much more expensive than the cost of bandwidth and CPU.

In my opinion this type of contest should be decided on mainly two factors:

  • Does it solve a real life problem for many WP users
  • Can a non geek teenager use the plugin successfully without help

Based on this criteria, my favorites were

  • Advanced Export for WP and WPMU which solves the problem of exporting big blogs from shared hosting were you don’t have a direct access to the DB. I only wish there was also a simple way to automate the process.
  • One time password. In a world were we blog from a coffee shop via unsecured WiFi it is actually a security madness to use our password (which we probably use at other sites as well). A one time password is probably the best solution after installing an SSL for the blog. I wish it could have been integrated with the FTP server.

Searchbox plugin – Add search to posts and pages

28 Sep

The searchbox wordpress plugin adds a seachbox short code which generates a search form when the page or post is displayed.

It is wordpress MU compatible both when used as regular plugin and as an MU plugin.

Typical usage: [searchbox]

If you find it useful, don’t be to shy to

Parameters

  • class

    Can be used to add a name of a CSS class for the form
    Example: [searchbox class=search]

  • plugins

    Can be used to apply the get_search_form filter to the form. Might be useful if you have some plugins which add extra functionality to the search widget.
    Example: [searchbox plugins=1]

  • Full shortcode format

    Can be used to inser text (or any HTML) in the form above the input fields. Usefull if you want that text to be styled differently.
    Example: [searchbox]Use the search Lok[/searchbox]

styling

The form do not reuse the id’s of the normal seach form and therefor it should result in a bare form for most themes.
If you wish to style the form you should specify your own class for the form with the class attribute.

The classes specified for the different sections of the form are

  • searchtext – The class of the text
  • searchbox – The class of the input box
  • searchsubmit – The class of the submit bottun

Installation

1. Download the plugin and unzip it (or not, if you are going to use the built in wordpress plugin installer)
2. Upload the unzipped folder searchbox to your plugins directory.
3. Activate the plugin through the ‘Plugins’ menu in WordPress

Changelog

  • 1.0

Initial release.

Suprising things no one bothered to tell me about custom 404 pages

27 Sep

I thought that 404 pages, which indicates that no content was found at the specified address, are about the most trivial aspect of the web, turns out I was wrong. A couple of weeks ago  a client asked me to add a 404 page to his WordPress theme, and since I like to leave as much control as I can at the hands of the site owner (reduces my headaches) I had to figure out how to integrate it into WordPress, and today I read one of the Firefox developers mentioning that he works on how Firefox should handle 404 pages.

I was surprised to learn that internet explorer and chrome will simply ignore your custom 404 page if it is shorter than 512 bytes, assuming that such a small message was automatically generated by the server and do not contain any useful information to the user. Therefor, if your 4040 page is shorter than 512, you should add some HTML comments to it to make it bigger.

Somewhat less surprising, but very interesting, is an article in a list apart which suggests that the 404 page should be dynamic with different pages served in different 404 contexts.

BTW: most WordPress themes serves what should be 404 pages as a 200 (success) pages. My solution fell into this trap too :(

Control Outbound RSS plugin for wordpress

18 Sep

This plugin will handle several aspects of RSS feeds generated by wordpress

  1. Reduce the amount of bandwidth required to transmit the RSS by sending only the items which were had not being already sent to the specific RSS reader.
  2. Better control on the amount of items which are included in the feed. For sites with very frequent posts or comments the default of 10 might not be enough.
  3. If it is possible at all, add a feed per a comment thread.

The first version handles only the bandwidth reduction issue. Just download it, put it into your wordpress plugin directory, if you use it for a stand alone wordpress, or at the mu-plugins directory for wordpressmu.

It was tested in wordpress 2.8, but it should work for wordpress 2.2 and above.

Page 1 of 3123»