WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a class with some private & public functions. The thing is that I don't want some of the functions to be able to be called outsite of the class, so I want to make them private. But I need to hook them at particular hooks and the hooks cannot call them if they are private. Is there some workaround for that?

share|improve this question
Using an intermediary public function? – brasofilo Oct 13 '12 at 9:43

No that's impossible. When WordPress calls a method it has to be public public. You could write a public method that is called on the hook which calls a private method inside.
Not sure if that makes sense though …

share|improve this answer
I agree. Yeah as in my example above, you can, but does it really make sense? Well, although the content can't be modified nor the private method accessed you have to ask is that the best approach.. – userabuser Oct 2 '12 at 11:35

Here is an example;

add_action( 'template_redirect', array( 'someClass', 'init' ));

class someClass {

    protected static $content = 'oh yeah, private!';    

    public static function init() {
        $class = __CLASS__;
        new $class;

    private function __construct() {
        add_filter('the_content', array(&$this, 'get_this_function'));

    public static function get_this_function(){
         return self::my_funk_she_on();

    private static function my_funk_she_on(){
        return self::$content;


In this example I am filtering the_content by passing a private function which gets its value from a private variable. Now since you can't call this directly my get_this_function is what will return what's private and that's what I pass into my constructor that adds the appropriate filter upon template_redirect.

You don't need to use static methods by the way, some recommend against it, but do as you please.

share|improve this answer
The function called on the hook is still public. And there is no way around it. – toscho Oct 2 '12 at 11:39
Yep, agreed. The only advantage is that you can't modify what the private method returns, nor can you access it directly, but you can return it. But again... is that the best approach? Would be good if user provides some context and code to see too... – userabuser Oct 2 '12 at 11:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.