0

I am building a function within my plugin where I have the list of available pages being displayed.

What I want is for whenever a user selects a page from the dropdown, it should save the slug of the page as a value in my option name of page_for_logged.

I have tried with the below code, but whenever I click on Save, the selected page is not getting saved in the DB. Also, from the frontend too, it's getting unselected and getting back to its default option. Can anyone please assist with this? Thanks!!

function rudr_slider_page_callback(){
    ?>
    
    <div class="wrap">
    <h1><?php echo esc_html( "Select Page" ); ?></h1>
    </div>
    </br>
    </br>    
    <?php
    print(
        __( 'Homepage for Logged IN user: ' ));
    ?>
    </br>
    </br>

<?php
                $nonceUrl = wp_nonce_url(admin_url('admin.php?page=rudr_slider'));
            ?>
            <form method="post" action="<?php echo esc_url($nonceUrl) ?>" enctype="multipart/form-data">

                <div class="rw">
                    <div class="col60">
                        
                        <input type="hidden" name="action" value="page_for_logged">

                        <p><label style="display: block; margin-bottom: 5px;">How to join call page?</label>
                            <select name="page_for_logged_in" style="border-radius: 5px; min-width: 300px; padding:6px 12px;" required>
                                <option>--</option>
                                <?php 
                                $selectedpagee = get_option('page_for_logged_in');
                                query_posts(array('post_type'=>'page', 'posts_per_page'=>'-1'));
                                if( have_posts() ){
                                    while (have_posts()) {  the_post();

                                        $post_id = get_the_ID();
                                        $pslug = get_post_field( 'post_name', $post_id );
                                        ?>
                                        <option value="<?php echo esc_attr($pslug); ?>" <?php if($selectedpagee == $pslug){ echo esc_attr('selected'); } ?> ><?php the_title(); ?></option>
                                        <?php

                                    }
                                }
                                wp_reset_query();
                                ?>
                            </select></p>

                        <BR>
                        <p><input type="submit" value="Save changes" style="border-radius: 5px; cursor: pointer; background: #000; color: #fff; border:none;padding: 6px 13px;"></p>

                    </div>
                </div>

            </form>

        </div>

    <?php
}
3
  • is there a reason you did not use the selected() function? It's very weird to see esc_attr( "selected" ), you should not be performing the if check and conditional echo yourself. Also you're using query_posts which is not good, avoid this function at all costs, there is never a valid use of query_posts inside a plugin or theme. Use get_posts or WP_Query instead
    – Tom J Nowell
    Commented Dec 1, 2022 at 14:10
  • Also where is the code that saves the value? I can see the function that displays the settings page and retrieves the value, but there is no code for updating page_for_logged_in, is there more to this code that is missing from your question?
    – Tom J Nowell
    Commented Dec 1, 2022 at 14:13
  • Hi @TomJNowell, thanks for your suggestion. I will replace the "query_posts" with the other acceptable functions. In regards to your second comment, actually this is all of my code. I am pretty unaware of how to fetch the currently selected option, and how to update the option in the DB after clicking on Save Changes. I know how to update the option value of Db, but not sure how to do that when anyone clicks on Save button, and update in the DB exact accordingly the user selected. Can you share some idea or a post link maybe? Would be much appreciated. Thanks!! Commented Dec 1, 2022 at 14:24

0