File: /var/www/doco2/wp-content/themes/ormedia/wp-admin-site-info.php
<?php
wp_enqueue_media();
// var_dump($_REQUEST);
$site_name = trim($_REQUEST['site_name']);
$site_logo = trim($_REQUEST['site_logo']);
if (!empty($site_name) && !empty($site_logo)) {
update_option('custom_site_name', $site_name);
update_option('custom_site_logo', $site_logo);
echo '<script>alert("更改成功!");</script>';
}
$site_name = get_option('custom_site_name');
$site_logo = get_option('custom_site_logo');
?>
<html>
<head>
<style type="text/css">
</style>
<script type="text/javascript">
jQuery(document).ready(function() {
var upload_frame;
var value_id;
jQuery('.upload_button').on('click', '', function(event) {
value_id = jQuery(this).attr('id');
event.preventDefault();
if (upload_frame) {
upload_frame.open();
return;
}
upload_frame = wp.media({
title: 'Insert image',
library: {
type: 'image'
},
button: {
text: 'Insert',
},
multiple: false
});
upload_frame.on('select', function() {
attachment = upload_frame.state().get('selection').first().toJSON();
console.log(attachment);
jQuery('input[name=' + value_id + ']').val(attachment.url);
jQuery('#preview_site_logo').css('display', 'block');
jQuery('#preview_site_logo').attr('src', attachment.url);
});
upload_frame.open();
});
});
</script>
</head>
<body>
<h1></h1>
<form class="wrap notification" method="post">
<h3>Site 設定</h3>
<table class="form-table">
<tbody>
<tr>
<th scope="row">站點名稱</th>
<td>
<input type="text" size="60" value="<?php echo $site_name; ?>" name="site_name" />
</td>
</tr>
<tr>
<th scope="row">站點 Logo</th>
<td>
<img id="preview_site_logo" src="<?php echo $site_logo; ?>" style="max-width: 200px; <?php echo empty($site_logo) ? "display:none;" : "display:block;"; ?>margin-bottom:10px;">
<input type="text" size="60" value="<?php echo $site_logo; ?>" name="site_logo" hidden />
<a id="site_logo" class="upload_button button" href="#">選擇媒體文件</a>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="儲存設定">
</p>
</form>
</body>
</html>