我在 Wordpress 中的自定义选项页面有问题。这个问题与我前段时间提出的媒体上传器有关。它可以正常工作,但是当重新保存选项时,上传文件的字段中的 url 消失了,并且在我的 html 代码中没有显示图像。仅当我在同一选项页面中连续两次保存选项时才会发生这种情况。url 应该保留在该字段中,但这不会发生。
要放入的媒体上传器的代码functions.php
是:
//Media Uploader Scripts
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('my-upload', get_bloginfo( 'stylesheet_directory' ) . '/js/uploader.js', array('jquery','media-upload','thickbox'));
wp_enqueue_script('my-upload');
//Media Uploader Style
wp_enqueue_style('thickbox');
if (isset($_GET['page']) && $_GET['page'] == 'my_plugin_page') {
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
}
uploader.js
的内容在哪里
jQuery(document).ready(function() {
jQuery('#upload_image_button').click(function() {
formfield = jQuery('#upload_image').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#upload_image').val(imgurl);
tb_remove();
}
});
这是添加到的代码functions.php
<?php
add_action( 'init', 'th_admin_init' );
add_action( 'admin_menu', 'th_options_page_init' );
function th_admin_init() {
$options = get_option( "th_theme_options" );
if ( empty( $options ) ) {
$options = array(
'th_intro' => 'Some intro text for the home page',
'th_tag_class' => false,
'th_ga' => false,
'upload_image' => ''
);
add_option( "th_theme_options", $options, '', 'yes' );
}
}
function th_options_page_init() {
$theme_data = get_theme_data( TEMPLATEPATH . '/style.css' );
$options_page = add_theme_page( $theme_data['Name']. ' Theme options', $theme_data['Name']. ' Opzioni', 'edit_theme_options', 'theme-options', 'th_options_page' );
add_action( "load-{$options_page}", 'th_load_options_page' );
}
function th_load_options_page() {
if ( $_POST["th-options-submit"] == 'Y' ) {
check_admin_referer( "th-options-page" );
th_save_theme_options();
$url_parameters = isset($_GET['tab'])? 'updated=true&tab='.$_GET['tab'] : 'updated=true';
wp_redirect(admin_url('themes.php?page=theme-options&'.$url_parameters));
exit;
}
}
function th_save_theme_options() {
global $pagenow;
$options = get_option( "th_theme_options" );
if ( $pagenow == 'themes.php' && $_GET['page'] == 'theme-options' ){
if ( isset ( $_GET['tab'] ) )
$tab = $_GET['tab'];
else
$tab = 'homepage';
switch ( $tab ){
case 'general' :
$options['th_tag_class'] = $_POST['th_tag_class'];
break;
case 'footer' :
$options['th_ga'] = $_POST['th_ga'];
break;
case 'homepage' :
$options['th_intro'] = $_POST['th_intro'];
$options['upload_image'] = $_POST['upload_image'];
break;
}
}
if( !current_user_can( 'unfiltered_html' ) ){
if ( $options['th_ga'] )
$options['th_ga'] = stripslashes( esc_textarea( wp_filter_post_kses( $options['th_ga'] ) ) );
if ( $options['th_intro'] )
$options['th_intro'] = stripslashes( esc_textarea( wp_filter_post_kses( $options['th_intro'] ) ) );
if ( $options['upload_image'] )
$options['upload_image'] = stripslashes( esc_textarea( wp_filter_post_kses( $options['upload_image'] ) ) );
}
$updated = update_option( "th_theme_options", $options );
}
function th_admin_tabs( $current = 'homepage' ) {
$tabs = array( 'homepage' => 'Home', 'general' => 'Generali', 'footer' => 'Footer' );
$links = array();
echo '<div id="icon-themes" class="icon32"><br></div>';
echo '<h2 class="nav-tab-wrapper">';
foreach( $tabs as $tab => $name ){
$class = ( $tab == $current ) ? ' nav-tab-active' : '';
echo "<a class='nav-tab$class' href='?page=theme-options&tab=$tab'>$name</a>";
}
echo '</h2>';
}
function th_options_page() {
global $pagenow;
$options = get_option( "th_theme_options" );
$theme_data = get_theme_data( TEMPLATEPATH . '/style.css' );
//Media Uploader Scripts
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('my-upload', get_bloginfo( 'stylesheet_directory' ) . '/js/uploader.js', array('jquery','media-upload','thickbox'));
wp_enqueue_script('my-upload');
//Media Uploader Style
wp_enqueue_style('thickbox');
if (isset($_GET['page']) && $_GET['page'] == 'theme_options') {
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
}
?>
<div class="wrap">
<h2><?php echo $theme_data['Name']; ?> Opzioni</h2>
<?php
if ( 'true' == esc_attr( $_GET['updated'] ) ) echo '<div class="updated" ><p>Modifiche salvate.</p></div>';
if ( isset ( $_GET['tab'] ) ) th_admin_tabs($_GET['tab']); else th_admin_tabs('homepage');
?>
<div id="poststuff">
<form method="post" action="<?php admin_url( 'themes.php?page=theme-options' ); ?>">
<?php
wp_nonce_field( "th-options-page" );
if ( $pagenow == 'themes.php' && $_GET['page'] == 'theme-options' ){
if ( isset ( $_GET['tab'] ) ) $tab = $_GET['tab'];
else $tab = 'homepage';
echo '<table class="form-table">';
switch ( $tab ){
case 'general' :
?>
<tr>
<th><label for="th_tag_class">Tags with CSS classes:</label></th>
<td>
<input id="th_tag_class" name="th_tag_class" type="checkbox" <?php if ( $options["th_tag_class"] ) echo 'checked="checked"'; ?> value="true" />
<span class="description">Output each post tag with a specific CSS class using its slug.</span>
</td>
</tr>
<?php
break;
case 'footer' :
?>
<tr>
<th><label for="th_ga">Insert tracking code:</label></th>
<td>
<textarea id="th_ga" name="th_ga" cols="60" rows="5"><?php echo esc_html( stripslashes( $options["th_ga"] ) ); ?></textarea><br/>
<span class="description">Enter your Google Analytics tracking code:</span>
</td>
</tr>
<?php
break;
case 'homepage' :
?>
<tr>
<th><label for="th_intro">Introduction</label></th>
<td>
<textarea id="th_intro" name="th_intro" cols="60" rows="5" ><?php echo esc_html( stripslashes( $options["th_intro"] ) ); ?></textarea><br/>
<span class="description">Enter the introductory text for the home page:</span>
</td>
</tr>
<tr>
<th><label for="upload_image">Upload Image</label></th>
<td>
<input id="upload_image" type="text" size="36" name="upload_image" value="" />
<input id="upload_image_button" type="button" class="button-secondary" value="Carica Immagine" />
<br /><span class="description">Enter an URL or upload an image for the banner.</span>
</td>
</tr>
<?php
break;
}
echo '</table>';
}
?>
<p class="submit" style="clear: both;">
<input type="submit" name="Submit" class="button-primary" value="Update options" />
<input type="hidden" name="th-options-submit" value="Y" />
</p>
</form>
</div>
</div>
<?php
}
?>
任何人都可以帮助我吗?
先感谢您!
- - - - - - 解决方案 - - - - - -
这不是数据库问题。这只是一个 HTML 问题,因为value
输入中命名的元素upload_image
被设置为""
. 因此,将文件表单内的代码function.php
从以下位置更改:
<input id="upload_image" type="text" size="36" name="upload_image" value="" />
至:
<input id="upload_image" type="text" size="36" name="upload_image" value="<?php echo esc_html( stripslashes( $options["upload_image"] ) ); ?>" />