lucaBiagini.com

Life is the childhood of our immortality.

Archive for the ‘Tutorials’ Category

Wordpress plugin: the excerpt re-reloaded

Saturday, November 29th, 2008

UPDATED!

The fantastic plugin The Excerpt Reloaded by Kaf Oseo, my trusted companion in dozens of  Wordpress installation, doesn’t work anymore on WP 2.6, so I have written an alternative version to finish the site I’m working on.

It’s a very small thing, not as complete as The excerpt reloaded, but, at least, it works, and it manages better than its inspirator the allowed tags, adding auto close to avoid pagination and validation errors.

It is based on the plain content of the post (like the content_rss in Kaf’s version) and like its predecessor, allowed tags cannot include <p> paragraphs, simply because I don’t need it on the site I’m currently developing, nor usually on any other. BTW, if somebody is interested, it can be easily fixed (or so I hope).

Parameters

  • words (integer): number of words to display before ending the excerpt. Default is 40.
  • link_text (string): defines the text for the link to the full post. Default is “Keep reading this entry »”. Of course it is displayed only if the selected length is less than the full content. It is displayed in a <p> pararaph with its specific class (more), so it can be easily stylized.
  • allowed_tags (string): defines which HTML tags to accept. Use the format ‘<img>’. For multiple tags, enter as single string: ‘<a><img>’. No tag allowed by default. NEW! Set to ‘all’ to accept all tags.
  • NEW! container (string): defines the container of the excerpt: paragraph, span, div, or none. The container has a specific class=”more” to easily customize its layout through css. If you choose a “div” container, an additional “p” tag inside the div is automatically included. Default is “p”, for no container use “plain”.

Examples

<?php the_excerpt_rereloaded(); ?>
//display the excerpt with default settings.

<?php the_excerpt_rereloaded('80','More','<strong><em>','div'); ?>
//display an excerpt of 80 words
//the link to full post is simply "more"
//allowed tags are strong and em
//the link path is <div class="more"><p><a href="#etc">More</a></p></div>

<?php the_excerpt_rereloaded('80','More','all','plain'); ?>
//display an excerpt of 80 words
//the link to full post is simply "more"
//all tags are accepted
//the link path is included in the excerpt paragraph with no additional container

Download

Click here to view, copy, paste und upload as usual in wp-content/plugins folder. Suggestions are welcome.

Previous version is still available.

Firefox: blank location bar and chronology not working

Sunday, November 23rd, 2008

I had a strange problem yesterday with firefox on my mac: when I launched the browser, url bar stayed empty. It had no (apparently) (good) reasons act like this, as I didn’t update anything. If I tried to type another url, and then to navigate starting from it, the url didn’t change. Next and previous page buttons wasn’t working either.

A little but annoying problem, so i started googling for a quick solution, but I just didn’t found it, though many other users had experienced my same issue. Some of the few answers I’ve read somewhere suggest to launch Firefox in safe mode. I tried, but the problem was still there. Another common and uneffective (at least, for me) solution is to disable plugins and extensions, one by one, to understand which one is causing the problem.

I post the solution I’ve found, hoping it may help somebody in the future to save some time. Access profile manager (instructions here, btw on mac, open terminal and digit: /Applications/firefox.app/Contents/MacOS/firefox -profilemanager), delete current profile and create a new one (it can be named like the previously deleted one - too lazy to think of something different from “default”).

Now everything is fine again, I’ve reinstalled the previously (and probably unnecessarily) deleted extensions and it’s still ok.

Bootcamp: Airport on Windows with Parallels

Sunday, July 20th, 2008

Switched back to MacBook Pro after few unhappy weeks of MacBook Air, I’ve decided to try again Parallels.

Last time I’ve used it, almost 2 years ago, it was a beta and had too many limitations for my needs. Two years later, I must admit that it simply works and suits perfectly my needs, even with “only” 2GB of ram.

I use it with the Windows XP partition generated with Boot Camp and the only problem I had, was with the airport connection. On Windows in Parallels, airport is not recognized as an installed device, so if you want to surf the web from windows with a wifi connection, the only solution is to share the wireless connection between the guest and the host OS.

In most cases, it works automatically, but not in mine. After hours of managing setting trying every combination of shared network, host only network, default adapter and so one, the best results I had gained was the possibility of browsing google.com (it wasn’t cached, it worked!) and few other sites. I could ping correctly everything, but couldn’t open any web page.

Googling and googling, I have found the solution I want to share on this post. I’ve downloaded a small app called Dr. TCP, and in the very last row of the window I have selected “Parallels network adapter” and set MTU to 1400. Now everything works like a charm.

