Создайте шорткод в Wordpress, снова


Есть много примеров как здесь, так и в Интернете, я пытался, но, похоже, не смог достичь того, что мне нужно, хотя мне кажется, что это должно быть достаточно просто.

В принципе, я хочу иметь возможность использовать такой шорткод:

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

И мне нужен Wordpress для вывода этого:

<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>

Мы будем очень признательны за вашу помощь. Большое спасибо!

 2
Author: pereyra, 2011-07-14

1 answers

// 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
Author: David, 2011-07-14 19:28:44