Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added an option to include meta boxes based on post parent.

  • Loading branch information...
commit e57871b9205aeba68d8ead173ff26ac77bfff8e8 1 parent 0137d1e
@zmian authored
Showing with 47 additions and 35 deletions.
  1. +47 −35 demo/better-include.php
View
82 demo/better-include.php
@@ -1,7 +1,7 @@
<?php
/**
* This file show you an improvement of better include meta box in some pages
- * based on post ID, post slug and page template
+ * based on post ID, post slug, page template and page parent
*
* @author Charlie Rosenbury <charlie@40digits.com>
*/
@@ -9,20 +9,23 @@
$prefix = 'rw_';
global $meta_boxes;
+
$meta_boxes = array();
+
$meta_boxes[] = array(
- 'title' => 'Meta Box Title',
- 'fields' => array(
+ 'title' => 'Meta Box Title',
+ 'fields' => array(
array(
- 'name' => 'Your images',
- 'id' => "{$prefix}img",
- 'type' => 'plupload_image'
+ 'name' => 'Your images',
+ 'id' => "{$prefix}img",
+ 'type' => 'plupload_image'
),
),
'only_on' => array(
- 'id' => array( 1, 2 ),
- // 'slug' => array( 'news', 'blog' ),
+ 'id' => array( 1, 2 ),
+ // 'slug' => array( 'news', 'blog' ),
'template' => array( 'fullwidth.php', 'simple.php' ),
+ 'parent' => array( 10 )
),
);
@@ -36,16 +39,14 @@ function rw_register_meta_boxes()
global $meta_boxes;
// Make sure there's no errors when the plugin is deactivated or during upgrade
- if ( !class_exists( 'RW_Meta_Box' ) )
- return;
-
-
- foreach ( $meta_boxes as $meta_box )
- {
- if ( isset( $meta_box['only_on'] ) && !rw_maybe_include( $meta_box['only_on'] ) )
- continue;
-
- new RW_Meta_Box( $meta_box );
+ if ( class_exists( 'RW_Meta_Box' ) ) {
+ foreach ( $meta_boxes as $meta_box ) {
+ if ( isset( $meta_box['only_on'] ) && ! rw_maybe_include( $meta_box['only_on'] ) ) {
+ continue;
+ }
+
+ new RW_Meta_Box( $meta_box );
+ }
}
}
@@ -56,48 +57,59 @@ function rw_register_meta_boxes()
*
* @return bool
*/
-function rw_maybe_include($conditions)
-{
+function rw_maybe_include( $conditions ) {
// Include in back-end only
- if ( !defined( 'WP_ADMIN' ) || !WP_ADMIN )
+ if ( ! defined( 'WP_ADMIN' ) || ! WP_ADMIN ) {
return false;
+ }
// Always include for ajax
- if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
+ if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return true;
+ }
-
- if ( isset( $_GET['post'] ) )
+ if ( isset( $_GET['post'] ) ) {
$post_id = $_GET['post'];
- elseif ( isset( $_POST['post_ID'] ) )
+ }
+ elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = $_POST['post_ID'];
- else
+ }
+ else {
$post_id = false;
+ }
$post_id = (int) $post_id;
+ $post = get_post( $post_id );
- foreach ( $conditions as $cond => $v )
- {
+ foreach ( $conditions as $cond => $v ) {
// Catch non-arrays too
- if ( !is_array( $v ) )
+ if ( ! is_array( $v ) ) {
$v = array( $v );
+ }
- switch ( $cond )
- {
+ switch ( $cond ) {
case 'id':
- if ( in_array( $post_id, $v ) )
+ if ( in_array( $post_id, $v ) ) {
+ return true;
+ }
+ break;
+ case 'parent':
+ $post_parent = $post->post_parent;
+ if ( in_array( $post_parent, $v ) ) {
return true;
+ }
break;
case 'slug':
- $post = get_post( $post_id );
$post_slug = $post->post_name;
- if ( in_array( $post_slug, $v ) )
+ if ( in_array( $post_slug, $v ) ) {
return true;
+ }
break;
case 'template':
$template = get_post_meta( $post_id, '_wp_page_template', true );
- if ( in_array( $template, $v ) )
+ if ( in_array( $template, $v ) ) {
return true;
+ }
break;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.