passing hook $_GET value from hook to function.
The reason I’m using wp_loaded hook is because he is the only one that able to receive $_GET values.
This is not a thing.
Hooks don’t receive $_GET
values, that’s not something that happens or needs to be done. You can use $_GET
anywhere.
You can safely remove the add_action
call, it does not help, and does not do anything. It does not need to be replaced with anything. As I mentioned before, you can use $_GET
everywhere. There are no restrictions imposed by WordPress on where it can be used, and you don’t need to “recieve it” to use it, it’s already there.
This was all you ever needed:
$sender = getsender();
More accurately:
$sender = $_GET['sender'];
It’s much more likely that you used method="post"
on a form or used POST for AJAX, or that there’s been a mistake somewhere else in the code.
For example in getsender
there’s a major mistake:
function getsender() {
if ( isset( $_GET['sender'] ) ) {
$sender = $_GET['sender'];
return $sender;
}
// BUT WHAT HAPPENS HERE?!
}
If $_GET['sender']
doesn’t exist then the if
statement never happens, and nothing is returned, so it’ll get null
or an empty value back and you’ll never know why. It needs to return a value, even if it’s a default value or an error message/value to check for.
but the add_action hook always return true.
This is a misunderstanding of what add_action
does. It’s returning true because you told it to run the function getsender
when the wp_loaded
event/hook happens, and it’s returning true
for success that it encountered no problems noting that down. This does not mean that it immediatley executed your function, or that it will return the functions value, that’s not what add_action
is for. Think of it like scheduling a meeting in a calendar.
add_action
lets you tell WP to call a function/callback when an event/hook happens with a particular name. It’s useful for controlling when code runs and doing things on certain events. E.g. you could run code after a post gets saved using the save_post
action.
It has nothing to do with retrieving variables.