Wordpress Plugins - Einblicke in eine Geheimwissenschaft

911 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
911
On SlideShare
0
From Embeds
0
Number of Embeds
157
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Wordpress Plugins - Einblicke in eine Geheimwissenschaft

  1. 1. Plugins für Einsteiger Einblicke in eine Geheimwissenschaft
  2. 2. Wozu überhaupt ein Plugin schreiben <ul><li>Erweiterung oder Veränderung der Kernfunktionalität von WordPress </li></ul><ul><li>Erspart Kopfschmerzen bei Versionsupgrades </li></ul><ul><li>Lässt sich an andere Nutzer einfach weitergeben </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  3. 3. Beispiel: Clever Adsense <ul><li>In aktuellen Posts soll Werbung möglichst dezent ausfallen </li></ul><ul><li>Ältere Blogposts dürfen aggressivere Werbung beinhalten </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  4. 4. Voraussetzungen <ul><li>Ohne PHP geht‘s nicht </li></ul><ul><li>Aber: Plugins sind auch keine Raketentechnologie </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  5. 5. Der Codex <ul><li>Der Codex enthält Antworten auf (fast) alle Fragen </li></ul><ul><li>http://codex.wordpress.org/ </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  6. 6. Anatomie eines Plugins <ul><li>Eine oder mehrere PHP-Dateien in WP-Content/plugins </li></ul><ul><li>Kommentarbereich mit Metadaten über das Plugin </li></ul><ul><li>Lizenzhinweise </li></ul><ul><li>Eventuell benötigte Grafiken oder Stylesheets </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  7. 7. Metadaten <ul><li><?php </li></ul><ul><li>/* </li></ul><ul><li>Plugin Name: Clever Ads </li></ul><ul><li>Description: Steuert die Platzierung von Werbemitteln </li></ul><ul><li>Author: Thomas Frütel </li></ul><ul><li>Author URI: http://fruetel.de </li></ul><ul><li>*/ </li></ul><ul><li>?> </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  8. 8. Metadaten Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  9. 9. Lizenz <ul><li><?php </li></ul><ul><li>/* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL) </li></ul><ul><li>This program is free software; you can redistribute it and/or modify </li></ul><ul><li>it under the terms of the GNU General Public License as published by </li></ul><ul><li>the Free Software Foundation; either version 2 of the License, or </li></ul><ul><li>(at your option) any later version. </li></ul><ul><li>This program is distributed in the hope that it will be useful, </li></ul><ul><li>but WITHOUT ANY WARRANTY; without even the implied warranty of </li></ul><ul><li>MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the </li></ul><ul><li>GNU General Public License for more details. </li></ul><ul><li>You should have received a copy of the GNU General Public License </li></ul><ul><li>along with this program; if not, write to the Free Software </li></ul><ul><li>Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA </li></ul><ul><li>*/ </li></ul><ul><li>?> </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  10. 10. Adsense Funktionen <ul><li>function getAdsenseBanner() </li></ul><ul><li>{ </li></ul><ul><li>return '<script type=&quot;text/javascript&quot;><!-- </li></ul><ul><li>google_ad_client = &quot;pub-xxxxxxxx&quot;; </li></ul><ul><li>google_ad_slot = &quot;yyyyyyyy&quot;; </li></ul><ul><li>google_ad_width = 468; </li></ul><ul><li>google_ad_height = 60; </li></ul><ul><li>//--> </li></ul><ul><li></script> </li></ul><ul><li><script type=&quot;text/javascript&quot; </li></ul><ul><li>src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;> </li></ul><ul><li></script>'; </li></ul><ul><li>} </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  11. 11. Adsense Funktionen <ul><li>function getAdsenseLargeRectangle() </li></ul><ul><li>{ </li></ul><ul><li>return '<script type=&quot;text/javascript&quot;><!-- </li></ul><ul><li>google_ad_client = &quot;pub-xxxxxxxx&quot;; </li></ul><ul><li>google_ad_slot = &quot;yyyyyyyy&quot;; </li></ul><ul><li>google_ad_width = 336; </li></ul><ul><li>google_ad_height = 280; </li></ul><ul><li>//--> </li></ul><ul><li></script> </li></ul><ul><li><script type=&quot;text/javascript&quot; </li></ul><ul><li>src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;> </li></ul><ul><li></script>'; </li></ul><ul><li>} </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  12. 12. Alter des Posts ermitteln <ul><li>function isOldPost() </li></ul><ul><li>{ </li></ul><ul><li>global $wp_query; </li></ul><ul><li>$currentPost = $wp_query->post; </li></ul><ul><li>$post_date = mysql2date('U',$currentPost->post_date); </li></ul><ul><li>$current_date = time(); </li></ul><ul><li>$offset = 24*3600*60; </li></ul><ul><li>return (($post_date + $offset) < $current_date); </li></ul><ul><li>} </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  13. 13. Hooks <ul><li>Hooks verbinden ein Plugin mit den WordPress Kernfunktionalitäten </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  14. 14. Filter Hooks <ul><li>Verändern Daten vor der Übermittlung an Browser oder Datenbank </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  15. 15. Action Hooks <ul><li>Werden durch bestimmte Ereignisse in WordPress ausgelöst </li></ul><ul><li>Zum Beispiel Veröffentlichung eines Posts, Abgabe eines Kommentars </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  16. 16. Verankern des Clever Adsense Plugins <ul><li>function showAds($content) </li></ul><ul><li>{ </li></ul><ul><li>if (is_single()) </li></ul><ul><li>{ </li></ul><ul><li>if (isOldPost()) { </li></ul><ul><li>return getAdsenseLargeRectangle().$content; </li></ul><ul><li>} else { </li></ul><ul><li>return $content.getAdsenseBanner(); </li></ul><ul><li>} </li></ul><ul><li>} else { </li></ul><ul><li>return $content; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//Filter Hook </li></ul><ul><li>add_filter ('the_content', 'showAds'); </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  17. 17. Konfigurierbarkeit <ul><li>Viele Plugins lassen sich im WordPress Adminbereich konfigurieren </li></ul><ul><li>Das können wir auch! </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  18. 18. Action Hook für das Backend <ul><li>function showAdsAdmin() </li></ul><ul><li>{ </li></ul><ul><li>/** </li></ul><ul><li>* @params $page_title, $menu_title,$capability, </li></ul><ul><li>* @params $menu_slug, $function </li></ul><ul><li>*/ </li></ul><ul><li>add_options_page('Clever Ads', 'Clever Ads', 10, 'clever_ad_options', 'cleverOptions'); </li></ul><ul><li>} </li></ul><ul><li>function cleverOptions() </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>add_action('admin_menu', 'showAdsAdmin'); </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  19. 19. Menüeintrag im Dashboard Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  20. 20. Die CleverOptions Funktion Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  21. 21. Das Optionsformular <ul><li>function cleverOptions() </li></ul><ul><li>{ </li></ul><ul><li>?> </li></ul><ul><li><div class=&quot;wrap&quot;> </li></ul><ul><li><h2>Clever Ads</h2> </li></ul><ul><li><form method=&quot;post&quot; action=&quot;&quot;> </li></ul><ul><li>Post ist alt ab <input name=&quot;old_age&quot; value=&quot;<?php echo get_option(&quot;ca_old_age&quot;) ?>&quot; /> Tagen<br /> </li></ul><ul><li>Adsense Ad Client: <input name=&quot;adclient&quot; value=&quot;<?php echo get_option(&quot;ca_adclient&quot;)?>&quot; /><br /> </li></ul><ul><li>Adsense Adslot: <input name=&quot;adslot&quot; value=&quot;<?php echo get_option(&quot;ca_adslot&quot;)?>&quot; /><br /> </li></ul><ul><li><input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;update_options&quot; /> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;<?php _e('Save Changes') ?>&quot; /> </li></ul><ul><li></form> </li></ul><ul><li></div> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  22. 22. Das Optionsformular Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  23. 23. Optionen speichern <ul><li>function cleverOptions() </li></ul><ul><li>{ </li></ul><ul><li>if ($_POST['action'] == 'update_options') { </li></ul><ul><li>update_option(&quot;ca_old_age&quot;, $_POST['old_age']); </li></ul><ul><li>update_option(&quot;ca_adclient&quot;, $_POST['adclient']); </li></ul><ul><li>update_option(&quot;ca_adslot&quot;, $_POST['adslot']); </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><div class=&quot;wrap&quot;> </li></ul><ul><li><h2>Clever Ads</h2> </li></ul><ul><li><form method=&quot;post&quot; action=&quot;&quot;> </li></ul><ul><li>Post ist alt ab <input name=&quot;old_age&quot; value=&quot;<?php echo get_option(&quot;ca_old_age&quot;) ?>&quot; /> Tagen<br /> </li></ul><ul><li>Adsense Ad Client: <input name=&quot;adclient&quot; value=&quot;<?php echo get_option(&quot;ca_adclient&quot;)?>&quot; /><br /> </li></ul><ul><li>Adsense Adslot: <input name=&quot;adslot&quot; value=&quot;<?php echo get_option(&quot;ca_adslot&quot;)?>&quot; /><br /> </li></ul><ul><li><input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;update_options&quot; /> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;<?php _e('Save Changes') ?>&quot; /> </li></ul><ul><li></form> </li></ul><ul><li></div> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  24. 24. Optionen auslesen <ul><li>function isOldPost() </li></ul><ul><li>{ </li></ul><ul><li>global $wp_query; </li></ul><ul><li>$currentPost = $wp_query->post; </li></ul><ul><li>$post_date = mysql2date('U',$currentPost->post_date); </li></ul><ul><li>$current_date = time(); </li></ul><ul><li>$offset = 24*3600*get_option('ca_old_age'); </li></ul><ul><li>return (($post_date + $offset) < $current_date); </li></ul><ul><li>} </li></ul><ul><li>function getAdsenseLargeRectangle() </li></ul><ul><li>{ </li></ul><ul><li>return '<script type=&quot;text/javascript&quot;><!-- </li></ul><ul><li>google_ad_client = &quot;'.get_option('ca_adclient').'&quot;; </li></ul><ul><li>google_ad_slot = &quot;'.get_option('ca_adslot').'&quot;; </li></ul><ul><li>google_ad_width = 336; </li></ul><ul><li>google_ad_height = 280; </li></ul><ul><li>//--> </li></ul><ul><li></script> </li></ul><ul><li><script type=&quot;text/javascript&quot; </li></ul><ul><li>src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;> </li></ul><ul><li></script>'; </li></ul><ul><li>} </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  25. 25. Standardwerte setzen <ul><li>function initializeCleverOptions() </li></ul><ul><li>{ </li></ul><ul><li>add_option(&quot;ca_old_age&quot;, 60); </li></ul><ul><li>add_option(&quot;ca_adclient&quot;, &quot;xxxxx-xxxxx&quot;); </li></ul><ul><li>add_option(&quot;ca_adslot&quot;, &quot;yyyyy-yyyyy&quot;); </li></ul><ul><li>} </li></ul><ul><li>register_activation_hook(__FILE__, &quot;initializeCleverOptions&quot;); </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel
  26. 26. Ausblick <ul><li>Absichern des Backend-Formulars mit einem Nonce </li></ul><ul><li>Erkennen von Besuchern, die über Google kommen </li></ul><ul><li>Erkennen von Stammlesern über ein Cookie </li></ul><ul><li>Identifizieren von Google Suchbegriffen </li></ul><ul><li>Geotargeting </li></ul><ul><li>Aufräumen der Optionen bei Deinstallation </li></ul><ul><li>Plugin bei WordPress.org veröffentlichen </li></ul>Thomas Frütel http://blog.webmaster-homepage.de/ http://twitter.com/Fruetel

×