In diesem Artikel geht es um sogenannten Shortcodes, das ist eine Möglichkeit von WordPress innerhalb eines Artikels Funktionen ausführen zu können. Einige Shortcodes hilfen Ihnen ohne Plugins auskommen und das verbessert die Performance von WordPress. Ich zeige Ihnen wie Sie Shortcodes benutzen und bringe Beispiele von einigen Shortcodes.
Was ist Shortcode?
Shortcode wird ab WordPress 2.5 unterstützt, das ist ein Wort innerhalb Artikels, es bestimmte Funktion von WordPress aufrufen kann. Zum Beispiel vordefinierte Shortcode ist, wenn dieses Wort im Text steht, dann wird die Funktion ausgeführt, die Bild Gallery öffnet.
Wie benutzt man Shortcodes?
Sie können Ihren eigenen Shortcode erstellen und die benutzen. Zusätzliche Shortcodes, die Sie selbst erstellt haben, sollten in functions.php eingefügt werden.
Beispiel für einfachen Shortcode:
Am Ende functions.php schreiben diese Code:
|
1 2 |
function show_current_year(){return date('Y');} add_shortcode('show_current_year', 'show_current_year'); |
Wenn wir im Text von unserem Artikel fügen [show_current_year] bekommen wir die Ausgabe von aktuellem Jahr.
Gehen wir auf unsere ersten Beispiel über Bild Gallery zurück. Mit Shortcode kann man Attributen verwenden. Zum Beispiel wir möchten bestimmte Bild von Gallery nicht zeigen, so kann man Shortcode verwenden:
Jetzt werden alle Bilder aus Gallery angezeigt, außer Bild, es ID=43 besitzt.
Die Shortcodes kann man nicht nur im Arktikel benutzen. Noch eine Möglichkeit ist Shortcode bei Text Widgets benutzen, dafür schreibt man diese Code in functions.php:
Mit Grundkenntnissen von PHP können Sie leicht Ihren eigenen Shortcode entwickeln und realisieren. Ich denke, es ist jetzt klar, wie Shortcode funktioniert… Also, noch mal:
- Die Funktion in functions.php hinzufügen
Wir machen Funktion, die „Hello World“ ausgibt:
123function hello() {return 'Hello World';} - Jetzt haben wir Funktion, die in Shortcode umgewandelt wird. Dafür benötigt man eine andere Funktion add_shortcode(), die wir auch in functions.php einfügen:
1add_shortcode(´hw´, ´hello´);
Der erste Parameter ist der Name von Shortcode und zweite ist Name der Funktion.
- Shortcode ist fertig, um den zu benutzen, wechseln wir in html Editor WordPress und geben folgendes:
1[hw]
Hier sind die weiteren WordPress Shortcodes die man gebrauchen kann:
Statistik im Sidebar anzeigen lassen
Manche Blogger möchten Anzahl der Artikel oder Kommentare von eigenen Blog im Sidebar darstellen, diese Code hilft es zu realisieren:
[wcs_count type="posts"]. Weitere Befehle kann man hier ansehen.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
add_shortcode('wcs_count', 'wcs_count_shortcode_handler'); function wcs_count_shortcode_handler($atts) { // extract parameters $parms = shortcode_atts(array( 'type' => 'posts', 'format' => 'true', 'extra' => '1', ), $atts); $type = strtolower($parms['type']); $format = strtolower($parms['format']); $extra = $parms['extra']; // process t/f options $b_format = false; if (($format == 'yes') || ($format == 'y') || ($format == 'true') || ($format == '1')) {$b_format = true;} // exit return wcs_get_count($type, $b_format, $extra); } function wcs_get_count($type='posts', $format='1', $extra='1') { // TYPES: // posts, posts_by_author, pages, tags, categories // users, ms_users, blogroll, blogroll_categories, commenters // comments, comments_pending, comments_spam, comments_pingback // comments_by_user, comments_by_nicename, comments_by_email // comments_per_post // $extra is used with: // posts_by_author, comments_by_user, comments_by_nicename, comments_by_email // comments_per_post // init global $wpdb; $type = strtolower($type); $count = 0; // process switch($type) { case 'posts': // published $count = wp_count_posts('post'); $count = $count->publish; // options: publish, future, draft, pending, private, trash, auto-draft, & inherit break; case 'posts_by_author': // use $extra for user/author id case 'posts_by_user': $query = "SELECT COUNT(*) FROM $wpdb->posts "; $where = "WHERE post_type='post' AND post_status='publish' AND post_author='$extra'"; $count = $wpdb->get_var($query . $where); // alternative method is: count_user_posts() break; case 'pages': // published $count = wp_count_posts('page'); $count = $count->publish; break; case 'tags': $count = wp_count_terms('post_tag'); break; case 'categories': $count = wp_count_terms('category'); break; case 'users': $count = count_users(); $count = $count['total_users']; break; case 'ms_users': // multi-site $count = get_user_count(); break; case 'blogroll': $query = "SELECT COUNT(*) FROM $wpdb->links "; $where = "WHERE link_visible='Y'"; $count = $wpdb->get_var($query . $where); break; case 'blogroll_categories': $count = wp_count_terms('link_category'); break; case 'commenters': $query = "SELECT COUNT(DISTINCT comment_author) FROM $wpdb->comments "; $where = "WHERE comment_approved='1' AND comment_type=''"; $count = $wpdb->get_var($query . $where); break; case 'comments': $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='1' AND comment_type=''"; $count = $wpdb->get_var($query . $where); break; case 'comments_pending': $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='0' AND comment_type=''"; $count = $wpdb->get_var($query . $where); break; case 'comments_spam': $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='spam' AND comment_type=''"; $count = $wpdb->get_var($query . $where); break; case 'comments_pingback': case 'comments_pingbacks': case 'comments_trackback': case 'comments_trackbacks': $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='1' AND comment_type='pingback'"; $count = $wpdb->get_var($query . $where); break; case 'comments_by_user': // use $extra for user_id $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='1' AND comment_type='' AND user_id='$extra'"; $count = $wpdb->get_var($query . $where); break; case 'comments_by_author': // use $extra for author nicename (case INsensitive) case 'comments_by_nicename': $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='1' AND comment_type='' AND comment_author='$extra'"; $count = $wpdb->get_var($query . $where); break; case 'comments_by_email': // use $extra for author email (case INsensitive) $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='1' AND comment_type='' AND comment_author_email='$extra'"; $count = $wpdb->get_var($query . $where); break; case 'comments_per_post': // $extra is decimal place precision (0 for integer only) // posts $posts_count = wp_count_posts('post'); $posts_count = $posts_count->publish; // comments $query = "SELECT COUNT(*) FROM $wpdb->comments "; $where = "WHERE comment_approved='1' AND comment_type=''"; $comment_count = $wpdb->get_var($query . $where); // average return round($comment_count / $posts_count, $extra); default: $count = 0; } // exit if ($format) {$count = number_format_i18n($count);} return $count; } |
E-mail Verschlüsseln
So kann man E-mail mit html Symbolen verschlüsseln, mit dem Code: [mailto]mein_email@example.com[/mailto]
|
1 2 3 |
function munge_mail_shortcode( $atts , $content=null ) { for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';'; r |
Autoformatierung WordPress im Text von Artikel deaktivieren
Mit diesem Shortcode deaktivieren Sie Autoformatierung WordPress von bestimmten Text: [raw]Hier Text ohne WordPress Formatierung[/raw]
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function my_formatter($content) { $new_content = ''; $pattern_full = '{(\[raw\].*?\[/raw\])}is'; $pattern_contents = '{\[raw\](.*?)\[/raw\]}is'; $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($pieces as $piece) { if (preg_match($pattern_contents, $piece, $matches)) { $new_content .= $matches[1]; } else { $new_content .= wptexturize(wpautop($piece)); } } return $new_content; } remove_filter('the_content', 'wpautop'); remove_filter('the_content', 'wptexturize'); add_filter('the_content', 'my_formatter', 99); |
Text nur für angemeldete Benutzer zeigen
Der Text wird nur für angemeldete Benutzer angezeigt und für nicht angemeldete wird ausgeblendet.
Die Verwendungscode: [member]Den Text sehen nur angemeldete User [/member]
|
1 2 3 4 5 6 |
function member_check_shortcode( $atts, $content = null ) { if ( is_user_logged_in() && !is_null( $content ) && !is_feed() ) return $content; return ''; } add_shortcode( 'member', 'member_check_shortcode' ); |
Nur Text im Rss anzeigen
Es wird nur ein Teil von Text in RSS dargestellt, was anlockend auf Leser wirkt. Mit dieser Shortcode können Sie auf FeesOnly Plugin verzichten – der macht das gleiche!
Die Code dafür: [feedonly] Text für Abonnierer [/feedonly]
|
1 2 3 4 5 |
function feedonly_shortcode( $atts, $content = null) { if (!is_feed()) return ""; return $content; } add_shortcode('feedonly', 'feedonly_shortcode'); |
Taste für Aufruf der Shortcodes in WordPress Editor
Wenn Sie mehrere Shortcodes haben und vergessen manchmal einige, dann kann man Dropdown-Liste für Shortcodes bei Formatierungsleiste erstellen. So spart man Tipparbeit und natürlich Ihre Ziet.
|
1 2 3 4 5 6 7 8 9 10 |
add_action('media_buttons','add_sc_select',11); function add_sc_select(){ global $shortcode_tags; /* ------------------------------------- */ /* enter names of shortcode to exclude bellow */ /* ------------------------------------- */ $exclude = array("wp_caption", "embed"); echo ' <select id="sc_select"> <option>Shortcode</option> '; foreach ($shortcode_tags as $key => $val){ if(!in_array($key,$exclude)){ $shortcodes_list .= ' <option value="['.$key.'][/'.$key.']">'.$key.'</option> '; } } echo $shortcodes_list; echo '</select> '; } add_action('admin_head', 'button_js'); function button_js() { echo ''; } |