我试图将图像上传器添加到我的模板设置页面,我得到了这个:
<?php
function pp_admin_init(){
register_setting('pp_theme_options', 'pp_name');
register_setting('pp_theme_options', 'pp_title');
//Odnosniki ikon
register_setting('pp_theme_options', 'pp_share_facebook');
register_setting('pp_theme_options', 'pp_share_twitter');
register_setting('pp_theme_options', 'pp_share_google');
register_setting('pp_theme_options', 'pp_share_pinterest');
//czy wyswietlac ikony
register_setting('pp_theme_options', 'pp_icon_facebook');
register_setting('pp_theme_options', 'pp_icon_twitter');
register_setting('pp_theme_options', 'pp_icon_google');
register_setting('pp_theme_options', 'pp_icon_pinterest');
//Image test
register_setting('pp_theme_options', 'pp_image_upload', 'pp_image_upload');
}
//image test
function pp_image_upload($args){
if(!empty($_FILES['pp_image_upload']['tmp_name']))
{
$overrides = array(
'test_form' => false
);
$banner_image = wp_handle_upload($_FILES['pp_image_upload'], $overrides);
$args['pp_image_upload'] = $banner_image['url'];
}else{
$args['pp_image_upload'] = get_option('pp_theme_options')['pp_image_upload'];
}
return $args;
}
//image test
add_action('admin_init', 'pp_admin_init');
function pp_settings_page() {
?>
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/admin.css">
<div class="wrap">
<?php screen_icon(); ?>
<h2>Ustawienia szablonu Photography+</h2>
<div id="pp-settings">
<form action="options.php" method="post" id="pp-options-form">
<?php settings_fields('pp_theme_options'); ?>
<!-- Image test -->
<input type="file" name="image_upload" />
<?php
echo '<img src="' . pp_image_upload . '" style="width: 100px; vertical-align: top;" />';
?>
<!-- Image test -->
<center><h1>Ustawienia ogólne</h1></center>
<table>
<tr>
<td>
<p><label for="pp_name">Twoje imię</label></p>
</td>
<td>
<input name="pp_name" id="pp_name" type="text" value="<?php echo esc_attr(get_option('pp_name')); ?>" />
</td>
</tr>
<tr>
<td>
<p><label for="pp_title">Twój tytuł</label></p>
</td>
<td>
<input name="pp_title" id="pp_title" type="text" value="<?php echo esc_attr(get_option('pp_title')); ?>" />
</td>
</tr>
</table>
<center><h1>Adresy ikon społecznościowych</h1></center>
<table>
<tr>
<td>
<p><label for="pp_share_facebook">Facebook</label></p>
</td>
<td>
<input name="pp_share_facebook" id="pp_share_facebook" type="text" value="<?php echo esc_attr(get_option('pp_share_facebook')); ?>" />
</td>
</tr>
<tr>
<td>
<p><label for="pp_share_twitter">Twitter</label></p>
</td>
<td>
<input name="pp_share_twitter" id="pp_share_twitter" type="text" value="<?php echo esc_attr(get_option('pp_share_twitter')); ?>" />
</td>
</tr>
<tr>
<td>
<p><label for="pp_share_google">Google +</label></p>
</td>
<td>
<input name="pp_share_google" id="pp_share_google" type="text" value="<?php echo esc_attr(get_option('pp_share_google')); ?>" />
</td>
</tr>
<tr>
<td>
<p><label for="pp_share_pinterest">Pinterest</label></p>
</td>
<td>
<input name="pp_share_pinterest" id="pp_share_pinterest" type="text" value="<?php echo esc_attr(get_option('pp_share_pinterest')); ?>" />
</td>
</tr>
</table>
<center><h1>Widoczność ikonek</h1></center>
<table>
<tr>
<td>
<p>Facebook</p>
</td>
<td>
<input name="pp_icon_facebook" id="pp_icon_facebook" type="checkbox" value="1" <?php checked( '1', get_option( 'pp_icon_facebook' ) ); ?> /><label for="pp_icon_facebook">Włącz/wyłącz widoczność ikonki Facebooka</label>
</td>
</tr>
<tr>
<td>
<p>Twitter</p>
</td>
<td>
<input name="pp_icon_twitter" id="pp_icon_twitter" type="checkbox" value="1" <?php checked( '1', get_option( 'pp_icon_twitter' ) ); ?> /><label for="pp_icon_twitter">Włącz/wyłącz widoczność ikonki Twittera</label>
</td>
</tr>
<tr>
<td>
<p>Google+</p>
</td>
<td>
<input name="pp_icon_google" id="pp_icon_google" type="checkbox" value="1" <?php checked( '1', get_option( 'pp_icon_google' ) ); ?> /><label for="pp_icon_google">Włącz/wyłącz widoczność ikonki Google+</label>
</td>
</tr>
<tr>
<td>
<p>Pinterest</p>
</td>
<td>
<input name="pp_icon_pinterest" id="pp_icon_pinterest" type="checkbox" value="1" <?php checked( '1', get_option( 'pp_icon_pinterest' ) ); ?> /><label for="pp_icon_pinterest">Włącz/wyłącz widoczność ikonki Pinterest</label>
</td>
</tr>
</table>
<div class="pp_send"><input class="button button-primary" type="submit" value="zapisz" /></div>
</form>
</div>
</div>
<?php
}
function pp_settings_menu(){
add_theme_page('Photography+ - Ustawienia', 'Szablon Photography+', 'manage_options', 'pp-theme-options', 'pp_settings_page');
}
add_action('admin_menu', 'pp_settings_menu');
//powiadomienie dla admina
function show_admin_panel_message() {
$key = esc_attr(get_option('pp_name'));
$keyt = esc_attr(get_option('pp_title'));
if(empty($key)) {
echo '<div id="message" class="error"><p><strong>Szablon Photography+ wymaga dodania Imienia do strony głównej</strong></p></div>';
}
if(empty($keyt)) {
echo '<div id="message" class="error"><p><strong>Szablon Photography+ wymaga dodania Tutułu do strony głównej</strong></p></div>';
}
}
add_action('admin_notices', 'show_admin_panel_message');
?>
所有表格都可以正常工作,但图像上传器除外。有人可以解释一下如何将图像上传器添加到自定义设置页面吗?正如您在代码中看到的,我不知道如何使用add_settings_field
. 我被教过这种方法,现在我只想扩展我的知识