Jak vytvořit a přidat vlastní shortcodes

Zakódování e-mailové adresy

function cwc_mail_shortcode( $atts , $content=null ) {
    for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
    return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';
add_shortcode('mailto', 'cwc_mail_shortcode');

Použití shortcode:


Obsah pouze pro přihlášené uživatele

function cwc_member_check_shortcode( $atts, $content = null ) {
         if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
                return $content;
        return '';

add_shortcode( 'member', 'cwc_member_check_shortcode' );

Použití shortcode:

[member]This text will be only displayed to registered users.[/member]

Zobrazení PDF v iframe

function cwc_viewpdf($attr, $url) {
    return '<iframe src="https://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$attr['width']. '; height:' .$attr['height']. ';" frameborder="0">Your browser should support iFrame to view this PDF document</iframe>';
add_shortcode('embedpdf', 'cwc_viewpdf');

Použití shortcode

[embedpdf width="600px" height="500px"]http://infolab.stanford.edu/pub/papers/google.pdf[/embedpdf]

Volání shortcode v šabloně (mimo editor)

echo do_shortcode('[jmeno_shortcode]');

viz dokumentace.

Znepřístupnění shortcodes

There might be some cases that you need the shortcodes of the text to be omitted. When that time comes, strip_shortcodes() is your friend.

Let's say that you need to strip shortcodes in the homepage but let them run in other content pages:

function remove_shortcode_from_index( $content ) {
    if ( is_home() )
        $content = strip_shortcodes( $content );
    return $content;
add_filter( 'the_content', 'remove_shortcode_from_index' );

Jak vytvořit vlastní shortcode (musilda.cz) »
10 super useful WordPress shortcodes »
Mastering Wordpress shortcodes »
8 fun & useful shortcode functions for WordPress »