Skip Navigation

[Resolved] Code previously given to now is no longer accepted in functions file

This thread is resolved. Here is a description of the problem and solution.

Problem: Custom code that should hide Toolset buttons in wp-admin is no longer working as expected.

Solution: Check the linebreak and indentation encoding, there seems to be some issue. Also if you use a custom code snippet in Toolset > Settings, you'll need to increase the priority of the init hook.

This support ticket is created 4 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by barryG-4 4 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1383585
Screenshot 2019-11-14 at 19.57.41.png

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:

#1383603

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/

#1383615

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?

#1383625

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');
#1383631
Screenshot 2019-11-14 at 20.42.37.png

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.

#1383653

Try increasing the priority of remove_toolset_buttons in the init action hook:

add_action( 'init', 'remove_toolset_buttons', 99 );
#1383655

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.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.