This article is to help some people having trouble adding this function into WordPress.
This is a modified version of this Steve Taylor‘s work.
Include this in a function.php file and it will add custom fields to post and page.
By default it will only display 1 field.
This field allow you to use the media library to insert the url of the image or file you selected.
Select your image and click “Insert into post” button just has you would add a image to the content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
<?php
if ( !class_exists('myCustomFields') ) {
class myCustomFields {
/**
* @var string $prefix The prefix for storing custom fields in the postmeta table
*/
var $prefix = '_sfr_';
/**
* @var array $customFields Defines the custom fields available
*/
var $customFields = array(
array(
"name" => "name_of_your_meta_value",
"title" => "Label goes here",
"description" => "Fully qualified URL (i.e: http://...)",
"type" => "file_upload", // OR checkbox, textarea
"scope" => array( "post", "page" ),
"capability" => "edit_pages"
),
array(
"name" => "name_of_your_meta_value2",
"title" => "Label goes here 2",
"description" => "Fully qualified URL (i.e: http://...)",
"type" => "file_upload", // OR checkbox, textarea
"scope" => array( "post", "page" ),
"capability" => "edit_pages"
)
);
/**
* PHP 4 Compatible Constructor
*/
function myCustomFields() { $this->__construct(); }
/**
* PHP 5 Constructor
*/
function __construct() {
add_action( 'admin_menu', array( &$this, 'createCustomFields' ) );
add_action( 'save_post', array( &$this, 'saveCustomFields' ), 1, 2 );
}
/**
* Create the new Custom Fields meta box
*/
function createCustomFields() {
if ( function_exists( 'add_meta_box' ) ) {
$tempChek = array();
foreach ( $this->customFields as $customField ) {
// Check scope
$scope = $customField[ 'scope' ];
foreach ( $scope as $scopeItem ) {
if (!in_array($scopeItem, $tempChek)) {
$tempChek[] = $scopeItem;
add_meta_box( 'my-custom-fields', __('Page Options'), array( &$this, 'displayCustomFields' ), $scopeItem, 'normal', 'high' );
}
}
}
}
}
/**
* Display the new Custom Fields meta box
*/
function displayCustomFields() {
global $post;
?>
<div class="form-wrap">
<?php
wp_nonce_field( 'my-custom-fields', 'my-custom-fields_wpnonce', false, true );
foreach ( $this->customFields as $customField ) {
// Check scope
$scope = $customField[ 'scope' ];
$output = false;
foreach ( $scope as $scopeItem ) {
switch ( $scopeItem ) {
case "post": {
// Output on any post screen
if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="post-new.php" || $post->post_type=="post" )
$output = true;
break;
}
case "page": {
// Output on any page screen
if ( basename( $_SERVER['SCRIPT_FILENAME'] )=="page-new.php" || $post->post_type=="page" )
$output = true;
break;
}
}
if ( $output ) break;
}
// Output if allowed
if ( $output ) {
?>
<div class="form-field form-required">
<script type="text/javascript">
//<![CDATA[
var formfield = '';
jQuery(document).ready(function() {
// Store original function
var original_send_to_editor = window.send_to_editor;
/**
* Override send_to_editor function from original script
* Writes URL into the textbox.
*
* Note: If header is not clicked, we use the original function.
*/
window.send_to_editor = function(html) {
if (formfield == '') {
original_send_to_editor(html);
} else {
fileurl = jQuery(html).attr('href');
if (typeof(fileurl)==="undefined") {
fileurl = jQuery('img',html).attr('src');
}
jQuery('#' + formfield).val(fileurl);
tb_remove();
formfield = '';
}
}
});
//]]>
</script>
<?php
switch ( $customField[ 'type' ] ) {
case "checkbox": {
// Checkbox
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'" style="display:inline;"><b>' . $customField[ 'title' ] . '</b></label> ';
echo '<input type="checkbox" name="' . $this->prefix . $customField['name'] . '" id="' . $this->prefix . $customField['name'] . '" value="yes"';
if ( get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) == "yes" )
echo ' checked="checked"';
echo '" style="width: auto;" />';
break;
}
case "textarea": {
// Text area
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . $customField[ 'title' ] . '</b></label>';
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>';
break;
}
case "file_upload": {
// Add a media upload/library
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . __($customField[ 'title' ]) . '</b>';
echo ' <img src="/wp-admin/images/media-button-image.gif" alt="Ajouter une image" id="'. $this->prefix . $customField[ 'name' ] .'_btn"></label>';
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 ) ) . '" />';
?>
<script type="text/javascript">
//<![CDATA[
jQuery('#<?php echo $this->prefix . $customField[ 'name' ]; ?>_btn').click(function() {
formfield = jQuery('#<?php echo $this->prefix . $customField[ 'name' ]; ?>').attr('name'); // set this to the id field you want to populate with the url
tb_show('', 'media-upload.php?post_id=<?php echo $post->ID; ?>&type=image&TB_iframe=1');
return false;
});
//]]>
</script>
<?php
break;
}
default: {
// Plain text field
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><b>' . __($customField[ 'title' ]) . '</b></label>';
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 ) ) . '" />';
break;
}
}
?>
<?php if ( $customField[ 'description' ] ) echo '<p>' . __($customField[ 'description' ]) . '</p>'; ?>
</div>
<?php
}
} ?>
</div>
<?php
}
/**
* Save the new Custom Fields values
*/
function saveCustomFields( $post_id, $post ) {
if ( !wp_verify_nonce( $_POST[ 'my-custom-fields_wpnonce' ], 'my-custom-fields' ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
if ( $post->post_type != 'page' && $post->post_type != 'post' )
return;
foreach ( $this->customFields as $customField ) {
if ( current_user_can( $customField['capability'], $post_id ) ) {
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) && trim( $_POST[ $this->prefix . $customField['name'] ] ) ) {
update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $_POST[ $this->prefix . $customField['name'] ] );
} else {
delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
}
}
}
}
} // End Class
} // End if class exists statement
// Instantiate the class
if ( class_exists('myCustomFields') ) {
$myCustomFields_var = new myCustomFields();
}
?> |
You can add multiple fields if you want.
You only have to change this to add more fields:
1 2 3 4 5 6 7 8 9 10 |
var $customFields = array(
array(
"name" => "name_of_your_meta_value",
"title" => "Label goes here",
"description" => "Fully qualified URL (i.e: http://...)",
"type" => "checkbox", // OR checkbox, textarea
"scope" => array( "page" ),
"capability" => "edit_pages"
)
); |
Hope it helps