Skip to content

Instantly share code, notes, and snippets.

@donnamcmaster
Last active April 6, 2017 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donnamcmaster/7324cba06b375ef35b5b to your computer and use it in GitHub Desktop.
Save donnamcmaster/7324cba06b375ef35b5b to your computer and use it in GitHub Desktop.
WordPress: Make Piklist plugin show metabox only for top-level pages (i.e., post_parent = 0)
<?php
/**
* This code went into the file in piklist/parts/metaboxes
*/
/*
Title: Section Name
Post Type: page
Top pages only: true
*/
/**
* These two functions went into my functions.php
*/
add_filter( 'piklist_get_file_data', 'mcw_custom_comment_block', 10, 2 );
function mcw_custom_comment_block ( $data, $type ) {
// if not a meta box file then bail
if ( $type != 'meta-boxes' ) {
return $data;
}
// allow Piklist to read our custom comment block attributes
$data['top_pages_only'] = 'Top pages only';
return $data;
}
add_filter( 'piklist_add_part', 'mcw_check_custom_comment_block', 10, 2 );
function mcw_check_custom_comment_block ( $data, $type ) {
global $post;
// if not a meta box then bail
if ( $type != 'meta-boxes' ) {
return $data;
}
// check if any custom requirements are set in the comment block
if ( !empty( $data['top_pages_only'] ) ) {
// is this a top-level page?
if ( $post->post_parent ) {
// change meta-box access to user role: no-role
$data['role'] = 'no-role';
}
}
return $data;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment