You’re doing it wrong #1

Welcome to a new series of mine, You’re doing it wrong. Now, You may’ve guessed what this series is going to be about.. WordPress “Programmers” doing things wrong.

Right now, I’m writing a new theme for a website, utilising a few plugins,  one of them is WP125.

 Notice: Trying to get property of non-object in G:\www\nrtt\wp\wp-includes\general-template.php on line 366

Yep, You’re doing it wrong. First glance it looks like its caused by WordPress, but after a lot of debugging, Here’s a stacktrace:

#0 G:\www\nrtt\wp\wp-includes\general-template.php(367) stackTrace(Array ( ) )
#1 G:\www\nrtt\wp\wp-includes\script-loader.php(410) get_bloginfo(Array ( [0] => text_direction ) )
#2 unknown(unknown) wp_default_styles(Array ( [0] => CONVERTED OBJECT OF CLASS WP_Styles ) )
#3 G:\www\nrtt\wp\wp-includes\plugin.php(414) call_user_func_array(Array ( [0] => wp_default_styles [1] => Array ( [0] => CONVERTED OBJECT OF CLASS WP_Styles ) ) )
#4 G:\www\nrtt\wp\wp-includes\class.wp-styles.php(31) do_action_ref_array(Array ( [0] => wp_default_styles [1] => Array ( [0] => CONVERTED OBJECT OF CLASS WP_Styles ) ) )
#5 G:\www\nrtt\wp\wp-includes\functions.wp-styles.php(72) CONVERTED OBJECT OF CLASS WP_Styles->__construct(Array ( ) )
#6 G:\www\nrtt\wp\wp-content\plugins\wp125\adminmenus.php(8) wp_enqueue_style(Array ( [0] => thickbox ) )
#7 G:\www\nrtt\wp\wp-content\plugins\wp125\wp125.php(75) require_once(Array ( [0] => G:\www\nrtt\wp\wp-content\plugins\wp125\adminmenus.php ) )
#8 G:\www\nrtt\wp\wp-settings.php(566) include_once(Array ( [0] => G:\www\nrtt\wp\wp-content\plugins\wp125\wp125.php ) )
#9 G:\www\nrtt\wp\wp-config.php(109) require_once(Array ( [0] => G:\www\nrtt\wp\wp-settings.php ) )
#10 G:\www\nrtt\wp\wp-load.php(30) require_once(Array ( [0] => G:\www\nrtt\wp\wp-config.php ) )
#11 G:\www\nrtt\wp\wp-admin\admin.php(20) require_once(Array ( [0] => G:\www\nrtt\wp\wp-load.php ) )
#12 G:\www\nrtt\wp\wp-admin\edit.php(10) require_once(Array ( [0] => G:\www\nrtt\wp\wp-admin\admin.php ) )

Do you notice whats happening here?  A seasoned WordPress developer should.. But many will not, As it’s a huge problem amongst some plugins..

The issue at hand here, Is that the WP125 plugin, is running code as soon as its included, In this case, its registering styles and scripts as soon as the plugin is included. NOT a good thing to do.

A plugin should NEVER run any code as soon as its included*, All code should be placed inside actions.

Eg, Instead of this:

if(is_admin()){
 wp_enqueue_script('jquery');
 wp_enqueue_script('thickbox');
 wp_enqueue_style('thickbox'); 
}

It should be:

add_action('admin_head', 'wp125_enqueue_styles');
function wp125_enqueue_styles() {
 wp_enqueue_script('jquery');
 wp_enqueue_script('thickbox');
 wp_enqueue_style('thickbox'); 
}

*1: Obviously, there are some cases where running code is considered ok, but it should be avoided. I’m ignoring calls to add_action/add_filter as running code here, as thats the way things should happen.

There  is one more thing which should be noted as well.. another thing a plugin should NEVER do, Is enqueue Scripts and Styles for EVERY administration page, Only load them on the pages which require the script. So lets take that last semi-better-than-original  sniplet and convert it to something which is how ALL plugins should be registering their scripts and styles:

So, Now I’m going to introduce you to one more function which many developers seem to miss, the action admin_print_scripts-<slug>

