如何在自定义选项面板中保存上传的图像?

时间:2011-09-14 作者:Downloadtaky

我正在尝试根据Netuts上的教程向新模板添加自定义选项面板。我将选项面板的所有代码保存在一个名为optionpanel的文件中。php(LoL)和一切都正常,但现在我想添加一个上传/图像按钮,这样我就可以直接从Wordpress媒体库更改幻灯片中的图像,而不是每次都通过FTP连接。

问题是,如何做到这一点?这就是我现在所拥有的,我可以看到媒体上传框,我可以上传图像,图像存储在媒体库中,但当我单击选项面板中的“保存”并尝试回显该功能时,它不会给我任何提示,在保存有图像链接的表单后,该图像再次变为空!

我知道我的英语很难听,请礼貌一点,如果可以的话,试着向我解释,就像你在和你3岁的儿子说话一样!

我搜索了很多(可能太多了!!!)我读到:

http://www.sitepoint.com/wordpress-options-panel/

http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/

http://www.theenglishguy.co.uk/2010/01/24/multiple-media-uploads-in-wordpress-functions-php/

https://stackoverflow.com/questions/2388697/wordpress-2-9-theme-options-admin-page-with-image-upload

http://www.wptavern.com/forum/themes-templates/1346-creating-upload-function-options-page.html#post13306

http://sicdigital.com/2010/07/create-custom-post-type-for-image-upload-wordpress3/

http://www.tanzilo.com/2009/01/15/wordpress-adding-a-custom-option-box-and-developing-file-upload-plugin/

http://wordpress.org/support/topic/file-upload-with-custom-post-type?replies=8

https://github.com/JeffreyWay/WordPress-Theme-Options-Page/blob/master/admin-menu.php

http://stuntsnippets.com/wordpress-custom-theme-options/

http://www.sitepoint.com/wordpress-options-panel/

还有更多!!!!

下面是我的代码:

<?php

$themename = "Centenario Pascoliano";
$shortname = "cp";

