简体   繁体   中英

WordPress is_user_logged_in failing with HTML Scripting

Could someone please review why this below added code won't execute correctly? I've escaped the PHP scripting to display the HTML content in WordPress, but I can't get this thing to work!

The results are that, whether the user is logged in or not, the HTML content displays for everything. Strangely, though, I can't get the else { ... } part to render in HTML. I'm not too good with PHP, so please mind if I done something incorrectly.

I've tried debugging via NetBeans, but the IDE failed to detect anything on a fail-safe conditioning.

<?php
    if ( is_user_logged_in() ) { 
?>
        <h2>Welcome to the Portal!</h2>
        <p>Here you can manage all information with an authorised account. You can click on the button below to create a new entry.</p>
        <button type="button" href="#" onClick="document.location = 'http://www.example.com'">Add Entry</button>
<?php 
    } else { 
        echo "<h1>Unauthorised!</h1>";
        echo "<javascript>document.location = \'http://example.com\'";
        exit;
    };
?>

Alright, so I got the coding going. It turned out that, by default, WordPress didn't allow PHP execution in my pages. I installed a plugin named "insert PHP", modified the code to mix html and php and it worked!

[insert_php]
if (is_user_logged_in()){
echo "<h2>Welcome to the Portal!</h2>";
echo "<p>Here you can manage all entries with an authorised account. You can click on the button below to create an entry.</p>";
echo '<button type="button" onClick="document.location = \'http://somesite.com\'">Add Listing</button>';
} else {
echo '<h2>Unauthorized 401</h2>';
echo '<script>document.location = \'http://domain.tld'</script>';
};
[/insert_php]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM