Last active
August 29, 2015 14:18
-
-
Save christianwach/291e646c1306a6652a4a to your computer and use it in GitHub Desktop.
CiviCRM Shortcode Button Hook Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
-------------------------------------------------------------------------------- | |
Plugin Name: CiviCRM Shortcode Button Hook Example | |
Plugin URI: https://gist.github.com/christianwach/291e646c1306a6652a4a | |
Description: An example plugin showing how to exclude the CiviCRM shortcode button from a custom post type | |
Author: Christian Wach | |
Version: 0.1 | |
Author URI: http://haystack.co.uk | |
-------------------------------------------------------------------------------- | |
*/ | |
/** | |
* Register CiviCRM hooks on CiviCRM plugin init. | |
* | |
* Adding actions and filters that are specific to CiviCRM in this function means | |
* that they will only ever be added when CiviCRM is present and loaded. | |
* | |
* @return void | |
*/ | |
function csbhe_register_civi_hooks() { | |
// kill CiviCRM shortcode button | |
add_action( 'admin_head', 'csbhe_kill_civi_button' ); | |
} | |
// add actions for plugin init on CiviCRM init | |
add_action( 'civicrm_instance_loaded', 'csbhe_register_civi_hooks' ); | |
/** | |
* Do not load the CiviCRM shortcode button unless we explicitly enable it. | |
* | |
* @return void | |
*/ | |
function csbhe_kill_civi_button() { | |
// sanity check, though this is unlikely ever to be acted upon | |
if ( ! function_exists( 'civi_wp' ) ) return; | |
// get screen object | |
$screen = get_current_screen(); | |
// is this the post type we want to target? | |
if ( $screen->post_type == 'my_cpt_identifier' ) { | |
// get Civi object | |
$civi = civi_wp(); | |
// do we have the modal object? | |
if ( isset( $civi->modal ) AND is_object( $civi->modal ) ) { | |
// remove Civi's button actions in 4.6+ | |
remove_action( 'media_buttons_context', array( $civi->modal, 'add_form_button' ) ); | |
remove_action( 'media_buttons', array( $civi->modal, 'add_form_button' ), 100 ); | |
remove_action( 'admin_enqueue_scripts', array( $civi->modal, 'add_form_button_js' ) ); | |
remove_action( 'admin_footer', array( $civi->modal, 'add_form_button_html' ) ); | |
} else { | |
// remove Civi's button actions pre-4.6 | |
remove_action( 'media_buttons_context', array( $civi, 'add_form_button' ) ); | |
remove_action( 'media_buttons', array( $civi, 'add_form_button' ), 100 ); | |
remove_action( 'admin_enqueue_scripts', array( $civi, 'add_form_button_js' ) ); | |
remove_action( 'admin_footer', array( $civi, 'add_form_button_html' ) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment