- This topic is empty.
-
AuthorPosts
-
July 25, 2015 at 3:59 pm #205503
marcdefiant
ParticipantI 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
July 25, 2015 at 4:14 pm #205504Senff
ParticipantCan 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.
July 25, 2015 at 4:29 pm #205505marcdefiant
ParticipantThis 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.
July 25, 2015 at 6:30 pm #205506Senff
ParticipantAlright, but where did you get the code from? Is it part of a theme? Who wrote it?
July 25, 2015 at 6:54 pm #205507marcdefiant
ParticipantI 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.
July 25, 2015 at 6:58 pm #205508marcdefiant
ParticipantAs for the snippet of code I used, (outside of the anonymous function) I just grabbed from some blog, I can’t remember
July 25, 2015 at 8:56 pm #205510marcdefiant
ParticipantI’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.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.