This action is actually pretty simple, On every pageload of WordPress Administration, many dynamic hooks are fired, in particular interest today, is the admin_print_scripts-<slug> hook. This fires when WordPress decides its time to start printing the <script> and <link rel=”stylesheet” /> items into the head of the document. This is where plugins should be hooking their addition of styles and scripts, As well as printing any extra <head> content to fit into the WordPress admin pages for their plugin.

Without going into it in depth, I’m just going to give a chunk of code, The only thing worth noting, is if you’re not sure of the slug of your page, Quite handy is, that the add_menu_*() functions will return it.. So, Finally, Here we go:

//Add the Admin Menus
add_action('admin_menu', 'wp125_add_admin_menu');
function wp125_add_admin_menu() {
 load_plugin_textdomain('wp125', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)).'/translations', dirname(plugin_basename(__FILE__)).'/translations');
 $pages = array();
 $pages[] = add_menu_page(__("125x125 Ads", 'wp125'), __("Ads", 'wp125'), MANAGEMENT_PERMISSION, __FILE__, "wp125_write_managemenu");
 $pages[] = add_submenu_page(__FILE__, __("Manage 125x125 Ads", 'wp125'), __("Manage", 'wp125'), MANAGEMENT_PERMISSION, __FILE__, "wp125_write_managemenu");
 $pages[] = add_submenu_page(__FILE__, __("Add/Edit 125x125 Ads", 'wp125'), __("Add/Edit", 'wp125'), MANAGEMENT_PERMISSION, 'wp125_addedit', "wp125_write_addeditmenu");
 $pages[] = add_submenu_page(__FILE__, __("125x125 Ad Settings", 'wp125'), __("Settings", 'wp125'), MANAGEMENT_PERMISSION, 'wp125_settings', "wp125_write_settingsmenu");

 foreach ( $pages as $my_page )
   add_action('admin_print_scripts-' . $my_page, 'wp125_register_scripts_styles', 9); //Add it a little earlier.

 //Include menus
 require_once(dirname(__FILE__).'/adminmenus.php');
}

function wp125_register_scripts_styles() {
 wp_enqueue_script('jquery');
 wp_enqueue_script('thickbox');
 wp_enqueue_style('thickbox');
}

The main benefit of behaving yourself? Much less chance of screwing up another admin page’s styling or JavaScript :)

Stay tuned for the next edition of this, Where once again, WP125 will feature, along side TDO Mini Forms

See Attachment for a .diff of the changes made to the plugin. NOTE: This is as of version 1.3.6

Tag Surfing filesystem

Oh how i wish.. folders and files suit a lot of people, and a lot of cases, But sometimes, When you’ve got a lot of data, it’d be nice to be able to access files via a progressive search..

For example, Right now, To access a Hilltop hoods album i might use:

K:\Media\Music\Hilltop Hoods\The Hard Road\

Or maybe

K:\Media\Music-unsorted\Hilltop_Hoods-The_Hard_Road\

Thats simple enough, and both have their places, Organising music  into  structures like that is good, and makes finding things much easier.

But what do you do, when your browsing remotely, through a media center, that doesnt have a keyboard, and instead requires you to scroll down the list item-by-item, I might have thousands of artists… Finding H is going to take awhile..

One solution to this is to break it up more:

K:\Media\Music\H\Hilltop Hoods\The Hard Road\

Now i can jump straight to the H’s without too much trouble.

But what if this could be done automatically?

Tags:\filetype\m\mp3\release-date\2007\starts-with\h\HillTop Hoods\directory\The Hard Road\

Now that might seem a rather long file path, But in reality, it’d be much faster to locate a specific file or  folder, by using the meta-data of the files, it’d be possible to drill down and return a smaller list of files to scroll through. For example, Another path could be:

Tags:\created\last-week\Music\

That’d only return items created last week, which also exist within the “Music” Tag (In this case, The Music tag would automatically be added to the file as it exists in K:\Media\Music\ and/or since it realises that a mp3 file is a Music file).

Another one could be:

Tags:\Media\Hilltop Hoods\

