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 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库