Creating a Simple Wordpress Plugin

Sunday, February 7, 2010 by Less Talk, More Example

The Goal

Creating a simple plugin that replace <!--ltme=COLOR--> with BIG and colored "Less Talk, More Example" words. Take a look at images below.
Wordpress - writing a post Result

The Process

  1. Open your text editor then create a new file.
  2. Copy and paste code below. <?php /* Plugin Name: LTME Plugin URI: http://www.less-talk-more-example/ Description: Simple plugin to subtitude the <!--ltme=COLOR--> string BIG "Less Talk, More Example" string. Author: Rio Astamal Version: 1.0 Author URI: http://www.rioastamal.net/ */ // print some CSS to the header function ltme_head() { echo ' <!-- start ltme --> <style type="text/css"> .ltme { padding: 20px 12px; font-size: 2em; font-weight:bold; border: 1px solid #ccc; background: #E5E5E5; } </style> <!-- end ltme --> '; } // function to replace the <!--ltme=COLOR--> string function ltme_replace($post_content) { $replacement = '<div class="ltme" style="color: $1">Less Talk, More Example</div>'; $post_content = preg_replace('/<!--ltme=([a-zA-Z#0-6]+)-->/', $replacement, $post_content); // if we enter using visual mode, wordpress intrepet the <!-- string differently $post_content = preg_replace('/&lt;!&#8211;ltme=([a-zA-Z#0-6]+)&#8211;&gt;/', $replacement, $post_content); return $post_content; } // Register our hooks add_action('wp_head', 'ltme_head'); add_filter('the_content', 'ltme_replace'); ?>
  3. Save the file to path wp-content/plugins/ltme.php
  4. Now, activate the LTME plugin
    Wordpress - Activate LTME plugin
  5. Write some content that include LTME special tag. Hello LTME Reader, This is example of simple plugin creation on wordpress. Print LTME with green color <!--ltme=green--> print LTME with #c60000 color <!--ltme=#c60000-->
  6. Publish your post, the result should look like this
    Result
  7. Congratulation for your first Wordpress plugin :)

Wordpress Version

Tested on Wordpress version 2.8.4 but should work for all version.

Download the Code

Having trouble copy or paste the code? Download the file here:
ltme-plugin.zip via ziddu.com.
Size: 0.72kb
MD5 Checksum: f8898209768ed7524afa4c59eefc82ee
Posted in Label: , , | 0 Comments »

0 komentar:

Post a Comment

Advertisement