I'm programming a plugin in Wordpress and want to access some database functions with in my theme with the do_action feature. First I just included $fdb-> get_finaboo_footer(1); on my page for example but he couldnt find the object. I added global $fdb; Then it worked. But I dont want to globalize on every php file the $fdb. So I thought I do it with the do_action feature. The Problem is I just get "null" in my var dump.

* Database Functions

class fdb {
    public function __construct() {
        global $wpdb;
        $this->wpdb = &$wpdb;
        add_action('finaboo_footer', array ( &$this, 'get_finaboo_footer'));

    public function get_finaboo_footer($footer_id) {
        $sql =  $this->wpdb->prepare( "SELECT * FROM wp_finaboo_content_footer WHERE footer_id = '$footer_id' ORDER BY option_id ASC;" );
        $results = $this->wpdb->get_results($sql, ARRAY_A );

$fdb = new fdb();


And this would be my call in the theme php files:

<?php $footer1 = do_action('finaboo_footer', '1'); ?>

don't know why it dosent work :(

Also I'm open for suggestions if this is not the smartest way in Wordpress :)

Thanks for help

  • I wonder if you can return something from do_action()... I have never tried. I would try echoing something in your function and see if that works? Then you would at least know that the action is running, but it might mean that you need to use @toscho's version with a static method.
    – Jake
    Commented Aug 24, 2012 at 19:37

2 Answers 2


I recommend to add a static method to access the plugin instance.

Example, taken from this answer:

class My_Plugin
    private $var = 'foo';

    protected static $instance = NULL;

    public static function get_instance()
        // create an object
        NULL === self::$instance and self::$instance = new self;

        return self::$instance; // return the object

    public function __construct()
        // set up your variables etc.

    public function foo()
        return $this->var;

// create an instance on wp_loaded
add_action( 'wp_loaded', array( 'My_Plugin', 'get_instance' ) );

In your theme you can access foo() now like this:

print My_Plugin::get_instance()->foo();

do_action() never returns something. You would need apply_filters():

$footer1 = apply_filters( 'finaboo_footer', 1 );

I think you need to add a parameter to your add_action call that tells it how many parameters the function accepts (1 in this case):

add_action('finaboo_footer', array ( &$this, 'get_finaboo_footer'), 1, 1);

add_action( $tag, $function_to_add, $priority, $accepted_args );

  • Maybe not. The default is one...
    – Jake
    Commented Aug 24, 2012 at 19:32
  • and how to call the do action ? dosent work with
    – mat0r
    Commented Aug 24, 2012 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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