web-dev-qa-db-fra.com

Créez à nouveau un shortcode dans Wordpress

Il y a beaucoup d'exemples ici et sur Internet, j'ai essayé, mais je ne semble pas être capable de réaliser ce dont j'ai besoin, même si cela me semble devoir être simple.

En gros, je veux pouvoir utiliser un shortcode comme ceci:

[download]http://site.com/file.doc[/download]

et j'ai besoin de Wordpress pour afficher ceci:

<a class="download" href="http://site.com/file.doc">download file</a>
<a class="download" href="http://docs.google.com/viewer?embedded=true&url=http://site.com/file.doc">preview file</a>

Votre aide serait grandement appréciée. Merci beaucoup!

2
pereyra
// Declare your shortcode
add_shortcode("download", "downloadFunction");

// Second Declare your shortcode function
function downloadFunction($att, $content, $code){

    // your function is passed 3 Arguments
    // $atts : this is an array of the shortcode's attributes
    // $content : this is the content between the code. in your case the file url.
    // $code : this is the code used. in this case it will be download.
    // here we only need the $content of the shortcode which we place for the file

$return = '<a class="download" href="'.$content.'">download file</a>';
$return .= '<a class="download" href="http://docs.google.com/viewer?embedded=true&url='.$content.'">preview file</a>';

// we then return the result.
return $return;
}
2
David