diff --git a/includes/CMB2.php b/includes/CMB2.php index 8d6af3098..e8fe83648 100644 --- a/includes/CMB2.php +++ b/includes/CMB2.php @@ -932,6 +932,11 @@ public function add_field( array $field, $position = 0 ) { return false; } + if ( 'oembed' === $field['type'] ) { + // Initiate oembed Ajax hooks + cmb2_ajax(); + } + $this->_add_field_to_array( $field, $this->meta_box['fields'], diff --git a/includes/CMB2_Ajax.php b/includes/CMB2_Ajax.php index b7618c5c2..41157211c 100644 --- a/includes/CMB2_Ajax.php +++ b/includes/CMB2_Ajax.php @@ -20,6 +20,29 @@ class CMB2_Ajax { protected $object_type = 'post'; protected $ajax_update = false; + /** + * Constructor + * @since 2.2.0 + */ + public function __construct() { + self::hooks( $this ); + } + + /** + * Hook in the oembed ajax handlers + * @since 2.2.0 + * @param CMB2_Ajax $self This object (for hooking) + */ + public static function hooks( $self ) { + static $hooked = false; + + if ( ! $hooked ) { + add_action( 'wp_ajax_cmb2_oembed_handler', array( $self, 'oembed_handler' ) ); + add_action( 'wp_ajax_nopriv_cmb2_oembed_handler', array( $self, 'oembed_handler' ) ); + $hooked = true; + } + } + /** * Handles our oEmbed ajax request * @since 0.9.5 diff --git a/includes/CMB2_hookup.php b/includes/CMB2_hookup.php index 290587e6e..88fb8e1e3 100644 --- a/includes/CMB2_hookup.php +++ b/includes/CMB2_hookup.php @@ -77,10 +77,6 @@ public function __construct( CMB2 $cmb ) { } public function universal_hooks() { - // Handle oembed Ajax - $this->once( 'wp_ajax_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) ); - $this->once( 'wp_ajax_nopriv_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) ); - foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); }