douhui2307 2015-08-20 12:00
浏览 96
已采纳

WordPress is_user_logged_in无法使用HTML脚本

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;
    };
?>
  • 写回答

1条回答 默认 最新

  • dongmei425373 2015-08-20 13:07
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM