Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plugin Example: Basic Placeholder Replacement
#1
This tutorial assumes knowledge of the following tutorials:


Say that you have a client who wants to easily paste some predefined HTML onto their page. For example, they want to just type (% facebook %) and have a fully stylized link to their company's facebook page show up. You can whip up a very quick plugin that will achieve this. The key is in the plugin hook 'content'.

plugin.class.php

In your plugin class file, locate the init method. Paste the below into the method's body:

PHP Code:
add_filter('content', array($this'filterContent')); 

This will call the 'content' filter on the filterContent method (which we will define). It works by pulling the page content and putting it into a string called $content before the page has fully loaded. We can then manipulate the string as we want before returning it, so that when the page is loaded, our modifications stick. So essentially, all we need to do is find instances of (% facebook %) in the string and replace it with whatever content/markup we want.

For the sake of separating the functionality of the plugin class from the operation that we want to do, we will create a new class called FacebookPlaceholder which will actually deal with the replacements. It will work so that the filterContent method simply has this definition:

PHP Code:
public function filterContent($content) {
    
// load the placeholder class and filter the content
    
$fbplaceholder = new FacebookPlaceholder();
    return 
$fbplaceholder->filter($content);
  } 

facebookplaceholder.class.php

The placeholder class will have two internal arrays - placeholders and values. The placeholders will map to the values such that each entry in the placeholder array will be replaced by its corresponding entry in the values array (when we parse the string).

PHP Code:
<?php
class FacebookPlaceholder {
  private 
$placeholders = array();
  private 
$replacements = array();
}
?>

construct

To keep the constructor clean, we'll set the placeholders and replacements in a separate, private function.

PHP Code:
public function __construct() {
    
$this->setPlaceholders();
  } 

setPlaceholders

This method will simply be us setting whatever placeholders we want. So instead of restricting us to the one placeholder, we can easily extend our list to have more, if the client so wants it.

PHP Code:
private function setPlaceholders() {
    
$this->placeholders[] = '(% facebook %)';
    
$this->replacements[] = 'Your desired string';
    
// follow this pattern to add more placeholders/replacements
  


filter

Finally, the filter method will take an input string and then use str_replace to replace all of the necessary strings. Because we've defined the placeholders and the replacements as arrays, we can just pass them into the str_replace function.

PHP Code:
public function filter($content) {
    return 
str_replace($this->placeholders$this->replacements$content);
  } 

That's it. If the plugin is enabled and the client uses one of those placeholders in their page content, it will be replaced by the content that you specified.
Reply




Users browsing this thread: 1 Guest(s)