Hope it helps somebody else - I’ve seen many other users looking for help in many forums, with both Windows XP and Windows Vista, and even with Linux. Credit to this velayo’s post on Insanely Mac Forum. I can just add that reboot is probably not needed on newer Windows version.

Php, MySql, .htaccess: friendly urls

Wednesday, March 26th, 2008

There are a lot of tutorials that show how to change an address like:

  • www.mysite.com/products.php?product_id=1234

into others like:

  • www.mysite.com/products/1234
  • www.mysite.com/products/1234.html

In this article I’d like to go one step forward and to create something like:

  • www.mysite.com/easy-to-remember-product-name
  • www.mysite.com/easy-to-remember-product-name.html

How-To and Contraindications

Switching from an address like the first one to the secon ones is search engine necessary, because SE spiders often don’t crawl correctly our dynamic urls. Switching to the third ones is much better, because it’s a search engine friendlier, and it’s human friendly too.

Now, I think we all agree that we should change our dynamic urls into the last ones, but the goal is to make it without modifying, or at least without modifying too heavily our web application. If our php script works with a query for product_id, and product_id is the unique, autoincremental key of our mysql database, we have to rewrite our urls without losing the possibility for our script to work in its original way.

That is the way Wordpress works: a function that allows the user to choose the preferred format for the urls. In this tutorial, we replicate - in a very simplier way - that tool.

How the system works

My solution - with downloadable example - uses php and mysql to manage the content and create the urls, and Apache’s server mod_rewrite module to rewrite the addresses. We assume to be on a linux server and that our app is php and mysql based.

We have 2 tables in our mysql db:

News table contains the articles, with the columns id, title, article and slug, where slug contains the sanityzed title, that is a title like: “That’s an easy mod_rewrite tutorial” becomes “thats-an-easy-mod-rewrite-tutorial”.
The script, normally, queries the db for the id, but we, through php, will find the related slug. Then we rewrite the url through mod_rewrite.

Options table contains id, name and value of the options, that is, in this example, permalinks yes/no. If permalinks are off, php will query the db for the id, and the url format will be: index.php?id=x. If permalinks are on, php will query mysql for the slug, and mod_rewrite will rewrite the url in format: rewritten-url-with-php-and-mod-rewrite.

Example

A working example is available at:
gruppomodulo.it/lavori/esempi/mod-rewrite-php-htaccess-tutorial/

Also downloadable in .zip format at:
gruppomodulo.it/lavori/esempi/mod-rewrite-php-htaccess-tutorial/ex.zip

Example includes following files:

  • .htaccess
    Very simple .htaccess for mod_rewrite, that transforms the url from ?slug=rewritten-url to /rewritten-url.
    If permalinks are off, url format will be ?id=x, and there will be no rewrite.
  • options.php
    The file with the basic tool to switch on/off the permalinks
  • single.php and index.php
    Show site contents.
  • functions.php
    Contains the function that generates the urls (rewritten or not, depending on permalinks selected status) and the function that gets the articles from mysql database.
  • Zip file also includes the .sql file to recreate the example datatabase.

Wordpress plugin: random subpages

Friday, February 22nd, 2008

This is the very first plugin I’ve written for wordpress. It’s so simple and could be easily optimized, but it works and it’s still having success, with a large number of viewers where I have originally posted it on Gruppo Modulo (where is also available in italian version).

Wordpress Plugin Random SubPages lets you show a customizable number of random child pages (or just some elements of them) of a parent page.

It can be used to display random services of a parent service page, i.e.:

+ web solutions
- xhtml + css
- php scripting
- webcommerce tools
- seo and sem
- web adv
- dem marketing
- ecc

Or it can be used to show the members of a team:

+ XYZ Football Team
- player 1
- player 2
- player 3
- players 4
- etc

Download Random Subpages and unzip the folder on your pc.
Then upload random_subpages.php in wp-content/plugin and activate it from admin panel.

Then recall it, outside the loop, in the page you wish with this code:

<?php

/* edit first row to set the number of
pages to show and the parent page */

// as set right now,it will show 3 pages, child of page 4

$randompages = gruppo_modulo_rsp('numberposts=3&parentpage=4′);
foreach($randompages as $post) : // do not edit this line
setup_postdata($post); // do not edit this line

/* now it will be shown what you want to show from the selected pages
(for example title, content, meta, ecc), see example below.
you can add additional formatting using echo.
n the example we have placed the title between h3 tags */

echo "<h3>";
the_title();
echo "</h3>";
the_excerpt();
the_content();
the_meta();

// end of the customizable part; do not edit below

endforeach;

?>