i am new with wordpress.
I have modify search form in wordpress.
<form class="form-inline" role="form" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<div class="form-group">
<input type="search" class="form-control" placeholder="Type your search" value="" name="query" />
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
In this form i have modify name attribute of Search input box name='s' to name='query'.
But after that search is not working .
Do i need to write anything in function.php to Get query string.
Try this code, i hope this will help you to workout,
Add this in theme's fuctions.php file:-
add_filter( 'query_vars', 'my_query_vars' );
function my_query_vars( $query_vars )
{
if ( isset( $_GET['query'] ) && ! empty( $_GET['query'] ) ) {
$_GET['s'] = $_GET['query'];
}
return $query_vars;
}
Related
Custom Wordpress Search Result Page not working and redirects to 404.
Target Url: https://example.com/blog/search/?post_type=post&s=a
// CUSTOM BLOG POST SEARCH FORM
function blog_search_form( $blog_form ) {
$blog_form = '
<form class="form-inline" id="search" action="'.home_url( "/blog/search/" ).'" method="get">
<div class="form-group mx-sm-1 mb-2 w-100">
<input type="hidden" name="post_type" value="post" />
<input class="form-control w-100" id="s" name="s" type="text" placeholder="Search blog" value="' . get_query_var('s') . '" required />
</div>
<input id="searchsubmit" class="main-btn btn mb-2" type="submit" value="Search" />
</form>';
return $blog_form;
}
add_filter( 'get_search_form', 'blog_search_form' );
// CHANGE URL FUNCTION
/function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
I need to set the search that's part of the theme, to search for Products only. At the moment it searches the whole platform.
It's not a custom form I need just the ability to change the default search operation. So rather than going to /s?green, it includes the 'product' code in the URL so it searches only for products.
add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $query ) {
if ( is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
We just want it to show products in their product tiles, rather that blog results.
If you only will ever be searching the product post type on your site, you can create your own searchform.php template to include this hidden field:
<input type="hidden" name="post_type" value="product" />
You can place that anywhere within the form HTML markup. Below is the what WP outputs by default if there is no searchform.php file and you're using the HTML5 form support.
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="post_type" value="product"/>
<label>
<span class="screen-reader-text"><?php _e( 'Search for:', 'label' ); ?></span>
<input type="search" class="search-field" placeholder="<?php esc_attr_e( 'Search …', 'placeholder' ); ?>" value="<?php echo get_search_query(); ?>" name="s"/>
</label>
<input type="submit" class="search-submit" value="<?php esc_attr_e( 'Search', 'submit button' ); ?>"/>
</form>
You can add the above code to your own searchform.php and add that to the root of your THEME directory.
It was within the functions file in the root theme area. We were directed to it, them took it out, placed it in child theme, edited the Form tag in the code, and it worked.
I've reviewed several online docs on this as well as reviewed the post's on this site but still can't seem to get my admin-post requests to work. In my admin menu constructor I add the following actions:
add_action('admin_post_modify_release_request', 'handle_release_posting');
with the following function for the handler:
function handle_release_posting(){
print_r($_POST);
echo "<br/>incoming:" . var_export($_POST, TRUE) . "<br/>";
?> <div class="wrap">
<?php screen_icon(); ?>
<div class="release_response">Posting response goes here.</div>
</div>
<?php
}
My html form element is this:
" method="post">
with other inputs plus...
<input type="hidden" id='action' name="action" value="modify_release_request">
<input type="submit" value="Modify Release"></div>
</form>
The behavior on submission of the request is to call http://joomla.tenasys.lan/wp-admin/admin-post.php
with a completely empty page:
<html><head></head><body></body></html>
What Have I missed? What am I doing wrong?
Somehow I have not managed to use hidden input to pass action parameter, like this:
The alternative solution was add action parameter in URL as this:
<!-- language: lang-html -->
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>?action=add_foobar" method="post">
Here is whole solution:
<!-- language: lang-php -->
function sandbox_show_plugin(){
global $post;
?>
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>?action=add_foobar" method="post">
<input name="name" type="text" placeholder="Enter email">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<?php
}
//Hooks
add_shortcode( 'sandbox_plugin_shortcode', 'sandbox_show_plugin' );
function prefix_admin_add_foobar(){
status_header(200);
var_dump($_POST);
// wp_redirect( 'http://any.url/',302 );
die("Server received '{$_POST['name']}' from your browser.");
}
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );
add_action( 'admin_post_nopriv_add_foobar', 'prefix_admin_add_foobar' ); // this is for non logged users
I've got my own custom registration form, and it allows people to register except for one small issue: the checkboxes don't do anything. When I check the meta_key fields in the database, they don't exist. Everything else is OK though.
I'm trying to have 'comps' and 'newsletter' get added to the meta_key fields (with a value of 1) when they checkboxes are ticked. My form looks like this:
<form id="form" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
<fieldset>
<label for="user-login">Username<br />
<input type="text" name="user_login" id="user-login" class="input" value="" size="20" tabindex="10" />
<label for="user-email">Email address<br />
<input type="text" name="user_email" id="user-email" class="input" value="" size="25" tabindex="20" />
<label for="comps">Competitions<br />
<input type="checkbox" name="comps" id="comps" class="" value="1" tabindex="30" /></label>
<label for="newsletter">Newsletter<br />
<input type="checkbox" name="newsletter" id="newsletter" class="" value="1" tabindex="40" /></label>
<input type="hidden" name="redirect_to" value="<?php echo get_settings('home'); ?>/registration-succeeded"/>
<input type="submit" name="wp-submit" class="button" value="Register Me!" tabindex="100" />
</fieldset>
Is there something I need to do in functions.php of my theme to get these meta_fields to go into the database when the user registers (and when the checkboxes are ticked of course)?
Thanks in advance.
I've worked it out. Just needed to register these extra fields for metadata use:
function at_register_custom_fields( $user_id, $password = "", $meta = array() ) {
// custom fields
$fields = array(
'comps',
'newsletter',
);
// cleans and updates the custom fields
foreach ( $fields as $field ) {
$value = stripslashes( trim( $_POST[$field] ) ) ;
if ( ! empty( $value ) ) {
update_user_meta( $user_id, $field, $value );
}
}
}
add_action( 'user_register', 'at_register_custom_fields' );
I am new to wordpress plug in development. I have designed a search form however I have no idea where to handle and print the submitted form data.
it is a wedget based plug in and the plugin form section code is here:
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$message = apply_filters( 'widget_content', $instance['content'] );
echo $before_widget;
//if ( $title )
// echo $before_title . $title . $after_title;
echo '<div class="shk_location_form_holder">
<span class="shk_loc_title">'.$title.'
<form mthod="post">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form></div>';
echo $after_widget;
if(isset($_REQUEST['shk_inp_search_locations'])){
add_filter('the_content','handle_content');
}
}
In WP plugins you usually have an empty action="" in a form, and handle it in the same function (by the way, as wordpress procedural code becomes very messy, it's better to write plugins using OOP), because, anyway plugins are loaded before any content is outputted in WP (this is the reason why writing ajax plugins is so easy in wp). So you can have everything structured like this:
function draw_form() {
handle_submit();
?>
<div class="shk_location_form_holder">
<span class="shk_loc_title"><?php echo $title; ?></span>
<form mthod="post" action="">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form>
</div>
<?
}
function handle_submit() {
if(isset($_POST['shk_inp_search_locations']) && $_POST['shk_inp_search_locations'] == 'test') {
echo 'you may want to end your program here, especially if it\'s ajax!';
exit;
}
}