bakedproject.com Blog Not just another WordPress weblog

29Oct/100

Baked json_encode… write your own for PHP 5.1.x

PHP's json_encode is a very handy function for encoding any associative array to JSON. This function comes bundled with PHP 5.2.x. I been working with a client who has PHP 5.1.6. I would install all the necessary tools, but client was not very willing to make changes and I been wanting to write my own JSON encoder for the longest time!

So here is my baked approach...

1
2
3
4
5
6
7
8
9
function my_json_encode($json_array) {
 $json = "[";
 for($i=0; $i1) $json .= ',';
   $json .= '"'.addslashes($key).'":"'.addslashes($row[$key]).'"';
  $i++;
 }
 $json .= "}";
 return $json;
}// end of my_json_encode

and the call

1
2
3
4
5
while($row = mysql_fetch_assoc($result)) {
 $json_row[] = $row;
} 
 
echo my_json_encode($json_row);

4Oct/101

Using jQuery UI with WordPress

I started developing with WordPress 3.0+ these days and figuring out a few things!.

WP loads a few jQuery libraries by default, so being a plugin developer you do not explicitly need to load those libraries, or that is what they have suggested. The original list is here http://codex.wordpress.org/Function_Reference/wp_enqueue_script.

I could not get the jQuery UI working for the widgets! The widget control does not load any UI component, the tabs are already "sortable" and "draggable". This probably is the reason. jQuery UI was not working on the actual widget display either. I tried a few things before remembering, a while ago I had to call the the jQuery UI init functions all the way to the bottom for a challenging project. We were using jQuery Light Box and some other components. The jQuery UI initi simply wont work if the call was made before

So this is my call

1
wp_enqueue_script('myscript', '/wp-content/plugins/bp-category-display/js/myscript.js', array('jquery', 'jquery-ui-core','jquery-ui-sortable'), '', true);

the important thing is, enqueuing the script to the end of the document, this the last 'true'.

Filed under: Uncategorized 1 Comment