Skip to content

Instantly share code, notes, and snippets.

@hampusn
Last active August 29, 2015 14:16
Show Gist options
  • Save hampusn/df8d1d5e530538f706e5 to your computer and use it in GitHub Desktop.
Save hampusn/df8d1d5e530538f706e5 to your computer and use it in GitHub Desktop.
Fix for ACF + WP Media Folder (ACF admin stops working due to javascript error in WP Media Folder)
<?php
/**
* Fixes issue where JS is halted on ACF admin pages when WP Media Folder is activated.
*
* These are the versions of the plugins I had when I tried
* - WP Media Folder - 1.0.4
* - Advanced Custom Fields Pro - (5.1.8, 5.2.1)
*
*
* NOTES
* -----
*
* WP Media Folder registers with the handle 'script' which is bad practice since the handle is
* really generic and can easily be overwritten/used by other plugins or themes as well.
*
* http://codex.wordpress.org/Function_Reference/wp_register_script#Parameters
*
* WP Media Folder should also have a check that window.wp.Uploader (js) exists before trying to access it.
*
* @return void
**/
function hampusn_admin_enqueue_scripts() {
global $typenow;
$wp_media_folder_is_hooked = has_action( 'admin_enqueue_scripts', 'wpmf_load_custom_wp_admin_script' );
$is_acf_admin_page = ( ! empty($typenow) && $typenow === 'acf-field-group' );
if ($wp_media_folder_is_hooked && $is_acf_admin_page ) {
global $wp_scripts;
// Make sure the handle is actually registered and has an src.
// The handle 'script' is used by WP Media Folder (1.0.4).
if ( isset( $wp_scripts->registered[ 'script' ]->src ) ) {
$script_src = $wp_scripts->registered[ 'script' ]->src;
// We should only dequeue the script if it seems to be belonging to WP Media Folder.
// These checks are made to prevent any errors if WP Media Folder actually changes
// their handle for the script when registering it.
if ( strpos( $script_src, 'wp-media-folder/script.js' ) !== false ) {
wp_dequeue_script( 'script' );
}
}
}
}
add_action( 'admin_enqueue_scripts', 'hampusn_admin_enqueue_scripts', 999 ); // 999 might be a bit overkill...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment