Show or Hide content user role wise - php

I'm developing a website in wordpress using Woocommerce and WC Vendor Marketplace plugin. I want to show content on certain page user role wise in wordpress website.
For example.
If i'm login as a vendor then it shows content "A" and if i'm login as a customer then it hides content "A".
Here i'm customize my own code for this but not work.
<?php
$userss =get_user_meta( $vendor_id, 'wp_capabilities', true );
if ($userss='vendor') {
echo'<li id="menuuu">Back To Dashboard';
echo'</li>';
}
?>
<?php endif; ?>
In above code 'wp_capabilities' is for user role in wordpress. In my code, user roles are 'vendor' and 'customer'. I'm really weak in php. So please help me.

In wp_capabilities data store as serilized array of user role instead of this you can use current_user_can for more information you can refer to this.
You can change your code as below:
if (current_user_can('vendor')) {
echo'<li id="menuuu">Back To Dashboard';
echo'</li>';
}
?>
<?php endif; ?>

I know this has been marked answered, however using current_user_can() with roles is unreliable and even says as much in the codex.
While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.
It's better to use our built in methods to determine if the current user is a vendor.
<?php if ( WCV_Vendors::is_vendor( $vendor_id ) ) : ?>
<li id="menuuu">Back To Dashboard</li>
<?php endif; ?>

Related

Woocommerce - Alternative h1 title of My Account page for not logged in users

I have set up our bakeries web store in a way that any woo commerce page is only accessible for logged in users. Now I am looking for a way to change the title of h1 on the My Account page in order to display something different for not logged in users. Has anyone an idea how to achieve that? Thanks for your input! Saludos!
Wordpress has a built in function to check if a user is logged in.
<?php
//Built in Wordpress function that checks if the user is signed in
if ( is_user_logged_in() ) {
//If the user is logged in
echo '<h1>Logged in title</h1>';
} else {
//If user is not logged in
echo '<h1>Not logged in title</h1>';
}
?>
You will need to modify the Woocommerce template by overriding it.
Example: To override the admin order notification, copy: woocommerce/templates/emails/admin-new-order.php to yourtheme/woocommerce/emails/admin-new-order.php
----(Update)----
I was wrong… Woocommerce My account is a page. The <h1>page title</h1> is the title of your page, so you will need to change it, in the wordpress template for pages in your theme (Each theme is different) and not in woocommerce templates.
Once you have found this template in your theme folder, you will use a conditional in an if statement around the <h1>page title</h1>:
// When user is on my account page and not logged in
if (is_account_page() && !is_user_logged_in()) {
echo '<h1 class="entry-title">'.__("My custom title", "the_theme_slug").'</h1>'; // My custom title
} else {
the_title( '<h1 class="entry-title">', '</h1>' ); // the normal template title
}
This code is just a closer example, you will need to customize it a bit…

Wordpress: Show custom post type details page

I have created a custom post type named as product. After that I have created a template file to show all products on that page and write the below code :
<?php $loop = new WP_Query( array( 'post_type' => 'acme_product',
'posts_per_page' => 14 ) );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<div>
<?php the_post_thumbnail('thumbnail');
?>
</div>
<div>
<?php the_title( '<h2 class="entry-title"><a href="'.get_permalink().'" title="'.
the_title_attribute( 'echo=0' ).
'" rel="bookmark">', '</a></h2>' );
?>
</div>
</div>
<?php endwhile;
?>
But when I click on the product title link it will not show my product details page. Can anyone help me?
You need to save your template as single-{post_type}.php so in your case it would be single-acme_product.php and make sure that permalink is enable if not than
Login to your WordPress admin.
Go to Settings -> Permalinks. And under Common Settings, let’s use Post name.
Then click ‘Save Changes’
as per your code you don't need to define the post_type here. just use with simple code
<?php while ( have_posts() ) : the_post(); ?>
It sounds as though you've already figured out how to loop through your product type. If you're new to custom post types and are not already aware of it, I would suggest investigating the "archive" template for custom post types (archive-{post_type}.php). I saw another answer that referenced single-{post_type}.php as well; both of these can be identified in the Template Hierarchy documentation in the WP Codex. By default, if you don't provide a customized single template for your post type, it will fall back to single.php (which may require customization if you want it to perform double-duty for multiple post types).
With that in mind, and assuming you're already using single-product.php in your theme (or have otherwise coaxed Wordpress into using your template), it would be helpful to know the symptoms you're experiencing:
Are you getting a 404? This could be a permalink issue, and if you haven't already attempted it you might consider flushing your rewrite rules. This can be accomplished by simply visiting the Settings -> Permalinks panel in the Dashboard.
Is your template being used? Consider adding some HTML comments to your template, and view source on the rendered version to ensure that you're even using the correct file. Failure to load the appropriate template could be due to a filename typo or some other logic that interferes with the normal template selection logic.
If you're getting the correct template, but you're not able to display the correct details, are you doing anything particularly wonky with your single template's loop?
Without clear details on what symptoms you're actually experiencing, it's hard to give you better direction. Hopefully some of this information points you to the correct answer, but if not please share additional details as to what is happening on your end.

If user meta is blank echo something?

I'm trying to add a custom profile avatar to Wordpress. I have the field added to the profile and it saves everything fine, but I'm trying to make a conditional that if the user does not upload a custom file, it defaults to the gravatar.
I have this so far:
<?php if(get_the_author_meta('da_avatar') != ''): ?>
<p>Exists!</p>
<?php else: ?>
<p>Does not exist!</p>
<?php endif; ?>
da_avatar is the ID of the custom field within the profile. This function always yields "DOES NOT EXIST" even if the field is not blank.
Any idea on why it is not working and how I can make it so that if the custom avatar field is empty, it will display "does not exist" and if it is not empty, have it display "Exists!"
I'm guessing it's because of where you're running this code. Try adding the User ID as the second parameter. I've also updated your code slightly to make it easier to use 'da_avatar' once you've performed the check.
My suggestion would be to do the following:
<?php if ( $da_avatar = get_the_author_meta( 'da_avatar', $user_id ) ) : ?>
<p>Exists!</p>
<?php else : ?>
<p>Does not exist!</p>
<?php endif; ?>
Make sure you replace $user_id with a valid user ID. If that works let me know where you're running this code and I'll talk you through setting it up properly.

Joomla! Unable to edit modules PHP codes

I'm quite new to Joomla! as well as PHP.
Currently i'm developing a Joomla! site using Joomla! Version 1.5.14.
I have downloaded and installed VirtueMart 1.1.3 on my site and now i want to edit the registration fields for the VirtueMart Module.
I went to C:\xampp\htdocs\mysite\modules\mod_virtuemart and opened up the mod_virtuemart PHP script.
I only see these codes for the registration part:
<?php endif; ?>
<?php if( $mosConfig_allowUserRegistration == '1' ) : ?>
<tr>
<td colspan="2">
<?php echo $VM_LANG->_('NO_ACCOUNT'); ?>
<a href="<?php $sess->purl( SECUREURL.'index.php?option=com_virtuemart&page=mysite.registration' ); ?>">
<?php echo $VM_LANG->_('CREATE_ACCOUNT'); ?>
</a>
</td>
</tr>
<?php endif; ?>
But i'm unable to find the few lines of codes for the registration fields (For the user to key in).
Examples: Email, company name, title, first name, last name, etc... (with the text boxes beside them)
Hope you get what i mean.
Now i want to add in some more fields for registration, such as 'Position in company', etc..
Can anyone tell me specifically where to find those codes so that i can edit them?
As per my knowledge you should do it from admin panel. Joomla provide custom settings of fields for virtuemart user registration.
You also can refer below link for that.
http://virtuemart.net/documentation/User_Manual/User_Registration_Fields.html
I think this would be helpful to you.
Let me know if anything new you get.
Thanks.

Seeking a User Role Plugin

A client of ours is currently using Rolescoper as a WP plugin to manage users' access to various "private" pages and posts. However, Rolescoper hides posts/pages from view unless the user is logged in. We're looking for a plugin that shows all the posts/pages but shows a "access denied" message with a prompt to log-in afterwards.
Here's a list of the requests verbatim:
Create User Account: username/password
Assign user to a page that is private
I want the private page to still appear in the navigation, even though a user may not be logged in.
Once a private page is clicked, they are prompted to enter their username/password. Once they do, they are then redirected to that page.
This would also need to be applied to document uploads
Any ideas? I did some Googling/WP plugin searching without much luck.
Thanks!
Maybe rather than using a plugin, try reworking your template to use is_user_logged_in?
I know this is a very old question, but for anyone else who finds it, I'll take a swing.
You could add to the beginning of your while statement in your single.php file a "Private" category check and "User Logged In" check, like so (NOTE: redirects to login page if user not logged in and category on post is set to "private"):
while ( have_posts() ) : the_post();
if(in_category("private")){
if(is_user_logged_in()){
get_template_part( 'content', get_post_format() );
} else {
auth_redirect();
}
} else {
get_template_part( 'content', get_post_format() );
}
Is User Logged In?
In Category X?
See also: auth_redirect()
Hope this helps!

Categories