Separate PHP file for admin and user page - php

I am a new PHP user, currently i am developing an online food order website for a school project. Basically there will be 2 roles, admin and normal user. I have two pages in my site, home and order food page. Both users will see the same home page, but the content in the order food page would be different for both roles.
My question is, is it necessary to create new files to take care of admin, or, can i just handle it in the same file?
Really appreciate your help!

Create session and store value in session.
session_start();
//For admin
$_SESSION['type'] = 'admin';
//For user
$_SESSION['type'] = 'user';
//if admin login we will check session and display admin content
if($_SESSION['type']=='admin'){
echo 'Admin content';
}
//if user login we will check session and display user content
if($_SESSION['type']=='user'){
echo 'User content';
}

Manage you content on basis of user role
You can do it like this...
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
$roles = $current_user->roles;
if (in_array('administrator',$roles) == 1){
//content for admin
}else{
//content for user
}

Related

How can I edit this code so that it only changes the Top Menu bar on my homepage when a user is logged in without changing the Main Menu?

I am using OceanWP theme to create my site.
I have two different menus on my homepage, I have a top menu and I also have a main menu. http://prntscr.com/pofn5r
I would like for the top menu to display different options for users who are logged in and users who are logged out.
I have used the following code which I placed in the functions.php file. I have also created two different menus for logged in and logged out users:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged_in';
} else {
$args['menu'] = 'logged_out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
It seems to work in terms of showing different menus for users which are logged in and users which are logged out but the problem is that it also changes the main menu as well as the top menu.
prntscr.com/podv5e
I wanted the main menu to remain the same and just have the top menu change.
I was wandering whether there was a way I could adjust the code so that it would only apply to the top menu and not the main menu?
You could do something like:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
echo "Here you put HTML code when logged in";
} else {
echo "Here you put HTML code when logged out"
}
}
since echo will work like it is writted directly into HTML

show recently viewed products for non-logged in users wordpress

I'm showing recently viewed products for logged in users with below code, of course before this I'm updating user meta in wp_footer action:
$rv = get_user_meta(get_current_user_id(), 'recently_viewed', true);
But I need to show recently viewed products even when user is not logged in. Is there any way to do it with get_option()/update_option() or in some other way?
I decided to show recently viewed product list for non-logged in users with cookies. I added below code in my functions.php file and in single product page I'm getting ID values from cookie and using get_post() function to show information:
function rv_products_non_logged_in(){
$rv_posts = array();
if ( is_singular('product-items') && !is_user_logged_in()){
if(isset($_COOKIE['rv_products']) && $_COOKIE['rv_products']!=''){
$rv_posts = unserialize($_COOKIE['rv_products']);
if (! is_array($rv_posts)) {
$rv_posts = array(get_the_ID());
}else{
$rv_posts = array_diff($rv_posts, array(get_the_ID()));
array_unshift($rv_posts,get_the_ID());
}
}else{
$rv_posts = array(get_the_ID());
}
setcookie( 'rv_products', serialize($rv_posts) ,time() + ( DAY_IN_SECONDS * 31 ),'/');
}
}
add_action('template_redirect', 'rv_products_non_logged_in');
I hope this will help someone else!

Wordpress - how to show/hide content in page

In Wordpress page text and other elements when added from admin, hidden for guests. When user is logged, private content showed for this user. This function created with plugin wp-private. I need get balance for user from other site api, if balance < n , should hide all content for logged users and showing other content . If balance > n , should show all content for logged users. For this I used this code:
wp-content/themes/currentTheme/page.php:
<?php if(is_page("id_page")):
$user_id = get_current_user_id();
$balance = get_balance($user_id);
if($balance <= 0){
remove_post_type_support( 'post', 'editor' );
echo "<a href='pay.php'>Pay</a>";
}
endif;
?>
After that still leaves the content page. What to do to not show page content?

How can I detect if user has subscribed Memberpress product already

How can I detect if user has already bought a memberpress product.
I'm searching something like this:
if(have_subscription){
//code to add button
}else{
// code to add something else
}
This should be pretty straight forward using MemberPress' built in capabilities:
if(current_user_can('memberpress_authorized')) {
// Do authorized only stuff here
}
else {
// Do unauthorized stuff here
}
MemberPress also adds capabilities for each MemberPress Membership so you could also do something like this:
if(current_user_can('memberpress_product_authorized_123')) {
// Do authorized only stuff here
}
else {
// Do unauthorized stuff here
}
In the second example the number 123 is the id of the MemberPress membership.
The answer from 2015 didn't work for me but is a top search result. I thought I should share the result of my search here for others in the future.
Also, I think that "product_authorized" capability only checked if a purchase was made, not verifying the expiration date.
So here is how MemberPress determines if active, inactive, or none:
$mepr_user = new MeprUser( $user_id );
if( $mepr_user->is_active() ) {
// Active
}else if($mepr_user->has_expired()) {
// Expired
}else {
// Never a member
}
has_expired() can return true even if the user is active from a separate transaction so don't rely on that alone.
If you need to check a specific membership you can use the following:
$user_id = 123;
$membership_id = 5000;
$mepr_user = new MeprUser( $user_id );
$mepr_user->is_already_subscribed_to( $membership_id ); // true or false
// OR
$mepr_user->is_active_on_membership( $membership_id ); // true or false
is_already_subscribed_to() accepts only a product id
is_active_on_membership() accepts any of: product id, MeprProduct, MeprTransaction, or MeprSubscription
You can also get all of a user's active subscriptions with:
$mepr_user->active_product_subscriptions( 'ids' ); // return array of ids
Open your WordPress website and:
Select Plugins > Add New
Click on Upload Plugin > Choose file
Select the MemberPress plugin from your saved files
Click Install Plugin > Activate
You will now find MemberPress has been added to your WordPress dashboard.
This should help.

Display conditionally php content depending on user role

I have a line that prints the contact information of a user in his profile. Only logged in user can see the contact information.
<?php
// Contact Info
?>
I have 2 user roles - supervisor, manager
What I am trying to achieve is that if the logged in user is a supervisor then he can see his own contact info, but he cannot see manager's contact.
But If the logged in user is a manager then he can see his own contact info, but he can also see supervisor's contact info.
I have been trying to use the standard if current_user_is function, but this will only display one or the other.
<?php global $current_user;
get_currentuserinfo();
if(current_user_is("manager"))
echo 'Contact Info Here';
else if(current_user_is("supervisor"))
echo 'Contact Info Here';
else if(current_user_is_not("manager"))
echo 'Restricted contact, must be a manager';
?>
I just can't figure out how to make this work, so it displays conditionally.
I also have these rules in the global rule, not sure how relevant it is :
$store_user = get_userdata( get_query_var( 'author' ) );
$store_info = get_store_info( $store_user->ID );
$user_meta = get_user_meta($store_user->ID);
From the question, it looks like you'll need to call the get_users function and pass the "supervisor" role to get users of that type and display their contact info to the "manager" role.
As you've written it, the contact info being displayed is for the current user. You'll need to grab another role's contact info if that's what you want to display.
Untested but this may be close...
//if current user is the manager...
$supervisors = get_users( 'role=supervisor' );
foreach ($supervisors as $supervisor) {
echo '<span>' . esc_html( $supervisor->user_email ) . '</span>';
}
I'm nothing more than a beginner with php but using 'OR' may help you aggregate roles' conditions on a single piece of content:
<?php global $current_user;
get_currentuserinfo();
if(current_user_is("manager") OR current_user_is("supervisor"))
echo 'Contact Info Here';
else()
echo 'Restricted contact, must be a manager';
?>

Categories