public
Created — forked from rmccue/plugin-file.php

Improved class concept

  • Download Gist
plugin-file.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
<?php
/*
Plugin Name: WP_Plugin class test
Description: A better test!
Author: Ryan McCue
Version: 1.1
Author URI: http://ryanmccue.info/
*/
 
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, 7);
add_filter($name, array($this, $method->name), 10, $params);
}
elseif (strpos($method->name, 'action_') === 0) {
$name = substr($method->name, 7);
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();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.