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.
Related
There's any way to hide these elements?
I only want to keep "Site Title" and "Tagline" two fields in General Settings page.
like that
There is no filter to hide disable the fields, you can hide the fields using css but they are still there. you can add this to your themes function.php
add_action('admin_head', 'css_general_options');
function css_general_options() {
echo '<style>
/* Hide all Fields on the General Options page */
.options-general-php .form-table tr {
display:none;
}
/* Display the 1st and 2nd row on the General Options page */
.options-general-php .form-table tr:nth-of-type(1),
.options-general-php .form-table tr:nth-of-type(2){
display:table-row;
}</style>';
}
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 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've used ACF's "Flexible Content" to create a fairly advanced "page builder" where authors can create a "section" (basically just a wrapping element with a CSS class name and ID) and then add all my flexible content (images, wysiwyg etc) to it.
What I'd like to do is hide some fields for non admins. I don't want any old editor to be able to go in and change a section's ID or class names (as it would mess up the layout).
I'm aware of the "Rules" panel in the ACF admin where you can choose to only display a certain field group to one type of user role, but what I want is that same thing but for individual fields.
It doesn't appear to be doable from the admin interface, but I'm wondering if someone knows how it could be done from my functions.php file? Perhaps some filter or action I can hook into and disable certain fields based on the current user's role?
I've attached two screenshots showing what I'd like hidden:
I'd like to hide these choices from the "Add row" menu:
And I'd like these panels to be invisible to non admins:
Edit: While we're at it, I wouldn't mind hiding individual fields from a repeatable too. You'll notice a "Modifiers" field in the first screenshot, that too would be nice to hide from non admins. I guess the solution would be pretty much the same for both problems?
As of ACF 5.0.0 there is an easier way to do this without having to disable the field or output CSS. If you use the acf/prepare_field hook and return false the field will not render.
<?php
function so37111468_hide_field( $field ) {
// hide the field if the current user is not able to save options within the admin
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
return $field;
}
add_filter( 'acf/prepare_field/key=MYFIELDKEY', 'so37111468_hide_field' );
?>
The documentation for that filter can be found here: https://www.advancedcustomfields.com/resources/acf-prepare_field/
I haven't managed to actually hide the fields, but I have managed to disable them. Unfortunately simply setting them to disabled in the acf/load_field action wasn't enough to remove them from the dropdown menu so I also added some CSS to the admin page to visually hide them at least. This is good enough seeing as the editors of the site won't exactly do their best to break it.
<?php
/**
* Hide some "ACF Section" related custom fields
*/
add_action('acf/load_field', 'sleek_hide_acf_section_fields', 10, 1);
function sleek_hide_acf_section_fields ($field) {
$hide = array('section_name', 'section_modifiers', 'modifiers');
global $current_user;
if ((isset($field['_name']) and in_array($field['_name'], $hide)) and (is_admin() && is_user_logged_in() && !in_array('administrator', $current_user->roles))) {
$field['disabled'] = true;
}
return $field;
}
add_action('admin_head', 'sleek_hide_acf_section_fields_css');
function sleek_hide_acf_section_fields_css () {
$hide = array('section_name', 'section_modifiers', 'modifiers');
global $current_user;
if (is_admin() && is_user_logged_in() && !in_array('administrator', $current_user->roles)) {
echo '<style>';
foreach ($hide as $h) {
echo 'div.acf-fc-popup a[data-layout="' . $h . '"]{display: none}';
}
echo '</style>';
}
}
I've got a PHP code block that will assign a heading a different style depending on the main page title. However, my site has multiple sub-menu items for each main page as well. The code currently works in assigning new styles to the main navigation pages but isn't assigning the colour profiles to the sub-page style elements. How can I expand my solution to accommodate any new sub-pages (i.e. a wordpress/php function that gets the sub-pages of the main navigation item).
Here's my code:
if(get_the_title($ID) === "Services") {
echo '<style type="text/css">
.header-page h1 {
background-color: #9B87A9;
}
.header-page h2 {
background-color: #9B87A9;
}
</style>';
}
else if(get_the_title($ID) === "About") {
echo '<style type="text/css">
.header-page h1 {
background-color: #dea949;
}
.header-page h2 {
background-color: #dea949;
}
</style>';
}
So I'm looking for something similar to get_the_title($ID) to accommodate any of Services sub-menu items or About. I want a more automated solution rather than hard code each sub-page title into my if statement.
Instead of adding the css to the page try adding the class to the element.