1

I am using a theme Indigo and have it working fine, in a child theme configuration. I want to add a custom function. Unlike the child style.css, I have read that you don't need to import the parent functions.php. However when I add my own function to a blank file, functions.php in the child theme folder, my page doesn't load correctly and I see text from the child functions.php.

I have seen solutions on here requiring that the parent functions.php has functions that can be rewritten.

What are my options here? I want my customizations, but I don't want a theme update to bork them.

This is what I'm trying to drop in...

/**
 * Disable admin bar on the frontend of your website
 * for subscribers.
 */
function themeblvd_disable_admin_bar() { 
    if( ! current_user_can('edit_posts') )
        add_filter('show_admin_bar', '__return_false'); 
}
add_action( 'after_setup_theme', 'themeblvd_disable_admin_bar' );

/**
 * Redirect back to homepage and not allow access to 
 * WP admin for Subscribers.
 */
function themeblvd_redirect_admin(){
    if ( ! current_user_can( 'edit_posts' ) ){
        wp_redirect( site_url() );
        exit;       
    }
}
add_action( 'admin_init', 'themeblvd_redirect_admin' );

It's supposed to disable the WP admin bar for low level logins.

FYI I have tested them in the parent functions.php

7
  • 4
    May be PHP tag is missing. Add <?php in top of file. May 18, 2015 at 9:18
  • Indeed there were none. But when I added the tags the site loads as a blank page :/ (I put opening and closing in) May 18, 2015 at 9:23
  • Set WP_DEBUG to true in your wp-config.php file. This will tell you exactly what your error is.
    – ngearing
    May 18, 2015 at 9:27
  • That didn't do anything, but I noticed I also had the custom functions in the parent theme functions.php still. Now the page loads OK but a few features of the template don't work :/ For example the social media links are missing, as is the logo and navigation menu. May 18, 2015 at 9:37
  • Seems to be working now. I think adding the php tag and removing the test functions from the parent file fixed it. I just had to add some customisations to the template through the dashboard again as they were lost when I switched to the child theme. May 18, 2015 at 11:11

2 Answers 2

0

I think the problem is with your choice of hook and conditional statement. Try init and remove the !.

function themeblvd_disable_admin_bar() { 
    if( current_user_can('edit_posts') )
        add_filter('show_admin_bar', '__return_false'); 
}
add_action( 'init', 'themeblvd_disable_admin_bar' );

Looks like your conditional is also wrong for the redirect

function themeblvd_redirect_admin(){
    if ( current_user_can( 'edit_posts' ) ){
        wp_redirect( site_url() );
        exit;       
    }
}
add_action( 'admin_init', 'themeblvd_redirect_admin' );

If these snippets work as tested please mark this answer as accepted and up vote using the arrows to the left. Thanks

2
  • Should the ! not stay because I want the admin bar removed if the logged in user can't edit posts. May 27, 2015 at 22:51
  • ! is the the logical operator for Not so you decide. May 28, 2015 at 2:38
0

I used the following to stop subscribers from seeing the Admin Bar and setup a redirect should they try to access the Admin Area.

//remove wp-admin access and remove admin bar from everyone who can't edit posts

function remove_admin_bar() {
    if (!current_user_can('edit_posts') && !is_admin()) {
     add_filter('show_admin_bar', '__return_false');
    }
}
add_action('init', 'remove_admin_bar');

function blockusers_init() {
    if ( is_admin() && ! current_user_can( 'edit_posts' ) && 
    ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
    wp_redirect( home_url() );
    exit;
    }
}
add_action( 'init', 'blockusers_init' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.