Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End passing information through anon functions

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #205503
    marcdefiant
    Participant

    I was curious to exactly how the following code works:

    add_action('script_loader_src', scripts($src))

    now, of course this will call the function ‘scripts’ and pass the $src variable, but where does the $src value come from?

    I haven’t been able to successfully find any information on this and have no idea even what to call it.

    any helps is much appreciated

    #205504
    Senff
    Participant

    Can you place it in context? I know it’s WordPress and that’s it’s a filter hook, but where does this code appear?

    Normally there should be no arguments/variables in the “scripts” function but even if there would be, $src must be defined somewhere. But seeing just this line without any context makes it hard to see where that would be.

    #205505
    marcdefiant
    Participant

    This is my actual code:

    add_action('script_loader_src', function($src){
      if(strpos($src, '?ver='))$src = remove_query_arg('ver', $src);
      return $src;
    }, 10, 2);

    I have no idea how this works because (as you stated) the variable isn’t defined anywhere in my code.

    #205506
    Senff
    Participant

    Alright, but where did you get the code from? Is it part of a theme? Who wrote it?

    #205507
    marcdefiant
    Participant

    I wrote the theme; looking up anything I didn’t already know to make everything work as expected.

    Here is the method that runs:

    public function _css_href($src, $ver, $handle){
      if(!is_bool($src) && !preg_match('|^(https?:)?//|', $src) && !($this->content_url && 0 === strpos($src, $this->content_url)))$src = $this->base_url.$src;
      if(!empty($ver))$src = add_query_arg('ver', $ver, $src);
      $src = apply_filters('style_loader_src', $src, $handle);
      return esc_url($src);
    }

    Which is interesting as I wouldn’t expect this code to behave as it does.

    #205508
    marcdefiant
    Participant

    As for the snippet of code I used, (outside of the anonymous function) I just grabbed from some blog, I can’t remember

    #205510
    marcdefiant
    Participant

    I’m looking for a deeper understanding as to how to pass variables in this manor and wrap my head around exactly what is happening here.

    I apologize for not being more clear about what I’m asking for. I’ve been playing with php for the better part of 6 years now, and feel I’ve become stagnant in my learning so I’ve started looking for more advanced concepts to further my knowledge in php when I came across this snippet I use in my themes.

    I appreciate your time and responses.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.