Все привет!
Стоит движок вордпресс.
Создал мета бокс (ы) :
function add_custom_meta_boxes() {
// Define the custom attachment for posts
add_meta_box(
'wp_custom_attachment',
'Фон загаловка страницы',
'wp_custom_attachment',
'post',
'normal'
);
// Define the custom attachment for pages
add_meta_box(
'wp_custom_attachment',
'Фон загаловка страницы',
'wp_custom_attachment',
'page',
'normal'
);
} // end add_custom_meta_boxes
add_action('add_meta_boxes', 'add_custom_meta_boxes');
function wp_custom_attachment() {
wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');
$doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);
$html = '<p class="description">';
$html .= 'Upload your jpg here.';
$html .= '</p>';
$html = '<div class="wp_custom_attachment_img">';
if(empty($doc)){
$html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';
}
$html .= '<input type="text" id="wp_custom_attachment_url" name="wp_custom_attachment_url" value="'. $doc['url'] .'" size="150" />';
$html .= '<a href="javascript:;" id="wp_custom_attachment_delete">' . __('<i class="glyphicon glyphicon-remove-circle"></i>') . '</a>';
$html .= '<img src="'. $doc['url'] .'" id="wp_custom_attachment_url wp_custom_attachment_img" />';
$html .= '</div>';
echo $html;
} // end wp_custom_attachment
function save_custom_meta_data($id) {
/* --- security verification --- */
if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) {
return $id;
} // end if
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $id;
} // end if
if(!current_user_can('edit_page', $id)) {
return $id;
} // end if
/* - end security verification - */
// Make sure the file array isn't empty
if(!empty($_FILES['wp_custom_attachment']['name'])) {
// Setup the array of supported file types. In this case, it's just PDF.
$supported_types = array('image/jpeg');
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
$uploaded_type = $arr_file_type['type'];
// Check if the type is supported. If not, throw an error.
if(in_array($uploaded_type, $supported_types)) {
// Use the WordPress API to upload the file
$upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));
if(isset($upload['error']) && $upload['error'] != 0) {
wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
} else {
$upload['file'] = str_replace('\\', '/', $upload['file']);
add_post_meta($id, 'wp_custom_attachment', $upload);
update_post_meta($id, 'wp_custom_attachment', $upload);
add_post_meta($id, 'wp_custom_attachment_url', $_POST['wp_custom_attachment_url']);
update_post_meta($id, 'wp_custom_attachment_url', $_POST['wp_custom_attachment_url']);
} // end if/else
} else {
wp_die("The file type that you've uploaded is not a PDF.");
} // end if/else
} else {
// Grab a reference to the file associated with this post
$doc = get_post_meta($id, 'wp_custom_attachment', true);
// Grab the value for the URL to the file stored in the text element
$delete_flag = get_post_meta($id, 'wp_custom_attachment_url', true);
// Determine if a file is associated with this post and if the delete flag has been set (by clearing out the input box)
if(strlen(trim($doc['url'])) > 0 && strlen(trim($delete_flag)) == 0) {
// Attempt to remove the file. If deleting it fails, print a WordPress error.
if(unlink($doc['file'])) {
// Delete succeeded so reset the WordPress meta data
update_post_meta($id, 'wp_custom_attachment', null);
update_post_meta($id, 'wp_custom_attachment_url', '');
} else {
wp_die('There was an error trying to delete your file.');
} // end if/el;se
} // end if
} // end if/else
} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');
function update_edit_form() {
echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');
Вывел картинку на страницу вот так:
<?php $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); ?>
<?php echo $doc['url']; ?>
всё работает, но при создании новой страницы появляется ошибка "Warning: Illegal string offset 'url' in" если добавить фото и перезагрузить страницу то надпись пропадает.