Is there a way to add a "class" to this line to make it adhere to the .button styling in my CSS stylesheet?
<?php submit_button(__('Update Profile')); ?>
Thanks
submit_button() is a core admin utility function. It's one of the many elements making up the WordPress admin theme and it's supposed not to be styled, so it will gracefully change when WP core developers decide to change the admin theme.
However, if you really want to style that particular button, here's what I suggest: add a custom attribute to your button, called data-style:
<?php
$attributes = array( 'data-style' => 'custom' );
submit_button ( 'Update Profile', 'primary', 'submit', true, $attributes );
?>
And, in your CSS you can now style your button with:
[data-style="custom"] {
/* style your button here */
}
UPDATE: Trix's answer made me take a closer look at the function referrence and realized ($type) can safely be used to add custom classes to any WP admin button. It's as simple as:
submit_button ( 'Update Profile', 'custom-class' );
Note that (according to the function reference) your button will still have the default button class. This should work:
.button.custom-class {
/* styles here */
}
UPDATE 2:
I've done some more testing and apparently, the function works as advertised. The actual output of
submit_button(__('Update Stuff'), "custom-class");
was:
<p class="submit">
<input type="submit"
name="submit"
id="submit"
class="button custom-class"
value="Update Stuff">
</p>
Most of the rules applying to the buttons in WP admin area are prefixed with .wp-core-ui. In this case, they come from .wp-core-ui .button or .wp-core-ui .button:hover. So the following selectors should work:
.wp-core-ui .button.custom-class {
/* normal state rules here */
}
.wp-core-ui .button.custom-class:hover {
/* hover state rules here */
}
.wp-core-ui .button.custom-class:focus,
.wp-core-ui .button-custom-class:active {
/* active/focus state rules here */
}
For example, adding this to the dashboard CSS changed the appearance of my button, without affecting other buttons:
.wp-core-ui .button.custom-class {
background-color: #272727;
border-color: #666;
color: #ddd;
}
.wp-core-ui .button.custom-class:hover {
background: #212121;
border-color: #666;
color: white;
}
Made it look like this:
Please note that .custom-class rules will be overridden by any rules set with .wp-core-ui .button (the default selector for buttons in WP Admin).
You may simply ,add your class like this:
submit_button( __('Update Profile'), 'my-custom-class' );
<?php submit_button(__('Update Profile', 'button')); ?>
This should work :) You can view the parameters of submit_button() here:
https://developer.wordpress.org/reference/functions/submit_button/
As you can see, the default class should be "primary". I dont know if Wordpress requires that, otherwise I guess you could just try:
<?php submit_button(__('Update Profile', 'button primary')); ?>
Related
I have a Multisite with a main domain (www.valleysgroup.co.uk) and various sub-domains (brickslipsdirect.valleysgroup.co.uk, tradeporcelain.valleysgroup.co.uk etc).
I am wanting to share a custom top bar containing a menu that is not the primary menu of the main domain, from the main domain across all the sub-domains. This menu will be simple links to each of the sub-domains, but in the form of tabs. Tab 1 will lead to brickslipsdirect.valleysgroup.co.uk, tab 2 will lead to tradeporcelain.valleysgroup.co.uk etc.
I have spent several days trying various solutions, which I then came across the plugin: Multisite Shared Menu
This works great, apart from it will only apply the primary menu from the main domain, and no other menu's. Ideally I would like to create a new menu location named Global Top Bar on the main domain and all subs, create my menu named Global Menu on the main domain, and then pull the menu into all the subs.
I am also unsure how to implement this on the main domain and sub-domains as tabs instead of a general horizontal, dropdown or list menu.
Please see image for clarity on what I am looking for looks-wise across the main domain and the subs:
Any help would be greatly appreciated!
Unfortunately I don't have too much time to spend on this, but hopefully you or someone you know can help flesh this out more.
I've added a bunch of comments that should hopefully explain most things. You just need to plug in the IDs for each site as you find in the Multisite backend. This code will kick out an unordered list with one entry per site, and the current site having a special class. You will probably want to add some additional classes but hopefully this is enough to get you somewhere at least.
// Just the Site IDs from WordPress
$siteIds = [2, 3, 4];
// This will hold <li> tags for each
$menuItems = [];
foreach ($siteIds as $siteId) {
// Get the WordPress WP_Site object
$site = get_site($siteId);
// Sanity check that it was found and public
if (!$site || !$site->public) {
continue;
}
// Optional attributes classes on the <li>, default none
$extraAttributes = '';
// For the current site, add a class
if ($siteId === get_current_blog_id()) {
$extraAttributes = ' class="current-site"';
}
// Append an HTML node
$menuItems[] = sprintf(
'<li %3$s>%1$s</li>',
esc_html($site->blogname),
esc_attr($site->siteurl),
$extraAttributes
);
}
// Make sure the above worked
if ($menuItems) {
// Output wrapped
echo '<ul>' . implode('', $menuItems) . '</ul>';
}
I have now gone ahead and added my new menu location named Global Top Bar using the following in my main domains Child Theme > Functions.php file:
function register_new_menu_location() {
register_nav_menu('global-menu-location',__( 'Global Top Bar' ));
}
add_action( 'init', 'register_new_menu_location' );
I have also created my menu named Global Menu and marked for it to display in the newly made location.
I have then gone into my Child Theme > Header.php file and added the following which seem to work:
switch_to_blog(1);
wp_nav_menu( array(
'menu' => '16',
'container_class' => 'GlobalMenuContainerClass',
'menu_class' => 'GlobalMenuClass'
) );
restore_current_blog();
Menu '16' being my menu's ID found on my main domain. This displays the menu on all sub-domains like this:
I Added some CSS to spice it up a little and make it look more like tabs using the following CSS code:
.GlobalMenuContainerClass {
height: 45px;
padding-top: 8px;
padding-right: 100px;
padding-left: 100px;
width: 100%;
margin-right: auto;
margin-left: auto;
background: #cc2325;
}
.GlobalMenuClass {
list-style: none;
margin: 0;
padding: 0;
}
.GlobalMenuClass li a {
text-decoration: none;
float: left;
margin-right: 5px;
color: white;
border: 1px solid #777777;
padding: 5px 10px 5px 10px;
min-width: 100px;
text-align: center;
display: block;
background: #777777;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
.GlobalMenuClass li a:hover:not(.active) {
background: #4e4e4e;
border: 1px solid #4e4e4e;
}
.active {
background: white;
color: black;
border: 1px solid white;
}
And here is how it now looks:
You can see there is a different hover colour for the tabs of a darker grey, but I have not yet implemented the .active tab CSS yet. Not quite sure how I am going to do this yet.
Hope this can help others in a similar situation.
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 created custom menu into my Wordpress site. I registered the new menu into functions.php file using this code:
// Add new Footer menu
function register_my_menu() {
register_nav_menu('new-menu',__( 'New Footer Menu' ));
}
add_action( 'init', 'register_my_menu' );
and after that inserted this line into footer.php file from current theme:
<?wp_nav_menu( array( 'theme_location' => 'new-menu', 'container_class' =>
'new_menu_class' ) ); ?>
and menu is showing into footer, but its showing into list view, and I want to show vertically inline in footer, of course centered is possible. I used CSS to add inline styling like this:
.new_menu_class {
display:inline-flex;
}
But seems do not many any changes to menu in footer. Any help here?
You're applying styles to the container of the wp_nav_menu function where the structure of the returned html is
<div class="new_menu_class"> <ul> <li>...
'container_class'
(string) Class that is applied to the container. Default 'menu-{menu slug}->container'.
https://developer.wordpress.org/reference/functions/wp_nav_menu/
For your CSS to be applied on the list items (I'm assuming you wish to have the horizontally). you will need to add this class under the arguments for 'menu_class'
'menu_class'
(string) CSS class to use for the ul element which forms the menu. Default 'menu'.
If you applied the same class in your question to the menu_class you could apply the following:
.new_menu_class {
display: flex;
align-items: stretch;
justify-content: space-evenly;
width: 80%;
margin: 0 auto;
padding: 0;
}
Looking at your site I can see you have a set width container with media queries, I would suggest the same when applying this so as your UI is clear when viewing on smaller devices.
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;
}