Created
July 24, 2012 12:58
-
-
Save miziomon/3169805 to your computer and use it in GitHub Desktop.
WordPress custom metabox implementation
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
<?php | |
/** | |
* parent metabox implementation | |
*/ | |
// hook | |
add_action( 'add_meta_boxes', 'CustomMetabox_setup' ); | |
/** | |
* callback from hook | |
*/ | |
function CustomMetabox_setup() { | |
add_meta_box( | |
'customer_parent', //Id Metabox | |
'MetaBox Title', //Title MetaBox | |
'customer_parent_metabox', //Callback Output function | |
'page', //(post,page,custom post type) | |
'side', //admin area | |
'high' //priority | |
); | |
} | |
/** | |
* metabox render function | |
*/ | |
function customer_parent_metabox(){ | |
global $post; | |
$out = ""; | |
$args = array( | |
'post_type' => 'forum', | |
'post_status' => 'publish', | |
'orderby' => 'title', | |
'order' => 'ASC', | |
); | |
$customer = get_transient( 'customer_parent_metabox' ); | |
if ( false == $customer ) { | |
$customer = get_posts( $args ); | |
set_transient( 'customer_parent_metabox', $customer ); | |
} | |
$out .= "<p><i>Seleziona l'azienda da associare</i></p>"; | |
$out .= wp_combo_posts("parent_id", $customer , $post->post_parent , "full_width"); | |
$out .= "<p><a href='post.php?post=" . $post->post_parent . "&action=edit'>vai alla scheda di dettaglio</a></p>"; | |
echo $out; | |
} | |
/** | |
* utils - generate select | |
*/ | |
function wp_combo_posts($id, $data, $selected = '' , $class='') { | |
$options = ""; | |
$options .= "<option value=''></option>"; | |
foreach ($data as $post) { | |
$selected_attribute = ( $post->ID == $selected ? " selected " : "" ); | |
// applico il filtro per gestione multilingua | |
$title = apply_filters('the_title', $post->post_title); | |
$options .= "<option " . $selected_attribute . " value='" . $post->ID . "'>" . $title . "</option>"; | |
} | |
return "<select id='$id' name='$id' class='$class' >$options</select>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment