Last active
July 14, 2018 21:11
-
-
Save mojaray2k/5fe9223f300f602c67e9 to your computer and use it in GitHub Desktop.
WordPress Meta Box Conditional Logic and Interaction with Advance Custom Fields
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
/** | |
* Meta Box { Conditional Logic | |
* | |
* @description: hide / show fields based on a "change" field | |
* @created: 12/21/14 | |
*/ | |
add_action('admin_head', 'non_acf_conditional_logic'); | |
if (!function_exists('non_acf_conditional_logic')) { | |
function non_acf_conditional_logic(){ | |
global $current_screen; | |
if('page' == 'post-new.php' || 'post.php') { ?> | |
<!-- code for select boxes --> | |
<script type="text/javascript"> | |
jQuery(document).ready( function($) { | |
$('#acf-group_id').hide(); | |
/** | |
* Adjust visibility of the meta box at startup | |
*/ | |
if($('#hybrid-post-template').val() == 'post-template-portfolio.php') { | |
// show the meta box | |
$('#acf-group_id').show(); | |
} else { | |
// hide your meta box | |
$('#acf-group_id').hide(); | |
} | |
/** | |
* Live adjustment of the meta box visibility | |
*/ | |
$('#hybrid-post-template').live('change', function(){ | |
if($(this).val() == 'post-template-portfolio.php') { | |
// show the meta box | |
$('#acf-group_id').show(); | |
} else { | |
// hide your meta box | |
$('#acf-group_id').hide(); | |
} | |
}); | |
}); | |
</script> | |
<?php }elseif ('page' == 'post-new.php' || 'post.php') { ?> | |
<!-- code for check boxes --> | |
<script type="text/javascript"> | |
jQuery(document).ready( function($) { | |
if ( $('#hybrid-post-template').is(':checked') ) { | |
$("form#adv-settings label[for='myplugin_sectionid-hide']").show(); | |
$('#myplugin_sectionid').show(); | |
} else { | |
$('#myplugin_sectionid').hide(); | |
$("form#adv-settings label[for='myplugin_sectionid-hide']").hide(); | |
} | |
$('#hybrid-post-template').live('change', function(){ | |
if ( $(this).is(':checked') ) { | |
$('#myplugin_sectionid').show(); | |
$("form#adv-settings label[for='myplugin_sectionid-hide']").show(); | |
} else { | |
$('#myplugin_sectionid').hide(); | |
$("form#adv-settings label[for='myplugin_sectionid-hide']").hide(); | |
} | |
}); | |
}); | |
</script> | |
<?php } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment