I have these two buttons in the header:
Members
Register Now!
If the user is not logged in or registered, these two buttons should be shown. If logged in, these two should not be shown. How can I do that?
You can check with is_user_logged_in() for if user is logged in wp or not
if ( is_user_logged_in() ) {
// your code for logged in user
} else {
// your code for logged out user
}
Add Members and Register button for logged out user you need to used below code
if ( !is_user_logged_in() ) {
Members
Register Now!
}
There are various ways one can do this. The first one I'll mention will be leveraging a WP function, and the second one using only CSS.
Lastly, I'll end the answer with a way to display a button only to logged in users.
Option 1
WordPress has a function to determine whether a current visitor is a logged in user: is_user_logged_in() and resembles the following
function is_user_logged_in() {
$user = wp_get_current_user();
return $user->exists();
}
On can find the function in wp-includes/pluggable.php.
For your particular case, the following might do the work
<?php if ( !is_user_logged_in() ) : ?>
Members
Register Now!
<?php endif; ?>
Option 2
Adding something like the following in the Customize>Additional CSS might also do the work (it worked for me)
.logged-in .button-class {
visibility: hidden !important;
display: none !important;
}
or
.button-class {display: block;}
.logged-in .button-class {display: none;}
Additional
Alternatively, if one wants to display the buttons only when a user is logged in, using CSS, do as follows
.button-class {
visibility: hidden !important;
display: none !important;
}
.logged-in .button-class {
visibility: visible !important;
display: inline-block !important;
}
Related
On woocommerce storefront header, I want to hide the search box for not-logged users, and replace it with 2 yellow buttons: "Login", "Register" (like the stackoverflow landing-page header) that send to my customized login/register urls.
I tried this CSS code that works to hide the search box, but I don't know what to do next:
.site-header .site-search { display: none; }
You can use the following that will add a body class when users are not logged in as follow:
add_filter( 'body_class', 'add_body_class_for_guest' );
function add_body_class_for_guest( $classes ) {
// Only for non logged users
if( ! is_user_logged_in() ) {
$classes[] = 'not-logged';
}
return $classes;
}
Code goes in functions.php file of the active child theme (or active theme).
Then you can use the following CSS rule to hide the storefront search box for non logged users:
.not-logged .site-header .site-search { display: none; }
Tested and works.
i want to hide ads for logged in user (subscribers). im using a plugin called "private" that generates shortcodes. i found the code i should use but dont know how to put it in my single.php. My single post is custom made so i cant use the rich editor.
the one i want to use...
[private role=”subscriber”]Content goes here[/private]
and i want to put this code in the middle position of my short code.
<div id="M589567ScriptRootC925056"></div>
<script src="https://jsc.adskeeper.co.uk/a/n/anime-tv.fun.925056.js" async></script>
You do not have to use [private] short codes to hide divs or ads in your case from logged in users.
Just use WordPress native current_user_can() function to target specific user roles like this in PHP file
if (current_user_can('subscriber')) {
//Hide something
} else {
//Show Something
}
You can do something if you want to hide something from all logged in users
if (is_user_logged_in) {
//Hide something
} else {
//Show Something
}
This code goes into your working php file or active theme function.php
There could be a much easier and versatile way to achieve that.
create a function to have the user role appear as a body class
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
add_filter('body_class','my_class_names');
function my_class_names($classes) {
$classes[] = get_user_role();
return $classes;
}
add a specific CSS class to posts, blocks, pages that you want to filter and declare display: none; or display: block; accordingly
Ex. I want to show a post to editors and authors but hide it to visitors, subscribers and contributors. So I add the class 'post-filter' to the post and add these lines to style.css
.post-filter,
.subscriber .post-filter,
.contributor .post-filter {display: none;}
.editor .post-filter,
.author .post-filter,
.administrator .post-filter {display: block;}
And that's it.
I am using Wordpress and WooCommerce to build a website for my client. For every product page I have set up two groups of links (buttons, secondary menu) to allow user to visit other product category pages. I would like certain links to be automatically a specific color and weight when the user is on a certain product page (like an active state, but without having to first clicked on the link, I guess similar to breadcrumbs to show where the user is in terms of what category/categories the product they're viewing belongs to).
I've been trying to target the post-id and the div of the specific links to make css changes, but it's not working. I would really appreciate any help!
Here is the css I used to target a specific secondary menu link:
#menu-item-1886 > a {
color: #003b59 !important;
font-weight: 600;
}
This works, but of course it made the change to this menu item across all pages. I want it to be only on a specific product page.
I also tried this, which also didn't work:
.body.post-id-766 #menu-item-1886 > a {
color: #003b59 !important;
font-weight: 600;
}
The site is at http://www.hoptri.d-gecko.com
Again, really would appreciate your help with this! Thanks in advance!
You can add with body_class WordPress filter hook and choosen conditional tags some additional classes where you want, like in this example:
add_filter( 'body_class', 'adding_some_custom_body_classes' );
function adding_some_custom_body_classes( $classes ) {
if ( is_product() ) {
$classes[] = 'product-single-pages';
} elseif( is_product() && is_single( array( 17, 19, 1, 11 ) ) {
$classes[] = 'product-single-pages-2';
}
return $classes;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works
So you will be able to use that classes selectors in your CSS rules like (fake example):
body.product-single-pages #menu-item-1886 > a,
body.product-single-pages2 #menu-item-1886 > a { font-weight: 600; }
body.product-single-pages #menu-item-1886 > a { color: green !important; }
body.product-single-pages2 #menu-item-1886 > a { color: red !important; }
WooCommerce inject yet in body classes some specific classes as in this html example:
<body class="product-template-default single single-product postid-19 logged-in woocommerce woocommerce-page right-sidebar woocommerce-active customize-support">
So you can also use this class selectors in your css:
single-product
woocommerce
woocommerce-page
woocommerce-active
For example this way:
body.woocommerce.single-product.woocommerce-active #menu-item-1886 > a {
color: blue !important;
}
References to conditional tags:
Wordpress conditional tags list
WooCommerce conditional tags list
I’m very new to php coding and I’ve been trying very hard to get this to work.
I’m using wedevs project management plugin with buddy press integration. There dev team doesn’t have the time to customize the plugin for me at the moment so I’m looking for a quick fix. I feel I’m so close but for some reason I’m not getting it to work.
I have a “create project” button that I would like to hide for all group members accept the Group admin. so I would like it to verify if the user is the group admin and show the “create project link”. But if it is just a group member or user, I would like the “create project link” to be hidden. Only group admins should be allowed to create projects… Here is what I have so far. But its not working. Any help or guidance would be greatly appreciated.
<?php if ( BP_Groups_Member::get_is_admin_of( $user_id )) { ?>
<style type="text/css" media="screen">
.cpm-projects nav.cpm-new-project a {visibility:visible; }
</style>
<?php } else { ?>
<style type="text/css" media="screen">
.cpm-projects nav.cpm-new-project a {visibility:hidden; }
</style>
<?php } ?>
Keep in mind that the stylesheet for the plugin has this inside the style sheet.
.cpm-projects nav.cpm-new-project a {
background: transparent url("../images/plus.png") no-repeat scroll 50% 50%;
box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3);
height: 80px;
width: 80px;
display: block;
border-radius: 3px;
}
For some reason its still showing for all members with this code.
I’ve tried all of these calls and still nothing
if ( BP_Groups_Member::get_is_admin_of( $user_id )) {
if ( ! groups_is_user_admin( $user_id, $group_id ) ) {
if ( groups_is_user_admin( $user_id, $group_id ) ) {
if ( groups_is_user_admin( get_current_user_id(), $group_id ) ) {
Not sure if it fixes your problem, but a cleaner way is to put the CSS code as two separate classes into your custom CSS file. In other words, you hide the button with CSS for all users by default, but then create a class ".can_create_proj" to show the button for the admin.
In your PHP code, you can add the class name to the button if the current user is admin.
<nav class = "cpm-new-project">
<a clas="<?php check_is_admin( $user_id, $group_id ) ? echo "can_create_proj"; ?>">Create Project</a>
</nav>
Maybe using check_is_admin( $user_id, $group_id ) will work. Ref: https://buddypress.org/support/topic/checking-if-member-is-also-a-group-admin/
In you CSS:
nav.cpm-new-project a {
display:block;
}
.can_create_proj {
display:none;
}
I want to add a background picture to a group of pages in wordpress.
I want to add a different background picture to another group of pages
Currently I am applying it to each individual page using PageID as in the code below. As there are over 1000 pages. Is there are a more simple way to apply to each group of pages?
.page-id-264 #header .logo a,
.page-id-272 #header .logo a {
background-image: url("http://www.richcoward.com/newcges/wp-content/uploads/2014/08/NWU-No-Frame.png") !important;
background-size: 100% !important;
background-repeat: no-repeat !important;
height: 86px !important;
width: 416px !important;
}
Thanks for your help
Here is a simple solution.
Create a custom css (say mystyle.css) file in your theme folder. Add
#header .logo a {
background-image: url("http://www.richcoward.com/newcges/wp-content/uploads/2014/08/NWU-No-Frame.png") !important;
background-size: 100% !important;
background-repeat: no-repeat !important;
height: 86px !important;
width: 416px !important;
}
into your mystyle.css. Now, open up your functions.php and enqueue your stylesheet conditionally. Use is_page() or is_page_template() conditional checks. I believe that you are talking about groups of pages, so I would imagine that you use specific page templates for specific groups, so I would tend to go for is_page_template
function enqueue_custom_style() {
if(is_page_template('page-whatever.php')) {
wp_enqueue_style('my-style', get_stylesheet_directory_uri() . '/mystyle.css' );
}
}
add_action('wp_enqueue_scripts', 'enqueue_custom_style', 999);
This should give you an idea
You could use a filter in your functions.php file to add a class to all the relevant pages. You're still going to have to rattle off each Page ID, but I guess doing it in PHP will keep your CSS smaller (and therefore load faster):
// Add specific CSS class by filter
add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
// array of Page IDs
$pageids = array(264, 272);
// if
if ( is_page( $pageids ) ) {
// add 'class-name' to the $classes array
$classes[] = 'class-name';
// return the $classes array
return $classes;
}
}
Alternatively, create page templates for each background.