Wordpress - Adding default value to dropdown that links to no page - wordpress

I have the following piece of code which creates a drop down of a number of posts.
<?php
$mypostype = get_posts('post_type=rentals');
if($mypostype) : ?>
<form action="" id="myform">
<label for="myselect">Rentals</label>
<select id="myselect" name="select_post_type" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<?php foreach ( $mypostype as $mypost ) : ?>
<option value="?rentals=<?php echo $mypost->post_name; ?>"><?php echo $mypost->post_title; ?></option>
<?php endforeach; ?>
</select>
</form>
<?php endif ?>
This is working brilliantly apart from one thing and that is being able to add a default value to the drop down list that links to no page, some text like 'Select Rental'.
It would be great if someone could explain how to add this value in.
Thanks for your time and help in advance.

By adding an <option> in the <select> and a slight change in the javascript onChange event by checking if the selected option value is not empty :
<?php
$mypostype = get_posts('post_type=rentals');
if($mypostype) : ?>
<form action="" id="myform">
<label for="myselect">Rentals</label>
<select id="myselect" name="select_post_type" onChange="((this.options[this.selectedIndex].value != '') ? document.location.href=this.options[this.selectedIndex].value : false);">
<option value="" selected="selected">Select Rentals</option>
<?php foreach ( $mypostype as $mypost ) : ?>
<option value="?rentals=<?php echo $mypost->post_name; ?>"><?php echo $mypost->post_title; ?></option>
<?php endforeach; ?>
</select>
</form>
<?php endif ?>
This should satisfy your needs...
By the way you could do like this to echo the post links (absolute links):
<option value="<?php echo get_permalink( $mypost->ID ); ?>"><?php echo $mypost->post_title; ?></option>

Related

Gravity Form on WordPress

I am trying to pull dynamic form id to create a dropdown for the end-user. This I have done here.
Now, I'd like to pass that id so that a different form shows up based on selection as shown below:
<form class="ui form">
<select name="select" class="ui fluid search selection dropdown">
<?php $myforms = RGFormsModel::get_forms(); foreach ($myforms as $form) { ?>
<option name="select" value="<?php echo $form->id ?>"> <?php echo $form->title ?> </option>
<?php } ?>
</select>
</form>
<?php echo gravity_form($form, true, true, false, '', true, 1); ?>
Here's what I might do to make a shortcode for gravity forms that pulls the ID from the URL parameter.
function return_gform_embed(){
$form_id = $_GET["form_id"];
return do_shortcode('[gravityform id="'.$form_id.'" title="false" description="false"]');
}
add_shortcode('form-url', 'return_gform_embed');
You'd add the shortcode [form-url] to the page where you'd like the form to display.
So now the URL with ?form_id=6 would load the gravity form with id 6. I didn't test the whole javascript aspect but assuming you can change the URL and force a reload things should work out ok.
Here is what I ended up doing if it helps someone else out there:
<form class="ui form">
<select name="select" id="select" class="ui fluid search selection dropdown" onchange="location = this.options[this.selectedIndex].value;">
<?php
function return_gform_embed(){
$form_id = $_GET["form_id"];
return do_shortcode('[gravityform id="'. $form_id .'" title="false" description="false"]');
}
add_shortcode('form-url', 'return_gform_embed');
?>
<?php $myforms = RGFormsModel::get_forms(); foreach ($myforms as $form) { ?>
<option value="/prevention/sessions/?form_id=<?php echo $form->id ?>"> <?php echo $form->title ?> </option>
<?php } ?>
</select>
</form>
<?php echo do_shortcode('[form-url]') ?>

How to add a text before the sorting option in woocommerce

I've been looking around the net but all they have is to teach you how to add/edit/remove an option in the sorting dropdown. But my template requires me to add a 'Sort By' text. Like this:
I tried adding it via Jquery but it keeps being gone after ajax filters.
Please don't rank this down, there might be someone who forgot that there is a format file for this in themes/woocomerce/loop/orderby.php
Then just edit the code like this:
<form class="woocommerce-ordering" method="get">
<span class="sort-name">Sort By</span>
<select name="orderby" class="orderby">
<?php foreach ( $catalog_orderby_options as $id => $name ) : ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>>
<?php echo esc_html( $name ); ?>
</option>
<?php endforeach; ?>
</select>
<input type="hidden" name="paged" value="1" />
<?php wc_query_string_form_fields( null, array( 'orderby', 'submit', 'paged', 'product-page' ) ); ?>

How do I search my WordPress site pages with search box

