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
}
selected()
function? It's very weird to seeesc_attr( "selected" )
, you should not be performing theif
check and conditional echo yourself. Also you're usingquery_posts
which is not good, avoid this function at all costs, there is never a valid use ofquery_posts
inside a plugin or theme. Useget_posts
orWP_Query
insteadpage_for_logged_in
, is there more to this code that is missing from your question?