Bonjour,

Je fais une page d'options à partir du tutoriel Wordpress avec plusieurs champs dont une image. Tout fonctionne bien saus que je n'arrive pas à récupérer les anciennes valeurs avec les nouvelles.
Pourtant, elles sont bien enregistrées dans ma base de données.

Je vous remercie pour votre aide

Voici mon code :

<?php
function ajouter_bootstrap()
{
    wp_enqueue_style('bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css');
    wp_enqueue_script('bootstrap', get_template_directory_uri() . '/assets/js/bootstrap.bundle.min.js', array('jquery'), '3.3.7', true);
}
add_action('admin_enqueue_scripts', 'ajouter_bootstrap');
class SponsorMenuPage
{

    const GROUP = 'sponsor_options';

    public static function register()
    {
        add_action('admin_menu', [self::class, 'addMenu']);
        add_action('admin_init', [self::class, 'registerSettings']);
    }

    public static function registerSettings()
    {
        register_setting(self::GROUP, 'sponsor_slider_input1');
        register_setting(self::GROUP, 'sponsor_slider_input2');
        register_setting(self::GROUP, 'sponsor_slider_textarea1');
        register_setting(self::GROUP, 'sponsor_slider_image');

        add_settings_section('sponsor_options_section', 'Paramètres', function () {
            echo "Vous pouvez ici gérer les paramètres liés aux sponsors";
        }, self::GROUP);

        add_settings_field('sponsor_options_slider', "Description", function () {
            $image_url = get_option('sponsor_slider_image');
            wp_enqueue_media();

?>
            <form class="row g-3 needs-validation" novalidate>
                <div class="col-md-6">
                    <label for="sponsor_slider_input1" class="form-label">Titre</label>
                    <input type="text" class="form-control mb-3" id="validationCustom01" required name="sponsor_slider_input1" value="<?= get_option('sponsor_slider_input1') ?>">
                    <div class="valid-feedback">
                        Veuillez saisir le titre
                    </div>
                </div>
                <div class="col-md-6">
                    <label for="sponsor_slider_input2" class="form-label">URL du site</label>
                    <input type="text" class="form-control mb-3" id="validationCustom02" required name="sponsor_slider_input2" value="<?= get_option('sponsor_slider_input2') ?>">
                    <div class="valid-feedback">
                        Veuillez saisir l'adresse du site web
                    </div>
                </div>

                <div class="col-md-6">
                    <label for="sponsor_slider_textarea1" class="form-label">Description</label>
                    <textarea class="form-control mb-3" id="validationCustom03" required name="sponsor_slider_textarea1"><?= get_option('sponsor_slider_textarea1') ?></textarea>
                    <div class="invalid-feedback">
                        Veuillez saisir une description
                    </div>
                </div>
                <div class="col-md-6">
                    <input type="hidden" name="sponsor_slider_image" id="sponsor_slider_image" value="<?php echo esc_attr($image_url); ?>">
                    <img src="<?php echo esc_attr($image_url); ?>" alt="" style="max-width: 100%;">
                </div>

                <div class="col-12 mt-3">
                    <button type="button" class="btn btn-primary" id="upload_image_button">Choisir une image</button>
                </div>
            </form>

            <?php

            $registered_settings = get_registered_settings();

            foreach ($registered_settings as $setting_key => $setting) {
                $option_value = get_option($setting_key);
                $option_value = json_decode($option_value, true);

                if (is_array($option_value)) {
                    foreach ($options as $option) {
                        $values = isset($option_value[$option]) ? $option_value[$option] : false;
                        if ($values) {
                            foreach ($values as $value) {
                                echo $option . ': ' . $value . '<br>';
                            }
                        }
                    }
                }
            }

            ?>

            <script>
                jQuery(document).ready(function($) {
                    $('#upload_image_button').click(function(e) {
                        e.preventDefault();
                        var custom_uploader = wp.media({
                            title: 'Sélectionner une image',
                            button: {
                                text: 'Choisir une image',
                            },
                            multiple: false
                        });
                        custom_uploader.on('select', function() {
                            var attachment = custom_uploader.state().get('selection').first().toJSON();
                            $('#sponsor_slider_image').val(attachment.url);
                        });
                        custom_uploader.open();
                    });
                });
            </script>

        <?php
        }, self::GROUP, 'sponsor_options_section');
    }

    public static function addMenu()
    {
        add_options_page("Gestion des sponsors", "Sponsor", "manage_options", self::GROUP, [self::class, 'render']);
    }

    public static function render()
    {
        ?>
        <h1>Gestion des sponsors</h1>

        <form action="options.php" method="post">
            <?php
            settings_fields(self::GROUP);
            do_settings_sections(self::GROUP);
            submit_button();
            ?>
        </form>
<?php
    }
}

Aucune réponse