Page 1 of 1

[SOLVED] Check User role

Posted: Thu Jul 01, 2021 10:45 am
by ctuxboy
Hello,

Missing a good search/filter-function in the dashboard -> manage content (i know there is a search function in de dashboard, but it's limited to the page-titles), so i used the front-end search plugin that also search in pages-content.

So for 'fast' find and editing the content from the frontend, adding an 'Edit'-link in my template after the page-title:

Code: Select all

<a href="<?php echo '/admin/edit-content/' . $page->key(); ?>">Edit</a>
This works great, but the link still showing also when a user not logged in.
Now trying show the 'Edit'-link ONLY when admin or author is logged in:

Code: Select all

<?php 
if ($user->role()=='admin'):
?>
  <a href="<?php echo '/admin/edit-content/' . $page->key(); ?>">Edit</a>
<?php endif ?>
But this doesn't work :?

My PHP-knowledge is not so good, so it's hard to find the right objects, keys,...
I'm using this print_r($users) for help myself, but can't figured out how check the user-role. See screenshot below, what i try to do...
bludit.dogvalley.be_koksijde_gladiolenlaan.png
bludit.dogvalley.be_koksijde_gladiolenlaan.png (246.36 KiB) Viewed 4371 times
Someone can give me a tip how check if user admin or author is logged in?

PS: I hope there would be more documentation, in the near future, about developing themes, plugins,.. about functions and so on, similar like the WP-docs.

Re: Check User role

Posted: Thu Jul 01, 2021 8:24 pm
by arakel2
Hi ctuxboy,

with print_r($users) you just print out the database of users.

Here viewtopic.php?t=1216 is a thread with the solution for your question.

Just use:

Code: Select all

<?php $Login = new Login();
if ( in_array($Login->role(), array("author","admin",true) )) { echo '<a href="/bludit/admin/edit-content/'.$page->key().'">Edit</a>'; } ?>

Re: Check User role

Posted: Thu Jul 01, 2021 9:49 pm
by ctuxboy
arakel2 wrote: Thu Jul 01, 2021 8:24 pm Hi ctuxboy,

with print_r($users) you just print out the database of users.

Here viewtopic.php?t=1216 is a thread with the solution for your question.

Just use:

Code: Select all

<?php $Login = new Login();
if ( in_array($Login->role(), array("author","admin",true) )) { echo '<a href="/bludit/admin/edit-content/'.$page->key().'">Edit</a>'; } ?>
Hi arakel2,

Wow! Try it and works perfect!
Thanks for your fast an clear answer. Searching the forum, and find this topic, but it was not clearify enough for me, now it works. Happy :P

Oh, okay, the user-database it not what i want :shock:
Have you an idea if there is a list with all the core objects, keys, ... that can be used in templates and plugins?

Regards,
Christophe

Re: Check User role

Posted: Thu Jul 01, 2021 10:46 pm
by Edi
arakel2 wrote: Thu Jul 01, 2021 8:24 pm

Code: Select all

<?php $Login = new Login();
if ( in_array($Login->role(), array("author","admin",true) )) { echo '<a href="/bludit/admin/edit-content/'.$page->key().'">Edit</a>'; } ?>
Without checking user roles you can use

Code: Select all

if ($login->isLogged()
instead of

Code: Select all

if ( in_array($Login->role(), array("author","admin",true) ))

Re: Check User role

Posted: Thu Jul 01, 2021 10:55 pm
by ctuxboy
Edi wrote: Thu Jul 01, 2021 10:46 pm
arakel2 wrote: Thu Jul 01, 2021 8:24 pm

Code: Select all

<?php $Login = new Login();
if ( in_array($Login->role(), array("author","admin",true) )) { echo '<a href="/bludit/admin/edit-content/'.$page->key().'">Edit</a>'; } ?>
Without checking user roles you can use

Code: Select all

if ($login->isLogged()
instead of

Code: Select all

if ( in_array($Login->role(), array("author","admin",true) ))
Hi Edi,

Thanks a lot for explain this more in detail, so i learn step-by-step, working with this awesome CMS :)