How to hide Facebook’s Like Box social plugin border May 26, 2010 1 Comment
I was trying to make my own rounded-colored-cooler-looking border for the Like Box social plugin, and Facebook’s default border was getting in the way.
That’s what I eventually used:
<div id="likebox" style="background-image: url(../images/likebg.png); width: 286px; height: 156px; padding: 7px;"> <div id="likebox-frame" style="width: 286px; height: 156px; overflow: hidden;"> <iframe style=" width: 288px; height: 155px; margin: -1px -4px 0 -4px;" frameborder="0" border="0" src="http://www.facebook.com/plugins/likebox.php?id=185550966885&amp;width=300&amp;connections=5&amp;stream=false&amp;header=false&amp;height=255" scrolling="no" frameborder="0" allowTransparency="true"></iframe> </div> </div>
So – what did I do?
- We have the #likebox element that sets the background image (that has rounded borders) and some padding, so content won’t hide the border.
- The #likebox-frame element is just a dummy container to make the negative margins on the actual iframe (see below) work while the padding of #likebox still pushes the content inside. it needs height/width (of the “real” likebox content, the inner (after removing the padding) width/height of #likebox)
- And now – for the actual iframe – we have 4 borders that facebook adds inside the iframe to hide. the top/left borders are hidden with a negative margin (top margin of -1px and left margin of -4px”) and the bottom/right borders are hidden because the iframe is bigger than its container – so it pushes outside and the #likebox-frame overflow:hidden takes care of hiding it.
That’s it. Worked out quite nicely.
8 Websites you need to stop building May 12, 2010 1 Comment
Internet Explorer “Unspecified Error” with jQuery and getBoundingClientRect May 8, 2010 No Comments
I’ve been getting “Unspecified Error” errors from Internet Explorer with some jQuery code. I traced the cause to jQuery’s “getBoundingClientRect” which comes from a call to $(…).offset({top: X, left: T}).
After looking around in some forums/blogs, it seems the error is coming from some handling of internet explorer with offsetParent in some specific cases. to be honest, I don’t really understand what happens there…
But I did manage to fix it by simply calling $(…).css({top: X, left: Y}) instead of $(…).offset({top: X, left:Y}), which does pretty much the same in my case.
Just thought someone out there might find it useful…
Toodledo API implemented in PHP March 29, 2010 5 Comments
I couldn’t find one, so I decided to write a PHP class that talks with Toodledo API. its not very well documented/commented, but quite simple and works well.
Works on PHP5+ only. Uses the built-in stream wrapper for making HTTP requests (I’ll probably add support for requests with CURL and POST method too), and returns the response as an SimpleXML object. It uses file-system caching for storing the tokens the API gives you (there’s a CACHEDIR const to define where it should be saved).
The usage is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $t = new Toodledo('user id', 'password', 'app id'); // last parameter is optional // Add task $response = $t->addTask(array( 'title' => 'Finish writing toodledo class in PHP', ... )); $id = (string)$response->added; // Get tasks $tasks = $t->getTasks(array( 'title' => 'foo', 'tag' => 'bar', )); foreach ($tasks->task as $task) { echo (string)$task->title; } |
You can also get the response as raw XML string and as array, by using Toodledo::RAW and Toodledo::ARR as the second argument.
print_r($t->getTasks(array('title' => 'foo'), Toodledo::ARR));
OK. So I must admit – I cheated. I didn’t really implement the entire API, I just wrote one method named request() and used some __call magic to make everything work. basically, you just call $object->APIMethodName(array(‘extra’=>’parameters’)) with whatever method name and parameters and it just build the request accordingly.
When an error occurs (either while making the request or the Toodledo API returns an error), an Exception is thrown. Make sure to catch those.
That’s about it. You can download it here. released under the SMWTFPL license, which means you can do whatever you want with it (modify, redistribute under every license, sell it and pretty much anything else).
HTTP error 417 with PHP’s curl March 11, 2010 4 Comments
I’ve been getting 417 response codes (Expectation failed) while making requests with PHP/curl to tinyurl.com (I’ll probably publish that project here in a week or two). Well, its very annoying. took me some time to figure that one out.
If someone else wonders about it, that’s the deal:
If the content length (of the request post data) is bigger than 1KB (>=1025 bytes), the client (curl in our case) sends the request with a “Expect: 100-continue” header and without the actual post data, than awaits for a response with status 100, and than finally sends the actual post data.
The problem is that many servers (lighttpd 1.4 among them, fixed at 1.5) doesn’t support that, which is exactly why I got the 417 error when my client attempted to use that header.
To make curl suppress that header, and just send the request/post data normally while ignoring the fact that the post data is more than 1KB, add the following piece of code:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
Adding a header with no value makes it overwrite the original value and suppress the header, which should make everything work fine.
It should be noted that every time you set CURLOPT_HTTPHEADER it overwrites the original HTTPHEADER value and not appends to it, so if you already set it, you should simply add ‘Expect:’ to your existing HTTPHEADER array instead of adding the entire line.
Blog publishing platforms and Google Sets March 7, 2010 No Comments
I was looking for a list of blog publishing platforms and thought Google Sets, a little-known Google labs experiment that takes a list of items as input and outputs a larger set of items of the same kind, might be useful.
I gave it Wordpress, pivot, geeklog and greymatter as input and got a pretty comprehensive list of blog publishing platforms.
Google did a pretty good job with that, quite impressive.
Disk Free Human User March 7, 2010 No Comments
I really don’t know how I haven’t heard of that blog until now. Very interesting, high-quality and inspiring content. this is definitely going to be one of the very few blogs I’m regularly following.
My implementation of PHP Chainable March 2, 2010 No Comments
I needed a way to use method chainability on objects without having to modify the actual class (e.g. extending an Chainable class or implementing one) and without having to “return $this” on every method.
I came up with, what I believe to be, an elegant simple solution – a generic approach that can be used on any class/object, which makes chaining possible on 3rd-party classes and enables you to return values from those methods.
For the lazy ones among us, it also allows to have the benefits of method chaining without having to think about it while coding. It just works.
So, first of all, this is the Chainable class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Chainable { private $obj; private $result; public function __construct($obj){ $this->obj = $obj; } public function __call($method, $args) { if (substr($method, 0, 1) == '_') { array_unshift($args, $this->result); $method = substr($method, 1); } $this->result = call_user_func_array(array($this->obj, $method), $args); return $this; } // __set & __get were added later on and mentiond at the very end of the post public function __set($key, $value){$this->obj->$key = $value; return $this;} public function __get($key){return $this->obj->$key;} public function result(){ return $this->result; } // new Chainable($obj)->foo() isn't possible, Chainable::get($obj)->foo() is public static function get($obj){ $chainable = new Chainable($obj); return $chainable; } } |
Basically, you use instances of Chainable instead of working directly with the actual class. You get a new instance by using new Chainable($object), or the more convient way which allows to call methods of it right away – Chainable::get($object).
After getting a Chainable object that’s connected to your object, you simply use $chainableObj->method1()->method2().
If you want to pass the result of the last method call to the next method, you prefix the method that should receive the result with a “_”, and than the first argument passed to it would be the last return value.
Also, you have a result() method for getting the last result from “outside”. this does create problems when you actually have a result() method on the object you’re passing to Chainable, and I will probably change it to something less likely to be used.
Lets take an example class, Foo:
1 2 3 4 5 6 7 8 9 | class Foo { public function bar($str){ return 'bar::' . $str; } public function taz($passed, $str) { return 'taz::' . $str . ' -- passed::' . $passed; } } |
Than, to use the Chainable interface you:
$foo = new Foo; echo Chainable::get($foo)->bar('shesek')->_taz('test')->result();
Which prints “taz::test — passed::bar::shesek”. note the “_taz” which allows taz() to get the return value of bar() as the first argument, $passed.
Another approach could be adding a “chain” method inside the Foo class (probably much easier to make a base class that has this and extend):
public function chain(){ return Chainable::get($this); }
Which enables you to use $foo->chain()->bar(’shesek’)->_taz(‘test’)->result();. It makes the code prettier, with the price of having to modify the class itself – but a very minor modification.
A drawback of this concept is that it prevents access to private/protected methods, and I will often use this from inside the object (Chainable::get($this)->foo()->bar()) .
To solve that, I’m thinking about making a generic public method that’ll serve as a kind of “proxy” to private methods – but it does kinda make private/protected methods purposeless…
I haven’t used it much yet (got the idea around 20 minutes ago and wrote the Chainable class and testcase while writing this post), but I do think I’ll find this to be quite effective and useful.
This code is released under the SMWTFPL license, so if any of you find this useful feel free to use it.
If you have any remarks or thoughts, I’ll be happy to hear about it!
Update: Added __set & __GET
__set – Now its possible to set variables in the object while chaining (example with PHPMailer):
$mail=new PHPMailer(); Chainable::get($mail)->__set('Subject','Hello there')->AddReplyTo('my@email.info')->SetFrom('my@email.info')->AddAddress('your@email.info')->msgHTML('<h1>Hey!</h1>')->send()->result()
This is equal to $mail->Subject=’…’;. This can be used as a magic setter too, but doesn’t make much sense.
__get allows you to read public variables of the object. it only makes sense if you use that last (that HTTP class doesn’t exists, just for showing the concept):
$http = new HTTP; echo Chainable::get($http)->__set('url', 'http://www.google.com/')->userAgent('Firefox')->request()->html;
The last -> html is equal to $http->html
jQuery shake() function December 29, 2009 No Comments
I needed a quick function for jQuery to shake an absolute or static positioned (can be easily modified to support other positions too, but it doesn’t at the moment) element up&down that isn’t depended on jQuery UI (I’m using to shake simplemodal’s container).
This is what I came up with that:
1 2 3 4 5 6 7 8 9 10 11 | (function($){ $.fn.shake=function(opt){ opt=$.extend({times: 8,delay: 150,pixels: 20},opt||{}); $(this).each(function(){ var orig=parseInt($(this).css('top')); for (var i=0; i<opt.times; i++) $(this).animate({top:orig+(opt.pixels*(i%2==0?1:-1))},opt.delay); $(this).animate({top:orig},opt.delay); }); } })(jQuery); |
Its usage looks like that:
$('#simplemodal-container').shake({times:8, delay:150, pixels:20});
All the parameters are optional, you could simply call $(..).shake() to use the defaults values or overwrite just some of the them.
times determines how many times to shake the element, delay is how long each animate() call should take, and pixels is the number of pixels to move up/down every time
Quick random Wordpress post hack December 26, 2009 No Comments
A friend of mine, Dima, was looking for a plugin that’ll make it possible to link to random post for his new site, shlat.im.
He told me over IRC that he couldn’t find any simple plugin to do it, and I replied with this quick hack:
<?php require('wp-load.php'); header('Location: index.php?p='.$wpdb->get_var('SELECT ID FROM '.$wpdb->posts.' WHERE post_type=\'post\' AND post_status=\'publish\' ORDER BY RAND() LIMIT 1'));
Just stick it in a rand.php file in the wordpress folder, and link to it. it doesn’t get any simpler than that.
