0

I am trying to make a plugin that allows users to add a shortcode to their website. The problem that I am facing is that once the shortcode is placed in a page the shortcode is being run on both the page editor and on the front end, where I want it to only run on the front end.

Short Code Running In Editor

I followed this guide on how to make a shortcode. I searched around a bit on the web and saw where someone suggested adding the function ob_start(); but it did not seem to help. I have looked at other guides and no one seems to cover this issue. What am I doing wrong?

Code:

add_shortcode('sss-redeem', 'Redeem');

function Redeem(){
    ob_start();
    $IsAdmin = is_super_admin(get_current_user_id());       
    ?>
    <form>
    <center>
    <?php
    if($IsAdmin){
        ?>
        <input type="checkbox" name="overwrite" value="true">Overwrite?<br>
        <?php
    }
    ?>
    <strong><h2>Order ID</h2></strong> <input type="text" maxlength="4" name="oid"/><br>
    <input type="text" name="pgs" value="a5b6n66f34ka1n1d" style="display: none;" />
    <button type="submit">Submit</button>
    </form>
    </center>   
    <?php
}

NOTE: I am new to developing WordPress plugins. In fact, this plugin here is my first attempt at making a plugin.

  • 1
    the shortcode function must return his content on not echoing it. try to end the function with return ob_get_clean(); to reply to ob_start(); – mmm Oct 2 '17 at 19:53
0

Thanks to @mmm I figured it out. What I am doing now is adding the form in between php tags.

EXAMPLE of what I was doing

?>
    <input type="checkbox" name="overwrite" value="true">Overwrite?<br>        
<?php

What I need to be doing is adding the content that I want to add to the page into a string that is then returned when the function is completed. So what I would want my code to look like is

EXAMPLE of what I should have been doing

<?php
$out = '<input type="checkbox" name="overwrite" value="true">Overwrite?<br>';
return $out;
?>
0

you should return value not echoing it ,you can use something like this :

add_shortcode('sss-redeem', 'Redeem');

function Redeem(){
    $IsAdmin = is_super_admin(get_current_user_id());       
    $RedeemValue.='<form>';
    $RedeemValue.='<center>';
    if($IsAdmin){
        $RedeemValue.='<input type="checkbox" name="overwrite" value="true">Overwrite?<br>'
    };
    $RedeemValue.='<strong><h2>Order ID</h2></strong> <input type="text" maxlength="4" name="oid"/><br>';
    $RedeemValue.='<input type="text" name="pgs" value="a5b6n66f34ka1n1d" style="display: none;" />';
    $RedeemValue.='<button type="submit">Submit</button>';
    $RedeemValue.='</form>';
    $RedeemValue.='</center>';
    return $RedeemValue;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.