How I search my WordPress site pages with search box I placed in my site?
Currently I am using this code.
<form id="searchform" method="get" action="<?php bloginfo('siteurl')?>/" style="margin-left: -43px;">
<input type="text" name="" id="s" class="textbox" placeholder="Search" required/>
<input id="btnSearch" type="submit" name="submit" value="<?php _e('Search'); ?>" />
</form>
I used name="s" but it not do search properly. I want that if user right page1 name search box take it to siteurl/page1 and so for page2 and remaining page.
Thanks in advance
Try this :
function redirect_search() {
if (is_search() && !empty($_GET['s'])) {
wp_redirect(home_url("/").urlencode($_GET['s']));
exit();
}
}
add_action('template_redirect', 'redirect_search' );
Add above code in your functions.php file.
So now when you type something, it will redirect it to that page.
NOTE:If you type something related to the published post, it will redirect to that POST instead of what have you entered.
For example : If I type hello in search box, then it will redirect you to hello world(which is default post that comes during installation of wordpress).
This is what have for you.
<div id="content" class="textbox <?php echo of_get_option('blog_sidebar_pos') ?>">
<?php _e('Search for:',''); ?> "<?php the_search_query(); ?>"
<?php
if (have_posts()) : while (have_posts()) : the_post();
$format = get_post_format();
get_template_part( 'include/'.$format );
if($format == '')
get_template_part( 'include/standard' );
endwhile; else:
?>
<?php echo '<p><strong>' . __('There has been an error.', '') . '</strong></p>'; ?>
<p> <?php _e('return to home page', ''); ?> <?php _e('use the search .', ''); ?></p>
<?php get_search_form(); ?>
</div><!--no-results-->
<?php endif; ?>
<?php get_template_part('include/post'); ?>

Filter Categories in Wordpress framework

I am working on a project where I have to create two dropdown menus and populate them with categories; subcategories actually.
I have two main categories and I want to populate the menus with their subcategories comparing the parents.
<select>
<option data-filter="*"><?php echo __('All', 'Avada'); ?></a></option>
<?php foreach($portfolio_taxs as $portfolio_tax_slug => $portfolio_tax_name): ?>
<?php if($portfolio_category = 'taste') ?>
<?php { ?>
<option data-filter=".<?php echo $portfolio_tax_slug; ?>" ><?php echo $portfolio_tax_name; ?>
<?php } ?>
</option>
<?php endforeach; ?>
</select>
Nothing seems to be working.
Help please!
Actually I managed to do what I wanted. The solution was to get the terms of the taxonomy:
$ideas_terms = get_terms('portfolio_category', array
( 'orderby' => 'count',
'hide_empty=> 0,
'parent'=>13
)
);
This returns an array with all element with
parentID == 13
Then to call the other elements of the array you have to use:
<?php foreach($ideas_terms as $term): ?>
<option data-filter=".<?php echo $term->slug; ?>" href="#">
<?php echo $term->name; ?>
</option>
<?php endforeach; ?>

Display the stored selectbox value in the selectbox after refresh on a WordPress options page

I have created a WordPress options page and on that page I want a selectbox in which I can select a page from my website. The following options code does that perfectly! I select a page, hit the save button and it stores the correct value.
My problem is that when I save my page (or refresh it) the selectbox does not show the selected and stored value, but the first in the list. How can I make it so that the checkbox shows the stored value?
<?php break; case 'select_pages': ?>
<div class="options_input">
<label class="option_title" for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label><br>
<select name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>">
<option value=""><?php echo esc_attr( __( '— Select —' ) ); ?></option>
<?php
$page_selected = $options[$valueid];
$pages = get_pages();
foreach ( $pages as $page) :
?>
<option value="<?php echo $page->post_title; ?>" <?php if ( $page_selected && in_array( $page->post_title, $page_selected ) ) { echo 'selected="selected"'; }?>><?php echo $page->post_title; ?></option>
<?php endforeach; ?>
</select>
<?php $cc_responsive_theme_options = get_option('cc_responsive_theme_options'); echo $cc_responsive_theme_options['footer_link_1']; ?>
<span class="option_desc"><?php echo $value['desc']; ?></span>
</div>
Accidentally found the solution. The checkbox was set to multiple and it did not compare the stored value to the one in the selectbox.
This line:
<option value="<?php echo $page->post_title; ?>" <?php if ( $page_selected && in_array( $page->post_title, $page_selected ) ) { echo 'selected="selected"'; }?>><?php echo $page->post_title; ?></option>
has to be changed to this:
<option value="<?php echo $page->post_title; ?>" <?php if( $page_selected == $page->post_title) { echo 'selected="selected"'; }?>><?php echo $page->post_title; ?></option>

Resources