How do I create a form inside my back end of widget and display it in a certain area for the end of my widget

Question

I have used the youtube video: (https://www.youtube.com/watch?v=OJdIUU1pjl4&t=1261s) to learn how to create my first custom widget. From here I have create a class with a front end display and a backend display (forms) I want to create a form to post the information, then gets saved to the database that later gets generated on the front end.

I have done this with an options page using update_option() and get_option() however, this would be a hard coded variable and since someone can use my widget in multiple places I would want each one to have a unique storage.

Here is currently what I have with php variables with no values because I am uncertain how to make my form post save the details to the database.

    //Back-end display of widget
    public function form($instance) {
        ?>
        <form method="post">
            <label for="inner-text">Inside Text</label>
            <input name="inner-text" type="text">
            <label for="bg-image">Background Image</label>
            <input name="bg-image" type="image">
        </form>
        <?php
    }
//front end display of widget
public function widget($args, $instance ) {
        echo $args['before_widget'];
        ?>
            <div class="bg-img" style="background: url('<?= $bg_img ?>');">
                <div class="container">
                    <div class="inner-rectangle">
                        <?= $inner_text ?>
                    </div>
                </div>

            </div>
        <?php
        echo $args['after_widget'];

        return;
}

The reason I have refrained from using get_option() is because it is a static variable and can’t have a unique value for each widget.

0
davidb3rn 4 months 0 Answers 16 views 0

Leave an answer