[SOLVED] Check User role

Post Reply
ctuxboy
Ssr. Bludit
Posts: 12
Joined: Sat Dec 15, 2018 11:37 pm

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 450 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.
Last edited by ctuxboy on Thu Jul 01, 2021 9:50 pm, edited 1 time in total.
User avatar
arakel2
Ssr. Bludit
Posts: 22
Joined: Sun Mar 28, 2021 11:14 am

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>'; } ?>
ctuxboy
Ssr. Bludit
Posts: 12
Joined: Sat Dec 15, 2018 11:37 pm

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
User avatar
Edi
Site Admin
Posts: 2467
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

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) ))
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
ctuxboy
Ssr. Bludit
Posts: 12
Joined: Sat Dec 15, 2018 11:37 pm

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 :)
Post Reply