upload(sample)

Display an image upload field that displays a preview of the image uploaded. Can also be used to upload other types of files such as a favicon.

Option Details & Parameters

Type: uplaod

Can be placed in: Admin Panels , Admin Tabs , Meta Boxes , Theme Customizer
[zilla_alert]As of version 1.5, the upload option no longer returns the direct URL to the selected image, but instead returns an attachment ID to the select image.[/zilla_alert]

Parameter Type Description
name string The name of the option, for display purposes only.
id string A unique ID for this option. This ID will be used to get the value for this option.
size string (Optional) The size of the image to use in the css parameter. The valid values for this are the same ones used in the $size argument in wp_get_attachment_image_src() function. Defaults to full. Introduced in v1.6
desc string The description to display together with this option.
default string (Optional) The default value for this option.
livepreview string (Optional) jQuery code that updates something in your site in the live preview. Only used when the option is placed in a theme customizer section. Refer to Livepreview for more information on this parameter.
css string (Optional) CSS rules to be used with this option. Only used when the option is placed in an admin page / panel or a theme customizer section. Refer to Generate CSS Automatically for your options for more information on this parameter.Example: body { background-image: value; }
placeholder string (Optional) The text to display inside the field when the field is empty

Return Value

[zilla_alert]As of version 1.5, the upload option no longer returns the direct URL to the selected image, but instead returns an attachment ID to the select image.[/zilla_alert]Returns an attachment ID of the selected image, or the image URL specified in the default parameter.

Since the return value may be an ID or a URL, you can use the PHP function is_numeric to check for the type of the value.

The WordPress function wp_get_attachment_image_src() can be used to get the URL and details on the selected image. More info in the Codex: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

Examples

Creating an upload option:

$panel->createOption( array(
'name' => 'My Upload Option',
'id' => 'my_upload_option',
'type' => 'upload',
'desc' => 'Upload your image'
) );

Getting the image in your header.php prior to version 1.5

<body>
<?php
$titan = TitanFramework::getInstance( 'my-theme' );
$image = $titan->getOption( 'my_upload_option' );
?>
<img src='<?php echo esc_url( $image ); ?>' />
</body>

Getting the image in your header.php for version 1.5 and beyond

<body>
<?php
$titan = TitanFramework::getInstance( 'my-theme' );
$imageID = $titan->getOption( 'my_upload_option' );
// The value may be a URL to the image (for the default parameter)
// or an attachment ID to the selected image.
$imageSrc = $imageID; // For the default value
if ( is_numeric( $imageID ) ) {
$imageAttachment = wp_get_attachment_image_src( $imageID );
$imageSrc = $imageAttachment[0];
} 
?>
<img src='<?php echo esc_url( $imageSrc ); ?>' />
</body>