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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器