Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:30:34+00:00 2026-06-08T14:30:34+00:00

WordPress uses hooks and actions for extensibility. A plugin might look something like this:

  • 0

WordPress uses hooks and actions for extensibility. A plugin might look something like this:

class myLightbox
{
    function __construct()
    {
        add_action('wp_footer',array($this,'my_footer'));
    }

    function my_footer()
    {
        echo '<script src="http://external-site.com/lightbox.js" ></script>';
    }
}

If I run that code outside of WordPress, I’d like the add_action to work – even if it just calls the function immediately.

I read these:

  • call_user_func(array($this, $method), $par) from parent's constructor?**

  • Enqueue a function (like wordpress add_action)

The second one is fairly close to what I’d like to do, but I don’t think it was designed to work with functions that are part of a class.

I tried using call_user_function but I’m not sure how to give it the array($this,'my_footer') stuff:

function add_action($whenToCall,$contextAndFunction=array())
{
    call_user_func($contextAndFunction);
}

I also tried this, but as you can tell my OOP isn’t great so I’m struggling:

function add_action($whenToCall,$contextAndFunction=array())
{
    $function = array_pop($contextAndFunction);
    $context = array_pop($contextAndFunction);

    $context->$function();

}

Failed test using minitech‘s suggestion:

class myLightbox
{
    function __construct()
    {
        add_action('wp_footer',array($this,'my_footer'));
    }

    function my_footer()
    {
        echo '<script src="http://external-site.com/lightbox.js" ></script>';
    }
}


function add_action($whenToCall,$contextAndFunction=array())
{
    $contextAndFunction();
}


$myLightbox = new myLightbox();

Produces:

Fatal error: Function name must be a string
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:30:36+00:00Added an answer on June 8, 2026 at 2:30 pm

    If you’re using PHP 5.4, it’s already a callable:

    $contextAndFunction();
    

    Here’s a demo.

    Otherwise, call_user_func will work as-is.

    call_user_func($contextAndFunction);
    

    And there’s a demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a WordPress plugin that uses WP_HTTP for making an API call.
I'm using a WordPress plugin Easy FancyBox. It uses a dynamic php file for
Wordpress's datamodel provides extensibility through it's meta tables (aka, wp_postmeta, wp_commentmeta, wp_usermeta). This name/value
I would like to create a plugin that uses the contact form 7 hook,
I'm developing a web site which uses wp-cumulus on its home http://www.roytanck.com/2008/03/06/wordpress-plugin-wp-cumulus-flash-based-tag-cloud/ it's a
OK, so I have this website I maintain, uses WordPress, etc. One of the
Wordpress uses friendly seo url without using htaccess. Can any explain this to me
I think there is a conflict with the way WordPress uses jQuery: Tried: (function($)
This image best illustrates the problem: I'm customizing a WordPress theme that uses SuperBGImage.
Even though wordpress uses PHP, I would like to build a web application within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.