Wordpress - how to show/hide content in page - php

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?

Related

Separate PHP file for admin and user page

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
}

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!

if else statement in PHP for Magento Site, need to hide on Homepage, checkout and success page

Here's what is currently working with not displaying on the homepage. And displaying on all other pages, need help not displaying on /checkout/cart/ and /onepage/ and onepage/successs.
<?php if(Mage::getBlockSingleton('page/html_header')->getIsHomePage()): ?>
<?php else: ?>
(Div I am trying to display only on pages other than homepage,checkout,success)
<?php endif; ?>
Please try this
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
if($routeName == 'cms' && $identifier == 'home') {
echo 'This is Magento Homepage.';
} else {
echo 'This is not a Magento Homepage.';
}
this should work for home page only .For hide on multiple pages you can create a array of pages and check if route name exist in that
thanks
place below code in your header file.
for home page :
<?php if ($this->getIsHomePage()){echo 'this is home page';}else{echo 'this is not home page';}
now we will take one variable $pageIdentifier for other then home page
<?php $pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName();?> /* this identifier value will change as per page.so echo this variable so you will get page identifier on all pages*/
for cart page :
if($pageIdentifier == 'checkout_cart_index'){echo 'this is cart page';}
for onepage checkout page :
if($pageIdentifier == 'aw_onestepcheckout_index_index'){echo 'this is checkout page';}
for order sucess page :
if($pageIdentifier == 'checkout_onepage_success'){echo 'this is sucess page';}
using this code you can get other pages identity and use as per your need.
Hope this will work.

How to check that if current user (ID) has posts or not wordpress

I have a Custom template. In it i want to check that if the current logged in user has custom posts named (student_form) or not if posts equals true i want to redirect it to some page other wise i want to show him/her a form which i created using wp fronted user.
Following code i wrote but its not redirecting to the desired Page if user has custom posts .
<?php
/*
* Template Name: Form Page
*/
get_header();
global $post,$current_user;
get_currentuserinfo();
if ($post->post_author == $current_user->ID) {
exit( wp_redirect(http://MYurl/dashboard/') );
}
else
{
echo do_shortcode('[wpuf_form id="414"]');
}
wp_reset_query(); // Restore global post data stomped by the_post().
get_footer();
?>

If user_meta is equal to X then show a div

On my site when a user is registering there is an option to pick from 3 values (House, car, boat). Once the user has registered and viewing their profile, I want to show a button on the front-end based on what meta value they have selected when registering. If they have no meta key, then nothing is shown.
Here is my code attempt, but does not work!
<?php global $current_user;
get_currentuserinfo(); //wordpress global variable to fetch logged in user info
$userID = $current_user->ID; //logged in user's ID
$havemeta1 = get_user_meta($userID,'house',true); //stores the value of logged in user's meta data for 'house'
$havemeta2 = get_user_meta($userID,'car',true); //stores the value of logged in user's meta data for 'car'
$havemeta3 = get_user_meta($userID,'boat',true); //stores the value of logged in user's meta data for 'boat'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div class="Button3"></div>
<?php } else { ?>
<div></div>
<?php }?>
In short - The expected result is, if the user has choose House when registering, then show the DIV with the class=Button1 etc etc
Update:
This code above now works!

Categories