I’ve got some code, here, that’s giving me some trouble.
function rcMetaDisplayNone($content)
{
global $rc_options;
global $post;
global $login_button;
$rcUserLevel = get_post_meta($post->ID, 'rcUserLevel', true);
if (!current_user_can('read') && ($rcUserLevel == 'Administrator' || $rcUserLevel == 'Editor' || $rcUserLevel == 'Author' || $rcUserLevel == 'Contributor' || $rcUserLevel == 'Subscriber'))
{
$userLevelMessage = strtolower($rcUserLevel);
return do_shortcode( $rc_options[$userLevelMessage . '_message'] );
}
else
{
// return the content unfilitered
return $content;
}
}
The task I’m trying to conquer, here, is how to append an echo
to the end of the if
statement, like so:
return do_shortcode( $rc_options[$userLevelMessage . '_message'] );
echo $login_link;
Now, by rules of how PHP works, I understand that this shouldn’t work. My question is: how would I make it work? Is there a function or a workaround that will let me do this?
I’m a bit of a beginner to PHP, so I apologize if the solution is obvious.
**UPDATE**
Holy answers, Batman! Thanks for the help, folks. I should clarify. I’ve updated the code to include the entire function.
The $login_link
code is essentially outputting a link that allows the user to log in (ironically enough). The return
code, however, is outputting a message that, in context to this WordPress plugin I am using, is stating that the user does not have permission to access the content on the page.
See the dilemma here? I am trying to append a log-in link to this message. It’s not simply an empty return value, but an actual message outputting to the browser.
**UPDATE 2**
This is a shortcode I’m using, so I’m not sure I can output said code outside of the function.
You cannot execute code after a
return
statement (in the function returning).If I understand your problem correctly (executing
do_shortcode()
then showing the value of$login_link
, then returning), then: Store the return value, callecho
, and finally return the stored value.Edit: Based on your update, I believe you’re looking for the following:
you would echo outside of the function. Rule of thumb you should never echo INSIDE the function always return the output and echo the return set
So you’d do
UPDATE USING YOUR FULL CODE
This is not just PHP but the rules to how functions work. Once a
return
is hit, the function is done. This is so even if there is more lines after thereturn
.This will only output
"test"
since the return is hit. If you want to do anything, you need to make it happen before the end of a function, not when it’s done.So you can echo out the variable before returning.
EDIT:
Since what you are looking for is to add to the message after
do_shortcode
, you can concatenate the string before returning. Like so:The “possible HTML” can be any string or formatting you may wish. Research
PHP String Concatenation
It doesn’t make sense, you could just move it to above the
return
:It might be made more sense of if you gave us more of an idea of what you actually want to do (in a less abstract way than “output a value”).
You can’t. Return will finish the function.
You can return what you need and then use echo where you call the function:
You can return array of walues if you need more.