0

I'm a newbie creating a WordPress plugin stuck with setting "checked" based on a variable value. I'm working on a custom form. I am struggling with syntax I think...

This is the bit that fails:

<input type="radio" name="reporter_alliance" id="reporter_talk" value="[TALK]" ' . if($currenta === "[TALK]"): . ' checked = "checked" '. ;'>

Here's the full function so far and the rest of it seems to work:

<?php
function report_roe_form( $reporter_alliance, $reporter_username)  

 {
    $current_u = wp_get_current_user();
    $current_uid = get_current_user_id();
    $currenta = get_user_meta($current_uid, 'alliance', true)[0];

    if($currenta === "[TALK]") echo 'true'; #test of the function (this works)

    echo ' 
    <form action="' . $_SERVER['REQUEST_URI'] . '" method="post"> 
        <div> 
            <label >Reporter Alliance <strong>*</strong></label> 
            
            <label for="reporter_chat">[CHAT]</label>
            <input type="radio" name="reporter_alliance" id="reporter_chat" value="[CHAT]">

            <label for="reporter_talk">[TALK]</label> 
            <input type="radio" name="reporter_alliance" id="reporter_talk" value="[TALK]" ' . if($currenta === "[TALK]"): . ' checked = "checked" '. ;'> 
        </div>
       
        <div>
            <label for="reporter_username">Reporter Username <strong>*</strong></label> 
            <input type="text" name="reporter_username" value="' . $current_u->user_login . '">  
        </div> 

        <div> 
            <input type="submit" name="submit" value="Submit"/>
        </div>
    </form>
    ';

 }
?>

I've seen lots online showing I should put <?php ?> around if($currenta === "[TALK]"): . ' checked = "checked" '. ; but I've tried various combinations that all fail.

I've tried several different combinations of putting things in single and double quotes but I just cant seem to get it. Please can someone point a newbie in the right direction?

Rising
  • 1
  • 1
  • The duplicate should answer your question. You should also look into https://www.php.net/manual/en/language.basic-syntax.phpmode.php though - outputting (HTML) content that is for the most part _static_, using echo statements, is not the best way to go to begin with. – CBroe Jul 18 '23 at 11:47
  • I've changed the line to: `'> ` and it doesn't work – Rising Jul 18 '23 at 12:23
  • Without changing any of the rest of it? When you are "in" an `echo` statement, then you must also be inside a `` block already - and those can't be nested. – CBroe Jul 18 '23 at 12:26
  • Yes I am already in a php block – Rising Jul 18 '23 at 12:34
  • So - get out of it then, that is one easy way to deal with this situation. (See first comment & link I posted.) Or use the ternary operator, as suggested in the response to the duplicate. – CBroe Jul 18 '23 at 12:37
  • Thanks for helping. I'm struggling to wrap my head around this, being so new. Trying to follow the info in the link I tried `'> ` but this fails – Rising Jul 18 '23 at 13:15
  • OK I finally worked it out with your help. Took me a while to see what you were saying. I took it out of echo and exited php. Sorry I was so slow on this. I am learning as I go along. – Rising Jul 18 '23 at 14:42

0 Answers0