WordPress : Le bouton Enregistrer de l’option Thème ne fonctionne pas
Je travaille actuellement sur la création d’un panneau d’options pour un thème (theme-options.php) sur mon hôte local. Tout allait bien jusqu’à ce que j’essaie de tester le bouton Enregistrer. Après avoir cliqué dessus, j’ai reçu le message indiquant que les options étaient enregistrées mais que rien n’était réellement enregistré. Je ne sais pas si cela compte, mais theme-options.php se trouve dans un dossier nommé « options » dans le répertoire d’accueil du thème. Voici la fonction actuelle que j’utilise pour enregistrer:
//add options page
function fround_add_admin(){
global $themename, $shortname, $options;
if ( $_GET['page'] == basename(__FILE__) ){
if ( 'save' == $_REQUEST['action'] ) {
//protect against request forgery
check_admin_referer('theme-save');
//save the options
foreach ($options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
}
}
header("Location: themes.php?page=theme-options.php&saved=true");
die;
} else if ( 'reset' == $_REQUEST['action'] ) {
//protect against request forgery
check_admin_referer('theme-reset');
//delete the options
foreach ($option as $value) {
delete_option( $value['id'] );
}
header("Location: themes.php?page=theme-options.php&reset=true");
die;
}
}
add_theme_page($themename." Options", "$themename Options", 'edit_themes', basename(__FILE__), 'fround_admin');
}
add_action('admin_menu' , 'fround_add_admin');
Je suis un tutoriel pour créer ce panneau (avec quelques modifications à mes besoins) et tout semble identique à celui du tutoriel. En le parcourant avec les connaissances que j’ai en php, je ne vois rien de mal non plus. Voici l’intégralité du theme-options.php :
<?php
//theme vars
$themename = "f Round";
$shortname = "fround";
$options = array();
function theme_options(){
global $themename, $shortname, $options;
$options = array (
array("name" => "General Settings",
"type" => "section"),
array("type" => "open"),
array("name" => "Type of Logo",
"desc" => "Select your logo type ( Image or Text )",
"id" => $shortname."_type_of_logo",
"type" => "select",
"options" => array("text", "logo"),
"std" => "text"),
array("name" => "Logo Upload",
"desc" => "Upload images using the native media uploader, or define the URL directly",
"id" => $shortname."_logo_upload",
"type" => "imageupload",
"std" => ""),
array("name" => "Logo Text",
"desc" => "Enter text for logo",
"id" => $shortname."_logo_text",
"type" => "text",
"std" => ""),
array("name" => "Logo Slogan",
"desc" => "Enter text for logo slogan",
"id" => $shortname."_logo_slogan",
"type" => "text",
"std" => ""),
array("name" => "Custom Favicon",
"desc" => "Upload a 16px x 16px Png/Gif image that will represent your website's favicon",
"id" => $shortname."_favicon_upload",
"type" => "imageupload",
"std" => ""),
array("type" => "close"),
array("name" => "Styling Options",
"type" => "section"),
array("type" => "open"),
array("name" => "Background Color",
"desc" => "Pick a color for the background",
"id" => $shortname."_background_color",
"type" => "color",
"std" => "#ffffff"),
array("name" => "Body Color",
"desc" => "Pick a color for the body",
"id" => $shortname."_body_color",
"type" => "color",
"std" => "#ffffff"),
array("type" => "close"),
array("name" => "Footer Options",
"type" => "section"),
array("type" => "open"),
array("name" => "Copyright Text",
"desc" => "Enter text for copyright in footer (if empty it will be removed)",
"id" => $shortname."_footer_text",
"type" => "text",
"std" => "Copyright TestSite.com"),
array("type" => "close")
);
}
add_action('init', 'theme_options');
//add options page
function fround_add_admin(){
global $themename, $shortname, $options;
if ( $_GET['page'] == basename(__FILE__) ){
if ( 'save' == $_REQUEST['action'] ) {
//protect against request forgery
check_admin_referer('theme-save');
//save the options
foreach ($options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
}
}
header("Location: themes.php?page=theme-options.php&saved=true");
die;
} else if ( 'reset' == $_REQUEST['action'] ) {
//protect against request forgery
check_admin_referer('theme-reset');
//delete the options
foreach ($option as $value) {
delete_option( $value['id'] );
}
header("Location: themes.php?page=theme-options.php&reset=true");
die;
}
}
add_theme_page($themename." Options", "$themename Options", 'edit_themes', basename(__FILE__), 'fround_admin');
}
add_action('admin_menu' , 'fround_add_admin');
//main function
function fround_admin() {
global $themename, $shortname, $options;
//saved or reset messages
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>';
//form
?>
<header>
<h1><?php echo $themename; ?></h1>
</header>
<aside>
<nav>
<ul>
<?php
foreach ($options as $value){
switch ( $value['type'] ){
case 'section':
echo '<li><a>'.$value['name'].'</li></a>';
break;
}
}
?>
</ul>
</nav>
</aside>
<form method="post">
<?php wp_nonce_field('theme-save'); ?>
<main>
<?php
foreach ($options as $value){
switch ( $value['type'] ){
case 'section':
?>
<section name="<?php echo $value['name'];?>">
<h3><?php echo $value['name'];?></h3>
<?php
break;
case 'open':
break;
case 'close':
?>
</section>
<?php
break;
case 'select':
?>
<div id="<?php echo $value['id']; ?>" class="option <?php echo $value['type']; ?>">
<select>
<?php
foreach ($value['options'] as $option){
echo '<option value="'.$option.'">'.$option.'</option>';
}
?>
</select>
<p><?php echo $value['desc']; ?></p>
</div>
<?php
break;
case 'text':
?>
<div id="<?php echo $value['id']; ?>" class="option <?php echo $value['type']; ?>">
<input type="text" value="<?php echo $value['std']; ?>">
<p><?php echo $value['desc']; ?></p>
</div>
<?php
break;
case 'imageupload':
?>
<div id="<?php echo $value['id']; ?>" class="option <?php echo $value['type']; ?>">
<p><?php echo $value['desc']; ?></p>
</div>
<?php
break;
case 'color':
?>
<div id="<?php echo $value['id']; ?>" class="option <?php echo $value['type']; ?>">
<p><?php echo $value['desc']; ?></p>
</div>
<?php
break;
}
}
?>
</main>
<p class="submit">
<input name="save" type="submit" value="Save changes" class="button-primary" />
<input type="hidden" name="action" value="save" />
</p>
</form>
<form method="post">
<?php wp_nonce_field('theme-reset'); ?>
<p class="submit">
<input name="reset" type="submit" value="Reset changes" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
<?php
}
?>
Qu’est-ce que je fais exactement de mal pour que le bouton de sauvegarde ne fonctionne pas? Toute aide est grandement appréciée.
EDIT: j’ai changé les cas ‘select’ et ‘text’ comme suit (ce qui ne fonctionne toujours pas pour moi):
case 'select':
?>
<div id="<?php echo $value['id']; ?>" class="option <?php echo $value['type']; ?>">
<select>
<?php
foreach ($value['options'] as $key=>$option){
if ($key == get_option($value['id'], $value['std']) ) {
$selected = 'selected="selected"';
} else{
$selected = "";
}
?>
<option value="<?php echo $key; ?>" <?php echo $selected ?>><?php echo $option; ?></option>
<?php } ?>
</select>
<p><?php echo $value['desc']; ?></p>
</div>
<?php
break;
case 'text':
?>
<div id="<?php echo $value['id']; ?>" class="option <?php echo $value['type']; ?>">
<input type="text" value="<?php echo stripslashes( get_option($value['id'], $value['std']) ); ?>">
<p><?php echo $value['desc']; ?></p>
</div>
<?php
break;
Je vais continuer à travailler dessus, mais un peu d’aide serait quand même très appréciée.
Solution n°1 trouvée
J’ai commencé à tout réécrire à l’aide de l’API Settings et tout fonctionne bien maintenant.
0 commentaire