Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

My first WordPress plugin

536 views

Published on

How to make a very basic WordPress Plugin using the "the_content" filter to modify the content of each post

Published in: Technology
  • Be the first to comment

  • Be the first to like this

My first WordPress plugin

  1. 1. A first plugin : playing with filters Pat Lockley @pgogy / @patlockley
  2. 2. Step 1 Go to wp-content/plugins/ on your server and make a folder or Make a folder anywhere (if brave)
  3. 3. Step 2 : Step harder Let’s call the folder “lightningtalk” In this folder make a file called “lightningtalk. php”
  4. 4. Step 3 Let’s call the folder “lightningtalk” In this folder make a file called “lightningtalk. php”
  5. 5. Step 4 in this file add the characters <?PHP
  6. 6. Step 5 /* Plugin Name: Lightning Talk Description: My First Plugin Version: 0.1 */
  7. 7. Step 6 now add … function change_content($content){ return $content; } add_filter(“the_content”, “change_content”);
  8. 8. Step 7 So if you make this on a server, go the the plugins page and enable it Or zip the folder up, upload it, and then go to the plugins page and enable it
  9. 9. uh oh…..
  10. 10. Step 8 ABSOLUTELY NOTHING
  11. 11. Step 9 function change_content($content){ return “Hello world”; }
  12. 12. Step 10 function change_content($content){ // The . symbol means merge strings in PHP return $content . “ Hello world”; }
  13. 13. Step 11 function change_content($content){ // The . symbol means merge strings in PHP return $content . “<a href=’url’>Visit us</a>”; }
  14. 14. Step 12 function change_content($content){ // str_replace - this is PHP for ctrl + H return str_replace(“Company name”,“<a href=’url’>Company name</a>”, $content ); }
  15. 15. Step 13 function change_content($content){ // str_replace - this is PHP for ctrl + H return str_replace(“Company name”,“<a href=’url’>Company name</a>”, $content ); }
  16. 16. Step 14 function change_content($content){ // str_replace - this is PHP for ctrl + H return str_replace(“Company name”,“<a href=’url’>Company name</a>”, $content ); }
  17. 17. Step 15 function change_content($content){ global $post; if($post->post_title==”hello”){ return str_replace(“Company name”,“<a href=’url’>Company name</a>”, $content ); }else{ return $content; } }

×