check wordpress select box value - wordpress

How to check if a select box has a value or not in wordpress by php?
I want to check if a select box has a value and if it has then check the value against $post array generated by for-each loop.
Is there any built in wordpress function?
Thanks.

We can use php function in wordpress actions for example:
<select name="select_value">
<option value="">Default value (key thing is leaving value="")</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
//check in the action or filter for the drop-down value:
if($_POST['select_value'] != '') {
//do something
echo $_POST['select_value'];
} else {
//if not post any value
}

This function may be helpful for you:
selected( $selected, $current, $echo);
$selected (required) = One of the values to compare.
Default: None
$current (optional) = The other value to compare if not just true.
Default: true
$echo(boolean) (optional) = Whether to echo or just return the string.
Default: true
Ref.link: https://codex.wordpress.org/Function_Reference/selected may be helpful for you.

Related

Strange error filtering custom post type by acf fields in admin

I'm using ACF Pro in Wordpress to define the fields for a custom post type. In the admin, I'm trying to add dropdowns to filter my custom post type in the admin page by various fields and I'm getting a very strange error. When I create a dropdown containing all possible values for that field, every other one that is created does not print out the <select> portion of the dropdown. It only prints out the options.
So if I add three filters, size, color and type, and then inspect the html of the admin page near the filter button, it looks like this:
<select name="filter_size">
<option value="">All Sizes</option>
<option value="small">Small</option>
<option value="large">Large</option>
</select>
<option value="">All Colors</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<select name="filter_type">
<option value="">All Types</option>
<option value="short">Short Sleeve</option>
<option value="long">Long Sleeve</option>
</select>
It refuses to print the second open and close select tags. If I add a forth filter it doesn't work, but the fifth one does. It's only messing up every other filter I add. It doesn't matter what order they are in. The odd numbered ones work, the even ones don't. I know the query is good because each one works individually and even for the ones that aren't display correctly, it is returning all of the values. It just seems to be the select that isn't being printed.
Here is the code I'm using:
if (is_admin()) {
add_action('restrict_manage_posts', function ($post_type) {
if ('my-cpt' == $post_type) {
buildAdminDropdownFilter($post_type, 'size', "All Sizes");
buildAdminDropdownFilter($post_type, 'color', "All Colors");
buildAdminDropdownFilter($post_type, 'type', "All Types");
}
});
});
function buildAdminDropdownFilter($postType, $metaKey, $label) {
global $wpdb;
$query_values = $wpdb->get_results(
$wpdb->prepare("SELECT distinct meta_value ".
" FROM $wpdb->posts, $wpdb->postmeta ".
" WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id ".
" AND $wpdb->posts.post_type = %s ".
" AND $wpdb->postmeta.meta_key = %s".
" ORDER BY $wpdb->postmeta.meta_value", $postType, $metaKey));
echo '<select name="filter_'.$metaKey.'">';
echo '<option value="">'.$label.'</option>';
$current = $_GET['filter_' . $metaKey] ?? '';
foreach($query_values as &$data) {
echo '<option value="'.$data->meta_value.'">'.ucwords($data->meta_value).'</option>';
}
echo '</selected>';
}
Any ideas what's going on? I'm stumped!
I found it with the help of a co-worker. It was an incredibly stupid little typo. For my closing select tag, I have /selected rather than /select.

Initiate a template iteration in Meteor

