Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add a QR code to the bar and I can do this but it's my first time to make a WordPress plug-in and I'm having trouble. It ends up showing to everyone even if they are not an admin or even not logged in.

Any ideas or guidance would be very grateful.

function selfURL() {
    $s = empty($_SERVER["HTTPS"]) ? ''
        : ($_SERVER["HTTPS"] == "on") ? "s"
        : "";
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
    $port = ($_SERVER["SERVER_PORT"] == "80") ? ""
        : (":".$_SERVER["SERVER_PORT"]);
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
    return substr($s1, 0, strpos($s1, $s2));
}

$urlToEncode=(selfURL());

generateQRwithGoogle($urlToEncode);
function generateQRwithGoogle($url,$widthHeight ='30',$EC_level='L',$margin='0') {
    $url = urlencode($url); 
    echo '<img src="http://chart.apis.google.com/chart?chs='.$widthHeight.
'x'.$widthHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.
'&chl='.$url.'" alt="QR code" widthHeight="'.$widthHeight.
'" widthHeight="'.$widthHeight.' "/>';
}
share|improve this question
    
Also, I get an error upon activating: The plugin generated 371 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin. –  Joseph Lancaster Jun 20 '12 at 20:45
add comment

2 Answers 2

You can limit to logged in users by wrapping the code with is_logged_in().

You can limit to levels via current_user_can('manage_options').

share|improve this answer
add comment

You only need one if statement:

if(is_admin()){
//your code here...
};

You can check out more about it here: http://codex.wordpress.org/Conditional_Tags

share|improve this answer
    
Actually this does not work for some reason. –  Joseph Lancaster Jun 20 '12 at 20:45
    
Does not work as if it still shows to everyone? About headers already ent error, delete all empty lines in the end of the file. –  OriginalEXE Jun 20 '12 at 20:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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