PHP if else condition giving 500 internal server error

时间:2015-07-28 16:54:18

标签: php wordpress internal-server-error

I am using some code to show editor a link to edit specific pages

but it is causing 500 internal server error for users who are not-logged-in

I am a starter and need immediate help My code is

<?php
      global $current_user, $wpdb;
      $role = $wpdb->prefix . 'capabilities';
      $current_user->role = array_keys($current_user->$role);
      $ncaps = count($current_user->role);
      $role = $current_user->role[$ncaps - 1];


    if ($role ==  "editor") {
    echo  '<div class="user-tips">

<h4><u>Edit Equity Market</u></h4>
<h5><a href="abc.com/wp-admin/post.php?post=215&action=edit"><button class="btn btn-primary btn-sm">Edit Intraday Tips</button></a></h5>
<h5><a href="abc.com/wp-admin/post.php?post=221&action=edit"><button class="btn btn-primary btn-sm">Edit Positional Tips</button></a></h5>
<h4><u>Edit Commodity Market</u></h4>
<h5><a href="abc.com/wp-admin/post.php?post=220&action=edit"><button class="btn btn-primary btn-sm">Edit Commodity Tips</button></a></h5>


</div>';
    }

    else {
    echo "";
}
?>

Thanks in advance

1 个答案:

答案 0 :(得分:0)

Try using function is_user_logged_in():

<?php
if ( is_user_logged_in() && current_user_can('edit_others_pages') ) {
    echo  '<div class="user-tips">
    <h4><u>Edit Equity Market</u></h4>
    <h5><a href="abc.com/wp-admin/post.php?post=215&action=edit"><button class="btn btn-primary btn-sm">Edit Intraday Tips</button></a></h5>
    <h5><a href="abc.com/wp-admin/post.php?post=221&action=edit"><button class="btn btn-primary btn-sm">Edit Positional Tips</button></a></h5>
    <h4><u>Edit Commodity Market</u></h4>
    <h5><a href="abc.com/wp-admin/post.php?post=220&action=edit"><button class="btn btn-primary btn-sm">Edit Commodity Tips</button></a></h5>
    </div>';
}
?>

EDIT: I also improved the check for roles a little. See Roles and Capabilities for others.