How do I make my custom WordPress meta box visible to admins only? - wordpress

I am using my functions.php to add a custom meta box on my posts page in the WordPress Admin Area. However, I need to make it so its only visible to admins, and not editors, contributors, etc.
What would I do to make it visible to admins only?

function your_function() {
global $current_user;
if($current_user->roles[0] == 'administrator') {
add_meta_box(your parameters);
// fill in your parameters
}
}
add_action('admin_init','your_function');

if ( is_user_logged_in() ) {
get_currentuserinfo();
# check if current user is admin
if ( $current_user->wp_user_level >= 10 ) {
# put your admin-only function here
}
}

This snippet works for custom taxonomies. It removes / hides a custom taxonomy meta box for all non-admins, assuming no other role has the update_core capability. Similar, but opposite of the answer by #johannes-pille
function remove_tax_metaboxes() {
if (!current_user_can('update_core')) {
remove_meta_box( 'taxdiv', 'post', 'side' );
}
}
add_action( 'do_meta_boxes', 'remove_tax_metaboxes' );
Note that the third argument of remove_meta_box may differ, see https://codex.wordpress.org/Function_Reference/remove_meta_box

Related

Hide plugin from admin bar & plugin list

I am trying to hide the following item from the following sections:
Admin bar: ID = wp-admin-bar-nitropack-top-menu
Plugin list: data-slug="nitropack"
I have tried these methods, but can not get it to work. Maybe i have the wrong IDs/Slugs?
Methods: https://divi.space/wordpress-and-divi-code-snippets/hide-any-plugin-from-the-wordpress-dashboard/
Would really appreciate some help, since a customer should not be able to change the settings within this plugin!
Best regards,
Isac
The css way
<style>
a[data-slug="nitropack"] { //hides all a href's with that data slug
display:none;
}
</style>
normally if its an wp admin menu you would do something like this:
//remove admin page item
function edit_admin_menus() {
remove_menu_page("index.php"); //Dashboard
remove_menu_page("itsec"); // wp-admin.php?page=itsec use this "itsec"
}
add_action("admin_menu", "edit_admin_menus");
or you need to remove admin bar item
//remove tool bar item
function remove_toolbar_node($wp_admin_bar) {
// replace 'updraft_admin_node' with your node id "nitropack" something
$wp_admin_bar->remove_node("avia");
$wp_admin_bar->remove_node("updates");
$wp_admin_bar->remove_menu("wp-logo");
$wp_admin_bar->remove_menu("themes");
$wp_admin_bar->remove_menu("widgets");
$wp_admin_bar->remove_menu("dashboard");
//$wp_admin_bar->remove_node("updraft_admin_node");
}
add_action("admin_bar_menu", "remove_toolbar_node", 999);
FYI, since you need to block access to the plugin you'll need to add a redirect based on member role. The customer may know the actual url and can still access the page.
//Admin or Editor role check, if else send to alt url
function block_pages_for_user() {
$blocked_pages = is_page('slug');
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if( !current_user_can('administrator') && !current_user_can('editor') && !current_user_can('subscriber') && $blocked_pages ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
add_action( 'wp', 'block_pages_for_user', 8 );

How to set a WooCommerce email template as default for all emails

I’m looking for a way to send all WordPress emails using a custom WooCommerce template so all emails will look the same.
The path to the template would be:
woocommerce/emails/my-custom-woocommerce-template.php
Does it have to all be templatized in a single file? If not, a combination of these entry points can probably get you the standardization you're looking for:
email-header.php lets you customize the start of the email including the header image (if you need to do more than change its URL). It opens the layout tags for the rest of the email content
email-footer.php lets you customize the footer, and closes the layout tags started in the header.
email-styles.php or the woocommerce_email_styles filter let you customize the CSS (see some gotchas in my article here).
Various actions/filters are scattered throughout the emails for customizing individual parts.
You can use the below function. It is working
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
// List of all templates that should be replaced with custom template
$woo_templates = array(
'emails/admin-new-order.php',
'emails/admin-failed-order.php',
'emails/admin-cancelled-order.php',
'emails/customer-completed-order.php',
'emails/customer-new-account.php',
'emails/customer-note.php',
'emails/customer-on-hold-order.php',
'emails/customer-processing-order.php',
'emails/customer-refunded-order.php',
'emails/customer-reset-password.php',
);
//Check whether template is in replacable template array
if( in_array( $template_name, $woo_templates ) ){
// Set your custom template path
$template = your_template_path.'emails/my-custom-woocommerce-template';
}
// Return what we found
return $template;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
add_filter( 'wp_mail', 'your_wp_mail_action' ); // $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
function your_wp_mail_action( $args ) {
global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
$your_prefix_your_email_args = $args;
ob_clean();
get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
$args['message'] = ob_get_clean();
// ... your logic
return $args;
}
To view and update email settings, log into your website dashboard. In the left-hand menu, click on WooCommerce → Settings.
There, you’ll find several options tabs at the top. Click Emails to view the following templates
you can custom all as you want

How to hide advanced custom fields(ACF) in the WP-admin UI?

Check the screenshot below; all I want to do is to hide certain ACF fields for custom users in the wordpress backend.
As of ACF 5.0.0 there is an easier way to do this without having to 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/
If you mean to hide it with CSS, then you should insert custom CSS to admin footer area.
For example, you can add such kind of code to your theme's functions.php file:
add_action('admin_footer', 'my_admin_hide_cf');
function my_admin_hide_cf() {
$u=wp_get_current_user();
$user_roles = $u->roles;
if ($user_roles[0]=='CUSTOM_USER_ROLE_NAME'){
echo '
<style>
#acf-FIELD_SLUG_HERE {display:none}
</style>';
}
}
And of course you should replace FIELD_SLUG_HERE and CUSTOM_USER_ROLE_NAME values with correct ones.
F.e. #acf-FIELD_SLUG_HERE can be #acf-url, CUSTOM_USER_ROLE_NAME can be "contributor".

Remove WordPress admin bar on a single page

Is there a way to remove the WordPress admin bar but just on a specific page?
I know that I can remove it or hide it completely by adding an action:
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
I've done that but on my site I have one page that is displayed in an iframe (using colorbox) and I don't ever want the admin bar to show up in that iframe.
Is there a way to hide the admin bar but just for a specific page?
Thanks,
Ben
Your function is a bit complicated, there's a filter for that. Just check for the Page ID and filter it out using the show_admin_bar filter.
function riga_hide_admin_bar(){
if( $post->ID == YOUR_POST_ID ){
return false;
}
}
add_filter( 'show_admin_bar' , 'riga_hide_admin_bar' );
Remove WordPress admin bar on a single page
Yes, you can remove admin bar from a specific page as you wish.
Use add_filter( 'show_admin_bar', '__return_false' ); to hide admin bar from a specific page.
function my_function_admin_bar(){
return false;
}
add_filter( 'show_admin_bar' , 'my_function_admin_bar');
For more details please see below link:
https://codex.wordpress.org/Plugin_API/Filter_Reference/show_admin_bar
Get ID of specific page and add your filter in condition which meet the page ID
`If(get_the_ID()==#736637){
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
}`
For me the easiest way is to add the code in the template of the page I don't want to show. At the top of the template file I just add:
<?php
show_admin_bar(false);

How to disable Yoast SEO adding article:author meta tags to pages

The excellent Yoast SEO plugin is adding some unwanted meta tags.
For example, I would like article:author to appear on posts, but not on pages or other content types.
Is there a way to adjust this globally?
I'm happy to edit functions.php, but I'm just unsure what I should be hooking in to.
I would be grateful for any pointers from those more familiar with the plugin.
I tried this:
function wpseo_show_article_author_only_on_posts() {
if ( !is_single() ) {
return false;
}
}
add_filter( 'xxxxxx', 'wpseo_show_article_author_only_on_posts' );
I need to know what hook should replace xxxxxx.
You're looking for the wpseo_opengraph_author_facebook filter, which ties into the article_author_facebook() method in frontend/class-opengraph.php of the plugin.
function wpseo_show_article_author_only_on_posts( $facebook ) {
if ( ! is_single() ) {
return false;
}
return $facebook;
}
add_filter( 'wpseo_opengraph_author_facebook', 'wpseo_show_article_author_only_on_posts', 10, 1 );
The article_author_facebook() method does a check for is_singular(), which checks that we're viewing single page, post or attachment:
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
The additional filter for ( ! is_single() ) ensures that article:author is only added to posts.
If people are looking for a way to remove more yoast SEO tags, just lookup the file wordpress-seo/frontend/class-opengraph.php, and you can see which filters you can hook into to remove certain tags.
This is the code I use to remove these tags from pages: url, image, title, description and type:
function remove_yoast_og_tags( $ogTag ){
// Do a check of which type of post you want to remove the tags from
if ( is_page() ) {
return false;
}
return $ogTag;
}
$yoastTags = array(
"url",
"image",
"title",
"desc",
"type"
);
foreach ($yoastTags as $tag) {
add_filter( 'wpseo_opengraph_'.$tag, 'remove_yoast_og_tags');
}

Resources