Skip to content

Instantly share code, notes, and snippets.

@mattradford
Created September 7, 2016 12:06
Show Gist options
  • Save mattradford/3f1ecbe9d17fcbae94c52895a8462601 to your computer and use it in GitHub Desktop.
Save mattradford/3f1ecbe9d17fcbae94c52895a8462601 to your computer and use it in GitHub Desktop.
Hide ACF flexible content sections from users, rather than delete them
// This hides page builder sections from users
// First add class to the body based on template, then target those classes with deisplay: none
function acf_admin_head_layout() {
?>
<style type="text/css">
/* Remove borders on li since we not removing li's */
.acf-fc-popup li {
border:0 !important;
}
/* WordPress Template "Page Builder"
* - hide ACF layouts named "accordion", etc on template-page-builder.php
*/
.has-template-page-builder .acf-fc-popup a[data-layout="accordion"],
.has-template-page-builder .acf-fc-popup a[data-layout="two_column"],
.has-template-page-builder .acf-fc-popup a[data-layout="two_thirds_third"],
.has-template-page-builder .acf-fc-popup a[data-layout="third_two_thirds"],
.has-template-page-builder .acf-fc-popup a[data-layout="three_column"],
.has-template-page-builder .acf-fc-popup a[data-layout="four_column"],
.has-template-page-builder .acf-fc-popup a[data-layout="gallery"],
.has-template-page-builder .acf-fc-popup a[data-layout="newsletter"] {
display: none;
}
/* WordPress default template
* - hide ACF layout named "block__text-img"
*/
.has-template-default .acf-fc-popup a[data-layout="block__text-img"] {
display: none;
}
</style>
<script type="text/javascript">
(function($) {
$(document).ready(function(){
<?php
global $post;
// Set a javascript variabel "$template_name" based on selected template in WP
// The variable is then used in window.UpdateACFView to add a CSS class
// name to the body-tag. Since standard/custom post types doesn't have
// page templates we need to set this variable on the first page load
if ($post->post_type == "faq") : // set 'post' for standard post
echo 'var $template_name = "post-type/faq";';
else :
// Just get the template name
echo 'var $template_name = $("#page_template").val();';
endif;
?>
// Add classes to body
window.UpdateACFView = function($template_name) {
if ($template_name == "template-page-builder.php") {
$('body').addClass('has-template-page-builder');
}
if ($template_name == "default") {
$('body').addClass('has-template-default');
}
}
window.UpdateACFView($template_name);
});
// When a user change the template in the dropdown we need to remove all our custom classes on the body-tag
// and then, when ajax is completed, trigger window.UpdateACFView in order to set the correct body-class
$(document).on('change', '#page_template', function(){
var $template_name = $('#page_template').val();
$(document).on('ajaxComplete', function(){
$('body').removeClass('has-template-page-builder has-template-default'); // Clear all custom classes
window.UpdateACFView($template_name);
$(document).off('ajaxComplete');
});
});
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_head', 'acf_admin_head_layout');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment