skip to Main Content

When editing wordpress code in Visual Studio Code,
if I have a function definition and than I use it as a callback for a call to add_action or add_filter than because its used as a string its not counted as a reference to the function and the intelisense says 0 references above my function which is true to some degree but also false and misleading in the practical sense.

Can I do something to tell visual studio code to add a reference to that function?

I was hoping I maybe could do something like:

function myCallback() {
//do some stuff
}

add_action('init',myCallback.name) 

or

add_action('init','myCallback' /* @references:myCallback */)     

or something similar

2

Answers


  1. If you are using PHP 8.1, you can use the first class callable syntax

    add_action('init', myCallback(...)) 
    
    Login or Signup to reply.
  2. You can use a callback to assign your function to:

    $callable = function() {
       echo 'Hello world!';
    };
    
    call_user_func($callable);
    

    This works for several versions: https://3v4l.org/1tH3K
    If I read 3v4l.org right, it works for

    5.3.0 – 5.3.29, 5.4.0 – 5.4.45, 5.5.0 – 5.5.38, 5.6.0 – 5.6.40, 7.0.0 – 7.0.33, 7.1.0 – 7.1.33, 7.2.0 – 7.2.34, 7.3.0 – 7.3.33, 7.4.0 – 7.4.33, 8.0.0 – 8.0.30, 8.1.0 – 8.1.23, 8.2.0 – 8.2.10

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search