$categories = get_categories(\'hide_empty=1&orderby=name\');
$wp_cats = array();
foreach ($categories as $category_list ) {
       $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
array_unshift($wp_cats, "Choose a category"); 

$options = array (

array( "name" => $themename." Opzioni",
    "type" => "title"),


array( "name" => "Generale",
    "type" => "section"),
array( "type" => "open"),

array( "name" => "Colour Scheme",
    "desc" => "Select the colour scheme for the theme",
    "id" => $shortname."_color_scheme",
    "type" => "select",
    "options" => array("blue", "red", "green"),
    "std" => "blue"),

array( "name" => "URL Locandina",
    "desc" => "Inserisci il link alla locandina",
    "id" => $shortname."_locandinah",
    "type" => "text",
    "std" => "/wp-content/themes/centenario/images/locandinacentenario.png"),

array( "name" => "Product Image Upload",  
    "desc" => "Upload product image",  
    "id" => $shortname."_upload_one",  
    "type" => "upload",  
    "std" => ""),       

array( "type" => "close"),
array( "name" => "Homepage",
    "type" => "section"),
array( "type" => "open"),

array( "name" => "Homepage header image",
    "desc" => "Enter the link to an image used for the homepage header.",
    "id" => $shortname."_header_img",
    "type" => "text",
    "std" => ""),

array( "name" => "Prima categoria da mostrare",
    "desc" => "Scegli la prima categoria da mostrare in HomePage",
    "id" => $shortname."_feat_cat",
    "type" => "select",
    "options" => $wp_cats,
    "std" => "Scegli la prima categoria"),

array( "name" => "Seconda categoria da mostrare",
    "desc" => "Scegli la seconda categoria da mostrare in HomePage",
    "id" => $shortname."_feat_cat2",
    "type" => "select",
    "options" => $wp_cats,
    "std" => "Scegli la seconda categoria"),

array( "type" => "close"),
array( "name" => "Footer",
    "type" => "section"),
array( "type" => "open"),

array( "name" => "Footer copyright text",
    "desc" => "Enter text used in the right side of the footer. It can be HTML",
    "id" => $shortname."_footer_text",
    "type" => "text",
    "std" => ""),

array( "name" => "Google Analytics Code",
    "desc" => "You can paste your Google Analytics or other tracking code in this box.             This will be automatically added to the footer.",
    "id" => $shortname."_ga_code",
    "type" => "textarea",
    "std" => ""),   

array( "name" => "Custom Favicon",
    "desc" => "A favicon is a 16x16 pixel icon that represents your site; paste the URL     to a .ico image that you want to use as the image",
    "id" => $shortname."_favicon",
    "type" => "text",
    "std" => get_bloginfo(\'url\') ."/favicon.ico"),  

array( "name" => "Feedburner URL",
    "desc" => "Feedburner is a Google service that takes care of your RSS feed. Paste     your Feedburner URL here to let readers see it in your website",
    "id" => $shortname."_feedburner",
    "type" => "text",
    "std" => get_bloginfo(\'rss2_url\')),


array( "type" => "close")

);


function mytheme_add_admin() {

global $themename, $shortname, $options;

if ( $_GET[\'page\'] == basename(__FILE__) ) {

if ( \'save\' == $_REQUEST[\'action\'] ) {

    foreach ($options as $value) {
    update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ] ); }

foreach ($options as $value) {  

if( $value[\'type\'] == \'upload\' )

{

    $upload = wp_handle_upload( $_FILES[ $value[\'id\'] ] );

    if( isset( $upload[\'url\'] ) )

    {

        update_option( $value[\'id\'], $upload[\'url\'] );

    }

}

        elseif( isset( $_REQUEST[ $value[\'id\'] ] ) )

    {

        update_option( $value[\'id\'], $_REQUEST[ $value[\'id\'] ]  );

    }

    }

header("Location: admin.php?page=optionpanel.php&saved=true");
die;

} 
    else if( \'reset\' == $_REQUEST[\'action\'] ) {

foreach ($options as $value) {
    delete_option( $value[\'id\'] ); }

header("Location: admin.php?page=optionpanel.php&reset=true");
die;

}
}

add_menu_page($themename, $themename, \'administrator\', basename(__FILE__),     \'mytheme_admin\');
}

function mytheme_add_init() {

$file_dir=get_bloginfo(\'template_directory\');
wp_enqueue_style("functions", $file_dir."/functions/functions.css", false, "1.0",     "all");
wp_enqueue_script("rm_script", $file_dir."/functions/rm_script.js", false, "1.0");

}
function mytheme_admin() {

global $themename, $shortname, $options;
$i=0;

if ( $_REQUEST[\'saved\'] ) echo \'<div id="message" class="updated fade"><p>        <strong>\'.$themename.\' settings saved.</strong></p></div>\';
if ( $_REQUEST[\'reset\'] ) echo \'<div id="message" class="updated fade"><p>    <strong>\'.$themename.\' settings reset.</strong></p></div>\';

?>
<div class="wrap rm_wrap">
<h2><?php echo $themename; ?> Settings</h2>

<div class="rm_opts">
<form method="post" enctype="multipart/form-data" >  
<?php foreach ($options as $value) {
switch ( $value[\'type\'] ) {

case "open":
?>

<?php break;

case "close":
?>

</div>
</div>
<br />


<?php break;

case "title":
?>
<p>To easily use the <?php echo $themename;?> theme, you can use the menu below.</p>


<?php break;

case \'text\':
?>

<div class="rm_input rm_text">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>
    <input name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" type="<?    php echo $value[\'type\']; ?>" value="<?php if ( get_settings( $value[\'id\'] ) != "") { echo     stripslashes(get_settings( $value[\'id\'])  ); } else { echo $value[\'std\']; } ?>" />
 <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>

 </div>
<?php
break;

case \'textarea\':
?>

<div class="rm_input rm_textarea">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>
    <textarea name="<?php echo $value[\'id\']; ?>" type="<?php echo $value[\'type\']; ?>"     cols="" rows=""><?php if ( get_settings( $value[\'id\'] ) != "") { echo     stripslashes(get_settings( $value[\'id\']) ); } else { echo $value[\'std\']; } ?></textarea>
 <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>

 </div>

<?php
break;

case \'select\':
?>

<div class="rm_input rm_select">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>

<select name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>">
<?php foreach ($value[\'options\'] as $option) { ?>
        <option <?php if (get_settings( $value[\'id\'] ) == $option) { echo     \'selected="selected"\'; } ?>><?php echo $option; ?></option><?php } ?>
</select>

    <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>
</div>
<?php
break;

case "checkbox":
?>

<div class="rm_input rm_checkbox">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>

<?php if(get_option($value[\'id\'])){ $checked = "checked=\\"checked\\""; }else{ $checked =     "";} ?>
<input type="checkbox" name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\'];     ?>" value="true" <?php echo $checked; ?> />


    <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>
 </div>
<?php break; 
case "section":

$i++;

?>

<div class="rm_section">
<div class="rm_title"><h3><img src="<?php bloginfo(\'template_directory\')?    >/functions/images/trans.png" class="inactive" alt="""><?php echo $value[\'name\']; ?></h3>        <span class="submit"><input name="save<?php echo $i; ?>" type="submit" value="Save changes"     />
</span><div class="clearfix"></div></div>
<div class="rm_options">


<?php break;
case "upload";?>
<div class="rm_input rm_upload">
        <tr valign="top">
<th scope="row">Upload Image</th>
<td><label for="upload_image">
<input id="upload_image" type="text" size="36" name="upload_image" value="" />
<input id="upload_image_button" type="button" value="Upload Image" />
<br />Enter an URL or upload an image for the banner.
</label></td>
</tr>
<div class="clearfix"></div> 
</div>

<?php break;
}
}
?>

<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
<div style="font-size:9px; margin-bottom:10px;">Icons: <a     href="http://www.woothemes.com/2009/09/woofunction/">WooFunction</a></div>
 </div> 


<?php
}
    ?>
    <?php
    function my_admin_scripts() {
wp_enqueue_script(\'media-upload\');
wp_enqueue_script(\'thickbox\');
wp_register_script(\'my-upload\', get_bloginfo(\'template_url\') .     \'/functions/maisdesignscript.js\', array(\'jquery\',\'media-upload\',\'thickbox\'));
wp_enqueue_script(\'my-upload\');
}
function my_admin_styles() {
wp_enqueue_style(\'thickbox\');
}
if (isset($_GET[\'page\']) && $_GET[\'page\'] == \'optionpanel.php\') {
add_action(\'admin_print_scripts\', \'my_admin_scripts\');
add_action(\'admin_print_styles\', \'my_admin_styles\');}
add_action(\'admin_init\', \'mytheme_add_init\');
add_action(\'admin_menu\', \'mytheme_add_admin\');
?>

1 个回复
最合适的回答,由SO网友:Brian Fegter 整理而成

您可以使用Wordrpess Settings API 也可以使用选项框架。我的团队构建了一个非常棒的选项框架,很容易融入你的主题。它被称为“向上策略框架”。您可以在github获取它的副本。https://github.com/chriswallace/UpThemes-Framework

要安装,只需将admin和theme options文件夹放到主题目录中即可。编辑主题选项文件,以便在代码中添加更多选项。最后,将其添加到函数中。php文件。

require_once(\'admin/admin.php\');
然后,通过在模板中使用以下内容,您将拥有指尖上的所有选项值。

global $up_options;
echo $up_options->option_id;
我们仍在记录所有选项类型(排版管理、颜色纠察等)的过程中。您可以看到一些功能here.

结束

相关推荐

为什么jQuery-UI-core在我的页脚而不是页眉中入队?

这就是我用来确保jquery ui核心可用的方法:if (!is_admin()) add_action(\'wp_enqueue_scripts\', \'zg_load_scripts\'); function zg_load_scripts(){ wp_enqueue_script(\"jquery-ui-core\"); } 我也试过了wp_enqueue_script(\"jquery-ui-core\", null, null, false, fal