I am trying to add code to functions.php that Waqar kindly save me a fews days ago to hide toolbar buttons inside custom post editor pages. I added it then and it was successful. Since then I must have updated WP and the code was removed from the file. I am now re-adding it but an error says it has an unexpected something or other and the file gets rolled back. I can't quite understand why it worked before and now it doesn't. Are you able to help please?
Here is the code:
function remove_toolset_buttons(){
// only remove buttons on back end for non-administrators
if ( is_admin() && !current_user_can( 'manage_options' ) ) {
// remove the Fields and Views button
add_filter( 'toolset_editor_add_form_buttons', '__return_false' );
// remove the CRED button
add_filter( 'toolset_cred_button_before_print', '__return_false' );
// remove the Access button for all non-administrator roles
add_filter( 'toolset_editor_add_access_button', function(){
global $wp_roles;
$all_roles = array_keys($wp_roles->roles);
return $all_roles;
});
}
}
add_action( 'init', 'remove_toolset_buttons' );
function remove_content_template_metabox() {
global $WPV_templates;
// only remove "Content Template" metabox for non-administrators
if ( is_admin() && !current_user_can( 'manage_options' ) ) {
remove_action('admin_head', array($WPV_templates,'post_edit_template_options'));
}
}
add_action('admin_menu', 'remove_content_template_metabox');
Link to a page where the issue can be seen:
I expected to see:
Instead, I got:
You should never modify Avada > functions.php, that's a theme file that should never be edited. Otherwise, when you update Avada the code you add will be lost. You should create a child theme and add the code to the child theme's functions.php file, or you can create a new code snippet in Toolset > Settings > Custom Code. Let me know when you have one of those set up and I'll be glad to take a closer look.
https://developer.wordpress.org/themes/advanced-topics/child-themes/
Hi Christian,
I hope you're not sick and tired of me by now!!! I realise now that I should have set up a child theme before I started customising things. I wasn't aware of the custom code option which will be perfect for this site's current parent theme set up. I added a custom code snippet but it is inactive due to an error. It's very odd as it's the same code that worked only a few days ago. How may I give you access again if you need it?
Something is up with the linebreaks and indentation in the code sample you shared above. I copied that into a blank file and recreated the line breaks and indentation. Please copy and paste this updated code:
// Put the code of your snippet below this comment.
function remove_toolset_buttons( ) {
// only remove buttons on back end for non-administrators
if ( is_admin() && !current_user_can( 'manage_options' ) ) {
// remove the Fields and Views button
add_filter( 'toolset_editor_add_form_buttons', '__return_false' );
// remove the CRED button
add_filter( 'toolset_cred_button_before_print', '__return_false' );
// remove the Access button for all non-administrator roles
add_filter( 'toolset_editor_add_access_button', function(){
global $wp_roles;
$all_roles = array_keys($wp_roles->roles);
return $all_roles;
});
}
}
add_action( 'init', 'remove_toolset_buttons' );
function remove_content_template_metabox() {
global $WPV_templates;
// only remove "Content Template" metabox for non-administrators
if ( is_admin() && !current_user_can( 'manage_options' ) ) {
remove_action('admin_head', array($WPV_templates,'post_edit_template_options'));
}
}
add_action('admin_menu', 'remove_content_template_metabox');
Hi Christian,
Thank you for figuring that out. I've added the code and there were no errors but the toolset buttons still appear to editors in custom post edit pages. It's the ones circled in red that disappeared when I originally added the code to functions.php a few days ago. I ticked only wp-admin in the three context options. Is there something else I should do to make it do the same thing as it would if were in that file?
Many thanks,
Barry.
Try increasing the priority of remove_toolset_buttons in the init action hook:
add_action( 'init', 'remove_toolset_buttons', 99 );
Hi Christian,
Once again I'm indebted to you. I don't know how you know so much. It's fixed now so thank you. My issue is resolved now.
Many thanks, Barry.