Create a gist now

Instantly share code, notes, and snippets.

Improved class concept
<?php
/*
Plugin Name: WP_Plugin class test
Description: A better test!
Author: Ryan McCue
Version: 1.0
Author URI: http://kovshenin.com
*/
class WP_Plugin {
public function __construct() {
$self = new ReflectionClass($this);
foreach ($self->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$params = $method->getNumberOfParameters();
if (strpos($method->name, 'filter_') === 0) {
$name = substr($method->name, 9);
add_filter($name, array($this, $method->name), 10, $params);
}
if (strpos($method->name, 'action_') === 0) {
$name = substr($method->name, 9);
add_action($name, array($this, $method->name), 10, $params);
}
}
}
}
// Here's an example of a plugin that would benefit from the above
class Some_Plugin extends WP_Plugin {
// Will filter the_title
public function filter_the_title( $title ) {
return $title . '123';
}
// Will filter the_content
public function filter_the_content( $content ) {
return $content . ' Add more content.';
}
// Will run during wp_footer
public function action_wp_footer() {
echo "I'm in the footer!";
}
}
$some_plugin = new Some_Plugin();
@bueltge
bueltge commented Jan 17, 2012

Priority on hooks please and then is fine ;)

@toscho
Owner
toscho commented Jan 17, 2012

Done. :)

@bueltge
bueltge commented Jan 17, 2012

simple and useful

@toscho
Owner
toscho commented Jan 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment