Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2010
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.25 KB | None | 0 0
  1. *
  2. Plugin Name: Custom Write Panel
  3. Plugin URI: http://imagewize.net
  4. Description: Allows custom fields to be added to a WordPress Post, Page or Custom Post Type
  5. Version: 1.1
  6. License: his code is licensed under the GPL v2.0 http://www.opensource.org/licenses/gpl-2.0.php
  7. Author: Jasper Frumau
  8. Based on http://sltaylor.co.uk/blog/control-your-own-wordpress-custom-fields/ and http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/
  9. /* ----------------------------------------------*/
  10.  
  11. if ( !class_exists('myCustomFields') ) {
  12.  
  13.     class myCustomFields {
  14.         /**
  15.         * @var  string  $prefix  The prefix for storing custom fields in the postmeta table
  16.         */
  17.         var $prefix = '_mcf_';
  18.         /**
  19.         * @var  array  $customFields  Defines the custom fields available
  20.         */
  21.         var $customFields = array(
  22.             array(
  23.                 "name"          => "large-image",
  24.                 "title"         => "Eerste grote afbeelding",
  25.                 "description"   => "Voeg de afbeelding toe via de media uploader en plaats hier de link",
  26.                 "type"          => "text",
  27.                 "scope"         =>  array( "portfolio","post" ),
  28.                 "capability"    => "edit_pages"
  29.             ),
  30.             /*array(
  31.                 "name"          => "block-of-text",
  32.                 "title"         => "A block of text",
  33.                 "description"   => "",
  34.                 "type"          => "textarea",
  35.                 "scope"         =>  array( "page" ),
  36.                 "capability"    => "edit_pages"
  37.             ), */
  38.             /*array(
  39.                 "name"          => "checkbox",
  40.                 "title"         => "Checkbox",
  41.                 "description"   => "",
  42.                 "type"          => "checkbox",
  43.                 "scope"         =>  array( "post","page" ),
  44.                 "capability"    => "manage_options"
  45.             ), */
  46.             array(
  47.                 "name"          => "client",
  48.                 "title"         => "Naam client",
  49.                 "description"   => "De naam van de client",
  50.                 "type"          =>  "text",
  51.                 "scope"         =>  array( "portfolio" ,"post" ),
  52.                 "capability"    => "edit_posts"
  53.                 ),
  54.             array(
  55.                 "name"          => "technologie",
  56.                 "title"         => "Technologie",
  57.                 "description"   => "Welke technologie of technieken en eventuele CMS zijn gebruikt bij het ontwerpen van dit projects",
  58.                 "options"       => array("xhtml", "css", "JavaScript", "Joomla", "Wordpress", "Magento"),
  59.                 "type"          =>  "checkboxes",
  60.                 "scope"         =>  array( "portfolio", "post" ),
  61.                 "capability"    => "edit_posts"
  62.             ),
  63.        
  64.             array(
  65.                 "name"          => "online",
  66.                 "title"         => "Online of Offline",
  67.                 "description"   => "Is de website online en actief?",
  68.                 "type"          =>  "dropdown",
  69.                 "options"       => array( "online", "offline"),
  70.                 "scope"         =>  array( "portfolio", "post" ),
  71.                 "capability"    => "edit_posts"
  72.                 )
  73.                
  74.         );
  75.         /**
  76.         * PHP 4 Compatible Constructor
  77.         */
  78.         function myCustomFields() { $this->__construct(); }
  79.         /**
  80.         * PHP 5 Constructor
  81.         */
  82.         function __construct() {
  83.             add_action( 'admin_menu', array( &$this, 'createCustomFields' ) );
  84.             add_action( 'save_post', array( &$this, 'saveCustomFields' ), 1, 2 );
  85.             // Comment this line out if you want to keep default custom fields meta box
  86.             add_action( 'do_meta_boxes', array( &$this, 'removeDefaultCustomFields' ), 10, 3 );
  87.         }
  88.         /**
  89.         * Remove the default Custom Fields meta box
  90.         */
  91.         function removeDefaultCustomFields( $type, $context, $post ) {
  92.             foreach ( array( 'normal', 'advanced', 'side' ) as $context ) {
  93.                 remove_meta_box( 'postcustom', 'post', $context );
  94.                 remove_meta_box( 'postcustom', 'page', $context );
  95.                 //Use the line below instead of the line above for WP versions older than 2.9.1
  96.                 //remove_meta_box( 'pagecustomdiv', 'page', $context );
  97.             }
  98.         }
  99.         /**
  100.         * Create the new Custom Fields meta box
  101.         */
  102.         function createCustomFields() {
  103.             if ( function_exists( 'add_meta_box' ) ) {
  104.                 //add_meta_box( 'my-custom-fields', 'Custom Fields', array( &$this, 'displayCustomFields' ), 'page', 'normal', 'high' );
  105.                 add_meta_box( 'my-custom-fields', 'Custom Fields', array( &$this, 'displayCustomFields' ), 'post', 'normal', 'high' );
  106.                 add_meta_box( 'my-custom-fields', 'Portfolio Details', array( &$this, 'displayCustomFields' ), 'portfolio', 'normal', 'high' );
  107.             }
  108.         }
  109.         /**
  110.         * Display the new Custom Fields meta box
  111.         */
  112.         function displayCustomFields() {
  113.             global $post;
  114.             ?>
  115.             <div class="form-wrap">
  116.                 <?php
  117.                 wp_nonce_field( 'my-custom-fields', 'my-custom-fields_wpnonce', false, true );
  118.                 foreach ( $this->customFields as $customField ) {
  119.                     // Check scope
  120.                     $scope = $customField[ 'scope' ];
  121.                     $output = false;
  122.                     foreach ( $scope as $scopeItem ) {
  123.                         switch ( $scopeItem ) {
  124.                             case "post": {
  125.                                 // Output on any post screen
  126.                                 if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="post-new.php" || $post->post_type=="post" )
  127.                                     $output = true;
  128.                                 break;
  129.                             }
  130.                                 case "portfolio": {
  131.                                     // Output on any portfolio page
  132.                                     if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="post-new.php" || $post->post_type=="portfolio" )
  133.                                         $output = true;
  134.                                     break;
  135.                                 }
  136.                             case "page": {
  137.                                 // Output on any page screen
  138.                                 if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="page-new.php" || $post->post_type=="page" )
  139.                                     $output = true;
  140.                                 break;
  141.                             }
  142.                            
  143.                         }
  144.                         if ( $output ) break;
  145.                     }
  146.                     // Check capability
  147.                     if ( !current_user_can( $customField['capability'], $post->ID ) )
  148.                         $output = false;
  149.                     // Output if allowed
  150.                     if ( $output ) { ?>
  151.                         <div class="form-field form-required">
  152.                             <?php
  153.                             switch ( $customField[ 'type' ] ) {
  154.                                 case "checkbox": {
  155.                                     // Checkbox
  156.                                     echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label>&nbsp;&nbsp;';
  157.                                     echo '<input type="checkbox" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="yes"';
  158.                                     if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "yes" )
  159.                                         echo ' checked="checked"';
  160.                                     echo '" style="width: auto;" />';
  161.                                     break;
  162.                                 }
  163.                                 case "checkboxes": {
  164.                                  // Checkboxes Technology
  165.                                  echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label>&nbsp;&nbsp;';
  166.                                 foreach($customField[ 'options' ] as $value){
  167.                                     echo  '<input style="width: auto;margin:0 5px;" type="checkbox" value="'. $value . '" name="'.$customField['name'] . '"';
  168.                                 if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == $value ) echo ' checked="checked"';
  169.                                 //if (isset($_POST[$customField]) == true ) echo ' checked="checked"';
  170.                                 echo  '>' .$value . '</input>'."\n";};
  171.                                 break;
  172.                                  }
  173.                            
  174.                                 case "textarea": {
  175.                                     // Text area
  176.                                     echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>';
  177.                                     echo '<textarea name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" columns="30" rows="3">' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '</textarea>';
  178.                                     break;
  179.                                 }
  180.                                 case "dropdown": {
  181.                                 // Dropdown for online or offline website indication
  182.                                 echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>';
  183.                                 echo '<select name="' . $this->prefix . $customField[ 'name' ] . '"><option value="0">Choose site status</option>';
  184.                                 foreach($customField[ 'options' ] as $value){        
  185.  
  186.                                 if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == $value ) $selected = "selected=\"selected\"";
  187.  
  188.                                 echo  '<option value="'.$value.'"'.$selected.'">' .$value . '</option>'."\n";}
  189.                                 echo "</select>";
  190.                                     break;
  191.                                 }
  192.                                 default: {
  193.                                     // Plain text field
  194.                                     echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>';
  195.                                     echo '<input type="text" name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" value="' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '" />';
  196.                                     break;
  197.                                 }
  198.                             }
  199.                                
  200.                             ?>
  201.                             <?php if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>'; ?>
  202.                         </div>
  203.                     <?php
  204.                     }
  205.                 } ?>
  206.             </div>
  207.             <?php
  208.         }
  209.         /**
  210.         * Save the new Custom Fields values
  211.         */
  212.         function saveCustomFields( $post_id, $post ) {
  213.             if ( !wp_verify_nonce( $_POST[ 'my-custom-fields_wpnonce' ], 'my-custom-fields' ) )
  214.                 return;
  215.             if ( !current_user_can( 'edit_post', $post_id ) )
  216.                 return;
  217.             if ( $post->post_type != 'page' && $post->post_type != 'post' && $post->post_type != 'portfolio' )
  218.                 return;
  219.                 foreach ( $this->customFields as $customField ) {
  220.                  if ( current_user_can( $customField['capability'], $post_id ) ) {
  221.                 if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) && trim( $_POST[ $this->prefix . $customField['name'] ] ) ) {
  222.                 update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $_POST[ $this->prefix . $customField['name'] ] );
  223.                 } else {
  224.                 delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
  225.            
  226.                     }
  227.                 }
  228.             }
  229.         }
  230.  
  231.     } // End Class
  232.  
  233. } // End if class exists statement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement