有人可以查看为什么以下添加的代码无法正确执行?我已经逃过PHP脚本以在WordPress中显示HTML内容,但我无法使用此功能!
结果是,无论用户是否登录,都会显示HTML内容。但奇怪的是,我无法在HTML中呈现else {...}部分。我对PHP不太好,所以请注意我做错了什么。
我已尝试通过NetBeans进行调试,但IDE无法在故障安全条件下检测到任何内容。
<?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;
};
?>
答案 0 :(得分:0)
好的,所以我开始编码了。事实证明,默认情况下,WordPress不允许在我的页面中执行PHP。我安装了一个名为“insert PHP”的插件,修改了代码以混合html和php,它运行了!
[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]