2

I currently have a custom CSS file specified in Settings > Resource URLS. In this CSS file I override some civicrm.css styles to make the CiviCRM admin screens look a little more like the Wordpress admin area. However, I would like to disable this file when viewing frontend pages e.g. contribution pages.

I've tried the basic usage of the Wordpress wp_dequeue_script option but I can't find what the handle is for this file, so this doesn't work.

Note: I have successfully disabled the standard civicrm.css on my front end pages with this code in functions.php suggested by another user:

function rs_remove_civicrm_styles() {

  global $civicrm_root;

  if (empty($civicrm_root)) {
   return; 
  }

  $civicrm_css = CRM_Core_Resources::singleton()->getUrl('civicrm', 'css/civicrm.css', TRUE);
  CRM_Core_Region::instance('html-header')->update($civicrm_css, array('disabled' => TRUE));

}

But I'm not sure same method can be used for the custom css file?

2

It might be simpler to just tweak the css to apply only to the admin pages using body.wp-admin (there may be more specific CiviCRM or WP classes you can use).

  • Thanks. I've already done that now, but it being a large project I'm keen to cut down on the nmber of resources files loaded, and as I'm copying most of the civi front end pages I need and styling them from my main wp stylesheet I really would like to prevent my custom css from being loaded – redactuk Jan 12 '16 at 23:29
  • Ah, I get you. I'm not on a computer at the moment, so can't try this out, but I think the handle for de-enqueuing the custom css is just civicrm_custom_css – liblogger Jan 13 '16 at 21:45
  • I've tried again with various combinations of both wp_dequeue_style and wp_dequeue_script but nothing seems to work. My custom css is located at /wp-content/plugins/files/civicrm/custom/css/custom.css, but I'm not sure what exact dequeue should be i.e. by a handle(?) or path to that file, or even if dequue works at all. If you have 5 mins to test this sometime and post code I would be grateful. – redactuk Jan 14 '16 at 14:06
  • Hm - I'm not having any joy with either dequeueing or trying to find the equivalent method for disabling it as you've disabled the default stylesheet... I will have a further look at how CiviCRM is hooked into wp_head :/ – liblogger Jan 18 '16 at 15:40
  • How do you feel about disabling all the css in CiviCRM and running the CiviCRM css through your WP theme (or a WP plugin)? – liblogger Jan 18 '16 at 15:45
1

Just to clarify, the block of code to drop into your functions.php to disable civiCRM CSS on the front-end only looks like this:

// Remove the CiviCRM styles from front-end displays.
function my_remove_civicrm_styles() {
    if (!is_admin()) {
        global $civicrm_root;
        if (empty($civicrm_root)) {
            return;
        }
        $civicrm_css = CRM_Core_Resources::singleton()->getUrl('civicrm', 'css/civicrm.css', TRUE); CRM_Core_Region::instance('html-header')->update($civicrm_css, array('disabled' => TRUE));
    }
}
add_action( 'wp_print_styles', 'my_remove_civicrm_styles', 100 );
0

Just an improvement for @jeff-van-loben-sels's answer (which saved my day). I've just added a condition on CRM_Core_Resources to prevent a fatal error.

function my_remove_civi_style() {
  if (!is_admin()) {
    global $civicrm_root;
    if (!class_exists('CRM_Core_Resources') || empty($civicrm_root)){
        return;
    }
    $civicrm_css = CRM_Core_Resources::singleton()->getUrl('civicrm', 'css/civicrm.css', TRUE); CRM_Core_Region::instance('html-header')->update($civicrm_css, array('disabled' => TRUE));
  }
}
add_action( 'wp_print_styles', 'my_remove_civi_style', 100 );
0

CiviCRM Admin Utilities version 0.4.1 introduces options on the plugin's Settings screen to disable (on the front-end of your website):

  • the default CiviCRM stylesheet
  • the CiviCRM menu stylesheet

And, when the Shoreditch extension is enabled, you can also disable:

  • the Shoreditch custom stylesheet
  • the Shoreditch Bootstrap stylesheet

For reference, the following code will disable any custom stylesheet defined in CiviCRM when not in WordPress admin:

add_action( 'wp_head', 'my_disable_resources', 9 );
function my_disable_resources() {

    // front-end only please
    if ( is_admin() ) return;

    // bail if CiviCRM isn't present and inited
    if ( ! function_exists( 'civi_wp' ) ) return;
    if ( ! civi_wp()->initialize() ) return;

    // get CiviCRM config
    $config = CRM_Core_Config::singleton();

    // bail if there's no custom CSS file
    if ( empty( $config->customCSSURL ) ) return;

    // get registered URL
    $url = CRM_Core_Resources::singleton()->addCacheCode( $config->customCSSURL );

    // get registration data from region
    $registration = CRM_Core_Region::instance('html-header')->get( $url );

    // bail if not registered
    if ( empty ( $registration ) ) return;

    // set to disabled
    CRM_Core_Region::instance('html-header')->update( $url, array( 'disabled' => TRUE ) );

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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