Skip to content

Instantly share code, notes, and snippets.

@christianwach
Last active November 23, 2016 19:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianwach/b39ce223fec1484e9937 to your computer and use it in GitHub Desktop.
Save christianwach/b39ce223fec1484e9937 to your computer and use it in GitHub Desktop.
Basic CiviCRM Modification
<?php /*
--------------------------------------------------------------------------------
Plugin Name: CiviCRM Modifier
Plugin URI: http://haystack.co.uk
Description: Custom code to modify CiviCRM's behaviour in a multisite context
Author: Christian Wach
Version: 0.1
Author URI: http://haystack.co.uk
--------------------------------------------------------------------------------
*/
/**
* Class definition
*/
class CiviCRM_Modifier {
/**
* Initialises this object
*/
function __construct() {
// late load Civi so we can prevent it if we want to
// NOTE: this may need to go in wp-config.php - here may be too late
if ( ! defined( 'CIVICRM_LATE_LOAD' ) ) {
define( 'CIVICRM_LATE_LOAD', 9 );
}
// add actions for plugin init on CiviCRM init
add_action( 'civicrm_instance_loaded', array( $this, 'register_civi_hooks' ) );
// hooked in with a lower priority so it runs before the CiviCRM action is fired
add_action( 'plugins_loaded', array( $this, 'civicrm_only_on_main_site_please' ), 8 );
}
/**
* Register hooks on CiviCRM plugin init
*
* @return void
*/
public function register_civi_hooks() {
// kill CiviCRM shortcode button
add_action( 'admin_head', array( $this, 'kill_civi_button' ) );
}
/**
* Do not load the CiviCRM shortcode button unless we explicitly enable it
*
* @return void
*/
public function kill_civi_button() {
// get screen
$screen = get_current_screen();
// is this a post type we want to allow the button on?
if ( $screen->post_type != 'page' ) {
// remove Civi's actions from all but pages
remove_action( 'media_buttons_context', array( civi_wp(), 'add_form_button' ) );
remove_action( 'media_buttons', array( civi_wp(), 'add_form_button' ), 100 );
remove_action( 'admin_enqueue_scripts', array( civi_wp(), 'add_form_button_js' ) );
remove_action( 'admin_footer', array( civi_wp(), 'add_form_button_html' ) );
}
}
/**
* Do not load the CiviCRM on sites other than the main site
*
* @return void
*/
public function civicrm_only_on_main_site_please() {
// if not on main site
if ( is_multisite() AND ! is_main_site() ) {
// Uncomment the following to prevent Civi from loading at all
//remove_action( 'plugins_loaded', 'civi_wp', 9 );
// unhook menu item, but allow Civi to load
remove_action( 'admin_menu', array( civi_wp(), 'add_menu_items' ) );
}
}
} // class ends
// init plugin
global $civicrm_modifier;
$civicrm_modifier = new CiviCRM_Modifier;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment