100819 items (213 unread) in 22 feeds
CNN
(5 unread)
MSNBC
(8 unread)
PHP
(62 unread)
Deals
(101 unread)
Web Development
(35 unread)
CNN Money
(1 unread)
Frugal Blogs
(1 unread)
First off — I don't have too much experience with disqus on Wordpress but when I looked at the code(-quality), disqus is one of the best plugins for Wordpress to date. I looked only very briefly, but I found it to be very clean, well documented and well architected. Good job, disqus!
In case you happen to dive into Wordpress plugins in 2012, you will see that this is unfortunately not a given. Even (or maybe especially) the commercially available plugins to extend Wordpress are a source for PHP worst practices and contain all kinds of examples how not to write PHP. Part of it, is the platform, the rest is unexperienced developers or general lazyness.
Anyhow! Enough the rant!
Disqus and the loopIn Wordpress, almost everything is a loop. If you'd like to display a list of posts somewhere (for whatever reason), a loop is executed. The advantage (vs. a straight SQL query) range from all kinds all things like caching to convenience. When said hook is used different actions (plugin hooks) and filters are run. So even when the objective is to just collect data (and not even display it) — for example for an Ajax request/response — all these things happen in the back.
So in this case, whenever a loop is used, disqus will attempt to add their JavaScript to it. Usually, to display a comment form, counts and what not.
Problem?The problem is, that the JavaScript code is then added all over and especially with Ajax you end up with the same code multiple times but add to that the cost of transport.
Solution!Here's a simple workaround to prevent that.
Stick the following code into your template when you do not want the code to appear:
<?php
if (has_action('loop_end', 'dsq_loop_end')) {
remove_action('loop_end', 'dsq_loop_end');
}
Yay! Happyness!
FinFilters and hooks are really what make Wordpress worth while. Period. In case you happen to do development for it, I urge you to take a look at them. I have a couple more ideas for small posts — stay tuned.
Before we start with today's topic, let's refactor our current framework just a little to make templates even more readable:
<?php // example.com/web/front.php require_once __DIR__.'/../src/autoload.php'; use Symfony\Component [HttpFoundation\Request;] use Symfony\Component [HttpFoundation\Response;] $request = Request::createFromGlobals(); $map = array( '/hello' => 'hello', '/bye' => 'bye', ); $path = $request->getPathInfo(); if (isset($map[$path])) { ob_start(); extract($request->query->all(), EXTR_SKIP); include sprintf(__DIR__.'/../src/pages/%s.php', $map[$path]); $response = new Response(ob_get_clean()); } else { $response = new Response('Not Found', 404); } $response->send();
As we now extract the request query parameters, simplify the hello.php
template as follows:
<!-- example.com/src/pages/hello.php --> Hello <?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>
Now, we are in good shape to add new features.
One very important aspect of any website is the form of its URLs. Thanks to the URL map, we have decoupled the URL from the code that generates the associated response, but it is not yet flexible enough. For instance, we might want to support dynamic paths to allow embedding data directly into the URL instead of relying on a query string:
# Before /hello?name=Fabien # After /hello/Fabien
To support this feature, we are going to use the Symfony2 Routing component.
As always, add it to composer.json and run the php composer.phar
update command to install it:
{
"require": {
"symfony/class-loader": "2.1.*",
"symfony/http-foundation": "2.1.*",
"symfony/routing": "2.1.*"
}
}
From now on, we are going to use the generated Composer autoloader inste
Truncated by Planet PHP, read more at the original (another 14824 bytes)