Rather than loading a new template, is there a way to force Meteor to initiate an iteration (using {{#each}}) of an array in Meteor? For example, if the user selects a value in a pull down selector, to then initiate Meteor to iterate through an array within that template to populate another multiple selector list rather than load a whole new template with the new selector list.
Let's say this is within a template:
.
.
.
<form class="input-field col s6 card-selector">
<select multiple">
<option value="" disabled selected>Select Students</option>
{{#each StudentList1}}
<option value= '{{FullName}}'>{{formatName FullName}} ({{Level}}) {{RoomWk1}}</option>
{{/each}}
</select>
</form>
.
.
.
When a user selects a value in a different selector in that template:
<select class="week-selector">
<option value="" disabled selected>Week</option>
<option value="Week1">Week 1</option>
<option value="Week2">Week 2</option>
<option value="Week3">Week 3</option>
<option value="Week4">Week 4</option>
<option value="Week5">Week 5</option>
</select>
it will force a reiteration of the first #each to:
<form class="input-field col s6 card-selector">
<select multiple">
<option value="" disabled selected>Select Students</option>
{{#each StudentList1}}
<option value= '{{FullName}}'>{{formatName FullName}} ({{Level}}) {{RoomWk2}}</option>
{{/each}}
</select>
</form>
It would be more efficient than loading a new template that's the same except for the multi selector values.
Sessions are reactive and you can achieve this by using sessions (check if you have session package).
//we don't want the session value from the previous search/events
Template.templateName.onRendered(function(){
Session.set('sessionName', undefined);
});
//I'd probably use onDestroyed instead of onRendered
Template.templateName.onDestroyed(function(){
Session.set('sessionName', undefined);
});
//template events
'change .week-selector': function(){
var selected = $('.week-selector').find(":selected").text();
Session.set('sessionName', selected)
}
//template helper
StudentList1: function(){
var session = Session.get('sessionName');
if(session !== undefined){
//return some documents using the session value in your find()
} else {
//return documents without session value
}
}
EDIT: I found .text() of the selected option in the event but you are free to return value or do whatever you want with the found value/text.

Get value from a select option and echo content in the header in WordPress

In my Theme Options I am allowing users to set page for the 404 template. (which means they can create any normal page and choose that page as the 404 Template). For this I added the page dropdown using wp_dropdown_pages().
Now I want echo noindex,nofollow meta values to the page that the user will choose for the obvious SEO reasons. I tried with this but it does not work.
function noindex_404(){
global $post;
$option = $_POST['404_id'];
// $post_val = '2';
if ($post->ID == $option){
//if ($post->ID == $post_val)
// if I try with the direct post value, it's working
echo '<meta name="robots" content="noindex,nofollow">';
}
}
add_action('wp_head', 'noindex_404', 1);
The following is the HTML created by wp_dropdown_pages() and Page 2 is selected under the option name of 404_id with the option value of 2
<select name="404_id" id="404">
<option value="1">Page 1</option>
<option value="2" selected="selected">Page 2</option>
<option value="3">Page 3</option>
<option value="4">Page 4</option>
<option value="5">Page 5</option>
</select>
you can check your 404 page using this condition.
if ( is_404() ) {
echo '<meta name="robots" content="noindex,nofollow">';
}
In this case you are not bound to any specific page. what ever the page is used for 404 this function will check if it is 404 page then your code will be echo out otherwise not.
cheers!

Stop form dropdown list from refreshing and losing index

I have a form, that includes a drop down list, and a submit button.
The drop down list has 5 values.
Let's say the user chooses value #3, then clicks submit. How would I prevent the drop down list from setting itself back to value #1?
Thanks!
use your serverside script to set selected="selected" for the selected <option>.
After your Submit your side should know what was submitted.
You can tell your DropDownList what was seleced befor by setting the seleced Value as selected.
Example:
<select name='myselect'>
<option value='1' <?php if($myselect == '1') { echo 'selected'; } ?>>#1</option>
<option value='2' <?php if($myselect == '2') { echo 'selected'; } ?>>#2</option>
<option value='3' <?php if($myselect == '3') { echo 'selected'; } ?>>#3</option>
</select>

jquery reset conditional filter

I have 2 dropdown lists on my webform and using jquery trying to filter/reset filter 2nd dropdown elements based on 1st dropdown selection.
$(document).ready(function()
{
$('#dropdown1').change(function(e)
{
switch ($(this).val())
{
case "4":
//this removal works
$('#dropdown2').filter(function()
{
return ($(this).val() == 16);
}).remove();
break;
.................
default:
//how would I restore filter here?
}
}
});
Removing part works, so it filters item with no problem, but I have difficulty restoring the filter on dropdown 2 if something else is chosen in dropdown 1. I was trying to use .hide() and .show() instead of .remove() but it doesn't seem to work on IE6 at least.
At the start of your document ready take a copy of the values in dropdown2 like this:
var drp2values = $('#dropdown2').html();
then whenever you want to reset the values in dropdown2 to its original state do this:
$('#dropdown2').html(drp2values);
The actual value in the var will be something like :
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="16">16</option>
just tried it:
This code works:
$(document).ready(function()
{
var drp2values = $('#dropdown2').html();
$('#dropdown1').change(function(e)
{
switch ($(this).val())
{
case "4":
//this removal works... now ;)
$('#dropdown2').find('option').filter(function()
{
alert('in4 filt' + drp2values + $(this).val());
return ($(this).val() == 16);
}).remove();
break;
default:
//how would I restore filter here?
$('#dropdown2').html(drp2values);
}
});
});
With this HTML
<BODY>
<select id='dropdown1'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id='dropdown2'>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="16">16</option>
</select>
</BODY>
The original code you posted where you said the remove worked, it didnt remove the individual option with value 16, it removed the entire dropdown as you were not geting the options before you filtered, you were removing the dropdown :)
Hope this helps.

Resources