Tags:\Pictures\created\2008\

Tags:\Tag\Holiday\created\or\photo-date\2007\filetype\jpg\

Tags:\created\before\year\2008\month\feb\Pictures\created\after\2005\Holiday\

There are certain projects for the Linux(And mac) desktop which allows this sort of functionality, but they’re more of a drill down based on tags that the user gives a file.. Whilst it works fine for sites like Flickr, it doesnt exactly apply well to local files which contain lots of meta data already.. Or for that matter, It doesn’t apply to me either (As running a Linux/mac desktop isn’t something I’m going to do again anytime soon, A Linux desktop just isn’t for me)

I can dream I guess.. I mean, I’d write it myself, Except I haven’t done Desktop Programming in a fair while, And dont think i’d go too well writing a file system driver. Last time i looked into this (I was still on XP, so say 18~24 months ago) It was near impossible to get your hands on the required software from Microsoft, Now, It looks like they give it away(As they well should).  Combined with a database with a decent structure, shouldn’t be -too- bad for speed lookups..

One question i can think of it, What about writing files to the disk? Well.. The only idea i can come up with there, is that it’d have to only allow modifying files, not creating them..

 

Edit: Ok, So Vista’s search folders originally looked like a good solution, But it never did fit for me.. Looks like windows 7 has improved a bit, but still not what i want.. For now, To ease access, I’ve created a “Shortcuts” folder, Which is basically a folder full of Symlinks (Yes, Vista+ supports them) linking to the latest media items i’ve got, or anything i want quick access to.

Dear all commenter’s

Lately, I’ve been recieving a lot of comments from certain people annoyed at the fact that WordPress has included more features and functionalities which they find extra bloat. Usually, They’re poorly written and I Delete them (Yes, I have no need for their drivel on this blog).

Today however, Its time to make a stand, I am sick of people complaining that WordPress is not what they want.

Many people seem to be unaware of what is actually happening in the world of Warcraft WordPress, oblivious even. Now thats fair enough to me, Not everyone has the time to watch the project evolve day by day, But i would expect someone to read about whats new in WordPress before upgrading, And then upgrade anyway for security reasons.

Please people, read whats new, Suggest new features, WordPress is the free application YOU have chosen to use, Help shape its future into the product that YOU want, Many people seem to sit back and just complain about the new items and call it bloat; To those of you, Fuck off, Go find a different project which suits YOU, Drupal or Joomla! might be it, Or maybe some mostly unknown app which uses 200MB of ram, or 200Bytes of ram, It doesnt matter to me, Find something which suits what you want.

WordPress suits many people, The constant addition of new functionalities, constant patching of potential bugs, and of security issues. All applications MUST do this, else they die off in the background, or a new project which offers them appear, I class wordpress as an innovator,  for 2 reasons, The plugin and Theme options available continuously change and extend wordpress into new directions. New technologies and functionalities are included into WordPress Core every release, as they should be, a LOT of hard work is put in by a handful — a dozen or so — people who love WordPress, and yet, WordPress is used by a few million people i would imagine. Support the developers who develop WordPress and make it what it is.

I realise that last paragraph makes reference to a dozen people, I’m talking about the WordPress Core there,  there are many hundreds of developers who use WordPress and develop Plugins and Themes, They deserve recognition as well.

So please, If WordPress isn’t your cup of tea, Dont come to my blog to complain about it, Get off your arse and help shape the future. There are so many ways in which to do that:

  • Start to fix bugs in WordPress core - Browse Trac for that
  • Start to implement new functionalities for WordPress – Trac again, Search for a ticket, if nothing, Create a new one
  • Make plugins or Themes to do what you want – And then Release them on WordPress Extend
  • Help others in the Support Forums – One of the simplest ways to help
  • Help others in other ways – A talk at your local WordCamp Event about how and why you use it, Perhaps even at your local computer event, It doesn’t matter, It all helps others get to know what WordPress is.
  • Suggest Ideas on the Support Forums, Or Trac

If you’ve made it this far, You’re probably going better than many others, Thanks for reading this, and Please become involved in WordPress one way or another.