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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化