Skip to content

Instantly share code, notes, and snippets.

@mathiasschopmans
Created October 5, 2010 15:33
Show Gist options
  • Save mathiasschopmans/611738 to your computer and use it in GitHub Desktop.
Save mathiasschopmans/611738 to your computer and use it in GitHub Desktop.
<?
/*
Plugin Name: Meta-Boxes
Plugin URI:
Description: Bilder, Fotos, Beiträge, etc.
Version: 2.0
Author: Rheinschafe - Mathias Schopmans
/* ----------------------------------------------*/
$new_meta_boxes =
array(
"fotos" => array(
"name" => "_fotos",
"std" => "",
"title" => "Fotos",
"description" => "URL zu weiteren Fotos",
"outputtitle" => "Entdecken Sie weitere Fotos"),
"beitraege" => array(
"name" => "_beitraege",
"std" => "",
"title" => "Beiträge",
"description" => "URL zu weiteren Beiträgen",
"outputtitle" => "Ähnliche Artikel zu diesem Beitrag"),
"cd" => array(
"name" => "_cd",
"std" => "",
"title" => "CD",
"description" => "URL wo man die CD kaufen / hören kann",
"outputtitle" => "CD anhören oder kaufen"),
"videos" => array(
"name" => "_videos",
"std" => "",
"title" => "Videos",
"description" => "URL zu weiteren Videos",
"outputtitle" => "Videos zum Beitrag"),
"nachrichten" => array(
"name" => "_nachrichten",
"std" => "",
"title" => "Nachrichten",
"description" => "URL zu weiteren Nachrichten",
"outputtitle" => "Nachrichten zum Beitrag")
);
function new_meta_boxes() {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'
<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />
';
echo'
<h2 style="margin-bottom: -10px; margin-top: 10px;">'.$meta_box['title'].'</h2>
';
echo'
<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="25" />
<br />
';
echo'
<label style="margin-top: 5px; display: block;" for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label>
';
}
}
function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'Infobox-Links', 'new_meta_boxes', 'post', 'normal', 'high' );
}
}
function save_postdata( $post_id ) {
global $post, $new_meta_boxes;
if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) OR (defined('DOING_AJAX') && DOING_AJAX) OR (defined('DOING_CRON') && DOING_CRON) ){
return $post_id;
}
foreach($new_meta_boxes as $meta_box) {
// Verify
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id )) return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id )) return $post_id;
}
$data = $_POST[$meta_box['name'].'_value'];
if(get_post_meta($post_id, $meta_box['name'].'_value') == "" && $data != "")
add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true) && $data != "")
update_post_meta($post_id, $meta_box['name'].'_value', $data);
elseif($data == "")
//delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
delete_post_meta($post_id, $meta_box['name'].'_value');
}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
function dp_list_meta($alternative_class='article-links'){
global $new_meta_boxes;
$out = array();
$custom_fields = get_post_custom();
foreach($new_meta_boxes as $meta_box) {
$val = $custom_fields[$meta_box['name'].'_value'][0];
if (isset($val)){
$external = strpos($val, get_bloginfo('url'));
$additional = $external === false ? ' target="_blank" rel="nofollow"' : '';
array_push($out,'<li class="'.$meta_box['name'].'"><a href="'.$val.'" title="'.$meta_box['outputtitle'].'"'.$additional.'>'.$meta_box['title'].'</a></li>');
}
}
if(isset($out[0])){
echo "\n<ul class=\"".$alternative_class."\">\n";
foreach($out as $li) {
echo "\t".$li."\n";
}
echo "</ul>\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment