file-get-contents : Un plugin WordPress pour inclure un fichier dans vos posts
Pour réaliser mon précédent post, j’ai eu besoin d’inclure dynamiquement le contenu de plusieurs fichiers dans le corps de l’article et je ne souhaitais en aucun cas faire du copier-coller, car l’éditeur wysiwyg n’est absolument pas adapté pour retoucher du code source. J’ai donc créé un petit plugin pour WordPress, qui me permet d’être parfaitement assuré que ce que vous voyez écrit est effectivement le code de la démonstration et qu’il fonctionne donc à coup sûr.
Son utilisation est des plus évidentes : une fois le plugin installé, placez une balise [ file-get-contents (...) ] où vous le souhaitez dans votre article (sans les espaces au niveau des crochets), et ajoutez ensuite les paramètres standards de la fonction file_get_contents de php séparés par une virgule. Il est inutile d’ajouter des guillemets, séparez simplement les arguments par des virgules. Le plugin va chercher ces balises à l’aide d’une expression régulière, exécuter la fonction file_get_contents et remplacer le contenu de la base par le contenu du fichier trouvé !
J’ai documenté le code en Anglais car je compte le faire recenser sur le site officiel de WordPress … Évidemment le code est affiché grâce à ce plugin
.
<?php
/**
* @package File_Get_Contents
* @author Pierre Quillery
* @version 1
*/
/*
Plugin Name: File Get Contents
Plugin URI: http://www.chosesafaire.fr/2009/09/file-get-contents-un-plugin-wordpress-pour-inclure-un-fichier-dans-vos-posts
Description: This plugin allows you to use the php function file_get_contents
in your posts or articles.
Activate the plugin, then use the following syntax in your post :
[file-get-contents (...)]
Replace the (...) with the standard parameters of the file_get_contents
function.
Example :
[file-get-contents ../myfile.txt]
[file-get-contents http://www.google.fr]
Author: Pierre Quillery
Version: 1
Author URI: http://www.chosesafaire.fr/
*/
/*
Copyright 2009 Pierre Quillery (email : pquillery@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
This filter looks for everything that looks like « [file-get-contents ...] »,
captures the arguments and pass them to the callback function.
It replaces then what it found with what the callback function returned.
*/
function filter_file_get_contents($content) {
return preg_replace_callback(
'`\[file-get-contents (.+?)]`i',
'cb_file_get_contents',
$content
);
}
/**
This callback function calls the file_get_contents function with the
parameters from the string returned by the regular expression.
*/
function cb_file_get_contents($parameters) {
$result = call_user_func_array(
'file_get_contents', explode(',', $parameters[1])
);
if ($result === FALSE)
$result = "An error has occured while trying to include the file.";
// Finally we encode the special entities before returning the result
return htmlspecialchars($result);
}
// Hooks the filter to the content that is displayed
add_filter('the_content','filter_file_get_contents');
I don’t usually reply to posts but I will in this case, great info…I will add a backlink and bookmark your site. Keep up the good work!