WP Plugins: How to remove a Filter

Eoin:

Do not use remove_filter(). It is possible it will break other hooks which can have unintended consequences. This post gives a really good alternative approach using static variables.

Originally posted on hakre on wordpress:

Worth to know for wordpress plugin authors: Making your plugin to safely unregister or remove a hook (filter or action) is not possible with the wordpress plugin API. Why? you might ask yourself. Even the name of the remove_filter() function suggests it and the codex documentation does say so as well.

But: It does not work, because this function does not work as intended. And beyond your own hooks, it has the potential to hinder the blog from working properly as it has the potential to remove other hooks then the specified one.

Does not is not exactly right, it does, but only sometimes. That’s specifically a bummer if you write plugins yourself, because you can not safely rely on it. For example, if running your plugin makes use of PHP 5.2 or greater it works. But it does not if it’s running on PHP 4 or PHP 5 lower…

View original 1,582 more words

HTML5 Data attributes in HTML and jQuery

When writing javascript, it is often necessary to include metadata in the HTML markup, to help define some element or behaviour. There are common options available. You can use hidden inputs and/or standard attributes like class or title to store this metadata. However with HTML5’s data attribute, storing and parsing this data has become a whole lot easier and cleaner.

The syntax is straightforward. Any attribute prefixed with data- will be treated as data storage.

<div class="test" data-foo="bar"></div>

jQuery accesses this data like so…

var data = $( 'div.test' ).data( 'foo' ); // returns bar

Support for the data attribute has been added since jQuery version 1.4.3. jQuery’s implementation is smart enough that it can parse the attribute easily and even determine the correct data type used.

What I have found really useful is the fact that the jQuery can parse JSON syntax and return a JSON object. This makes passing data in PHP trivial, using the json_encode method. We also need to use htmlspecialchars method to escape or convert any quotes in the JSON string.

<?php  
$test = array( 'row' => 1, 'col' => 6, 'color' => 'pink' ); //create array of data you want to pass to jquery
$test = json_encode( $test ); //convert array to a JSON string
$test = htmlspecialchars( $test, ENT_QUOTES ); //convert any quotes into HTML entities so JSON string behaves as a proper HTML attribute.
?>
<div class="test" data-complex="<?php echo $test ; ?>"></div>

The jQuery parses the JSON string like so…

var test = $( 'div.test' ).data( 'complex' ); // returns JSON Object

console.log( test.color ); // outputs pink!

Important to note that this method is also backward compatible with older browsers, so there is no excuse not to give it a go!