I want a select-dropdown field to display the last saved option in the database; I started with this video from YT, https://www.youtube.com/watch?v=hbJiwm5YL5Q&t=3969s and got as far as 1:05:00 and this is my current version
function selectpageHTML() {?>
<select name="lp_actPage">
<? $pages = get_pages();
foreach ( $pages as $page ) {
$slug = $page->post_name;
$title = $page->post_title;
echo "<option value='$slug'" . selected(get_option('lp_actPage', $slug)) .">$title</option>";
} ?>
</select>
<?}
I also looked at Select dropdown not showing selected value php, but instead of comparing floats I'm looking to compare strings.
<?
with<?php
, short tags are deprecated and in newer versions of PHP don't work at all. You should also avoid directly embedding variables into quotes the way that code does with$slug
as it's not possible to apply escaping and is a great way to inject malicious HTML. It should beecho '<option value="' . esc_attr( $slug ) . '" '.