I am using the following to place a search form in a slider revolution hero slide. However it searches the entire site. I would like it to only search woocommerce products. Any ideas what I need to change to make that work?
<form role="search" method="get" id="searchform" class="revtp-searchform" action="http://mywebsiteURL.com/"/>
<input type="text" value="" name="s" id="s" placeholder="What are you looking for?" />
<input type="submit" id="searchsubmit" value="Find" />
</form>
You can solve it using codex: pre_get_posts and developer: pre_get_posts
add_action( 'pre_get_posts', 'search_woocommerce_product' );
function search_woocommerce_product( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$query->set( 'post_type', 'product' );
}
}
You can add further condition to this hook function like include in particular page only is_page
Or with certain short code like this
$pattern = get_shortcode_regex();
$patternToken = 'masterslider';
if (preg_match_all('/' . $pattern . '/s', get_the_content(), $matches) &&
array_key_exists(2, $matches) &&
in_array($patternToken, $matches[2])) {
//Your code logic
}
Related
I am creating form for wordpress website. Currently i am getting value on same page successfully with blank action.
Now i need to get form value on data.php page.
Form
<form id="post-container" action="<?php echo home_url( '/' ); ?>data.php" method="POST">
<input name="aa" value="post_id"> ........ </input>
<input name="ab" value="post_id"> ........ </input>
<input type="submit" name="submit" value="submit">
</form>
data.php
<?php
if (isset ( $_POST['aa'] )){
$ID = $_POST['aa'] ;
echo get_post_meta( $ID, 'brand', true );
}
if (isset ( $_POST['ab'] )){
$ID = $_POST['ab'] ;
echo get_post_meta( $ID, 'brand', true );
}
?>
After submit
url = http://localhost/mobile/data.php which is correct.
but it display index.php which is wrong and also not print any value.
How to submit form to data.php and print value?
Wordpress have protection from POST, GET requests..
I have some links about it
wordpress-jquery-contact-form-without-a-plugin
create-a-submit-form-in-wordpress
add_query_arg
how-to-submit-a-form-in-wordpress
I have a website with a blog and a custom post_type for Videos (named video). Along with various taxonomies attached to it (Video Categories, Video Tags, etc.)
I am trying to set up a search function to search just the video taxononmy and another to search just the blog taxonomy. There will be a search box in each of these pages to slim down the results.
Here is what I have done so far.
<aside id="sub-search" class="widget widget_search">
<form class="search-form" action="http://www.studiobenna.com/jf/" method="get" role="search">
<label>
<span class="screen-reader-text">Search for:</span>
<input class="search-field" type="search" name="s" value="long" placeholder="Search Videos">
</label>
<input type="hidden" name="post_type" value="video" />
<input class="search-submit" type="submit" value="Search">
</form>
</aside>
Which results in the url ending in: http://example.com/?s=video&post_type=video
But this doesn't filter only the video taxonomy. I have one that references post_type=post for the regular blog search.
What is the correct way to query the Wordpress search function in the URL to only return one post type? I am using the WP Extended Search plugin to allow a search box at the top right of the screem to search the entire site.
I also want these searches to be limited to post type but also pickup any categories and tags attached to them (I don't know if this is any extra step).
An example of what I am doing is here http://www.studiobenna.com/jf/?page_id=8 in the search box next to browse. If you type in Blog here there should be only one result title "Great Western Loop" but others come back.
I have tried adding this to my functions.php:
function mySearchFilter($query) {
$post_type = $_GET['post_type'];
if (!$post_type) {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');
But it doesn't work.
I also tried adding this to the search.php page right before the if (have_posts) loop:
<?php
if(isset($_GET['post_type'])) {
$type = $_GET['post_type'];
$args = array( 'post_type' => $type );
$args = array_merge( $args, $wp_query->query );
query_posts( $args );
}
?>
Still nothing.
Everything is correct except the post_type set of the query.
This will work correctly for your case:
function mySearchFilter( $query ) {
$post_type = $_GET['post_type'];
if (!$post_type) {
$post_type = 'any';
}
if ( $query->is_search ) {
$query->set( 'post_type', array( esc_attr( $post_type ) ) );
}
return $query;
}
add_filter( 'pre_get_posts', 'mySearchFilter' );
I created a custom meta box where you can choose a value from some radio buttons and save it to the post_meta table in the wordpress database. With the following code I save the value:
function save_value_of_my_custom_metabox ($post_id, $post){
$post_id = get_the_ID();
$new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_class( $_POST['the_name_of_the_radio_buttons'] ) : '' );
$meta_key = 'my_key';
update_post_meta( $post_id, $meta_key, $new_meta_value );
}
But if the post will be edited again I want the radio button with the current value to set checked. What is the best way to do that? Here is the function to display the meta box:
function my_custom_meta_box( $object, $box ) {
$post_id=get_the_ID();
$key='my_key';
$the_value_that_should_be_set_to_checked=get_post_meta( $post_id, $key);
//$the_value_that_should_be_set_to_checked[0] returns the value as string
?>
<label for="my_custom_metabox"><?php _e( "Choose value:", 'choose_value' ); ?></label>
<br />
<input type="radio" name="the_name_of_the_radio_buttons" value="value1">Value1<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value2">Value2<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value3">Value3<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value4">Value4<br>
<?php
}
I could write something like if(isset($the_value_that_should_be_set_to_checked[0])=="value of that line") echo "checked='checked'"; in every line but that doesn't seem very elegant to me. Using javascript is also pretty complicated in wordpress because I would have to use the hooks, enqueue the script and just for changing the checked property with one line of javascript it's not worth it. What's the best practice for that?
I am assuming that you are trying to add custom meta box for 'Posts'. Below code will work for you. It will show Radio buttons on add new post or edit post screen. Please read the comments in the code. It will help you in understanding the code.
You can use WordPress's checked function to decide whether to select the radio button or not.
Feel free to ask if you have any doubts.
/**
* Adds a box to the main column on the Post add/edit screens.
*/
function wdm_add_meta_box() {
add_meta_box(
'wdm_sectionid', 'Radio Buttons Meta Box', 'wdm_meta_box_callback', 'post'
); //you can change the 4th paramter i.e. post to custom post type name, if you want it for something else
}
add_action( 'add_meta_boxes', 'wdm_add_meta_box' );
/**
* Prints the box content.
*
* #param WP_Post $post The object for the current post/page.
*/
function wdm_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'wdm_meta_box', 'wdm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, 'my_key', true ); //my_key is a meta_key. Change it to whatever you want
?>
<label for="wdm_new_field"><?php _e( "Choose value:", 'choose_value' ); ?></label>
<br />
<input type="radio" name="the_name_of_the_radio_buttons" value="value1" <?php checked( $value, 'value1' ); ?> >Value1<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value2" <?php checked( $value, 'value2' ); ?> >Value2<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value3" <?php checked( $value, 'value3' ); ?> >Value3<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value4" <?php checked( $value, 'value4' ); ?> >Value4<br>
<?php
}
/**
* When the post is saved, saves our custom data.
*
* #param int $post_id The ID of the post being saved.
*/
function wdm_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( !isset( $_POST['wdm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( !wp_verify_nonce( $_POST['wdm_meta_box_nonce'], 'wdm_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Sanitize user input.
$new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_class( $_POST['the_name_of_the_radio_buttons'] ) : '' );
// Update the meta field in the database.
update_post_meta( $post_id, 'my_key', $new_meta_value );
}
add_action( 'save_post', 'wdm_save_meta_box_data' );
I found a working solution but I think this is not how you should do it. Still open for better solutions ;)
This code was added under the php code from above:
if(isset($$the_value_that_should_be_set_to_checked[0])){
$the_value_that_should_be_set_to_checked= $the_value_that_should_be_set_to_checked[0];
}
else{
$the_value_that_should_be_set_to_checked='';
}
Here's the code that I added below the radiobuttons:
<script type="text/javascript">
jQuery(document).ready(function () {
var checked_value= <?php echo json_encode($the_value_that_should_be_set_to_checked);?>;
if(checked_value!==''){
jQuery("input[name=the_name_of_the_radio_buttons][value="+checked_value+"]").attr('checked', 'checked');
}
});
</script>
P.S.: The $ selector will not work but that maybe depends on the theme you use.
I have Wordpress installed and I have this form on each category page sidebar:
<form name="tags" onChange="document.forms.tags.submit();">
<input type="checkbox" name="tag" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1<br>
<input type="checkbox" name="tag" value="tag2" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag2") { echo "checked";}?>>tag2
</form>
The goal is to create an url like /?tag=tag1+tag2 and so on.
With the above code I get the url like this: /?tag=tag1&tag=tag2.
I've searched for two weeks and tried a lot, but nothing works for me. I've tried for example
<input type="checkbox" name="tag[]" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1
but then i get ?tag%5B%5D=tag1 and Wordpress don't find any results.
The form submit each time a checkbox is checked.
If I use radio input fields, then it works great, because then there's one value each time, but I want to pass multiple values with the same name and render a url like /?tag=tag1+tag2+tag3+tag4 etc.
Can anybody help me with this problem, because I don't know how to get this work for me. Thanks!
this is what you want:
// This would output '/client/?s=word&foo=bar'
echo add_query_arg( 'foo', 'bar' );
take a look on add_query_arg() and get_query_var()
Consider on this example http://codepad.org/s08Jmseg
$url_string = $_SERVER['QUERY_STRING'];
// reuturn: tag=tag1+tag2+tag3
parse_str($url_string);
// $tag holds the value tag1+tag2+tag3
$string = urldecode( $tag );
$arr = explode(" ", $string);
$my_tag = "tag2";
if( in_array( $my_tag, $arr ) ) {
// $key
echo $my_tag . ' found!';
}
else {
echo "Tag Not Found!";
}
//print_r($arr);
From what I read in the net, the following code should be adding a "City" field in the user registration form of Wordpress.
Thing is that, it seems to be not working - I don't see the additional "City" field in the user form.
Any help appreciated.
add_action( 'register_form', 'extended_register_form' );
add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
add_action('user_register', 'myplugin_user_register');
function extended_register_form() {
$city = ( isset( $_POST['city'] ) ) ? $_POST['city']: '';
?>
<p>
<label for="city">City<br />
<input type="text" name="city" id="city" class="input" value="<?php echo esc_attr(stripslashes($city)); ?>" size="25" /></label>
</p>
<?
}
function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {
if ( empty( $_POST['city'] ) )
$errors->add( 'city_error', __('<strong>ERROR</strong>: You must include a city.') );
return $errors;
}
function myplugin_user_register ($user_id) {
if ( isset( $_POST['city'] ) )
update_user_meta($user_id, 'city', $_POST['city']);
}
Since Wordpress 3.0.0, the action to call is "signup_extra_fields" instead of "register_form", so you should use:
add_action( 'signup_extra_fields', 'extended_register_form' );
I tried your code and it worked perfectly, perhaps it is because of the <? in line 12... Try <?php instead...
Where have you added the code? As a plugin or in the functions.php of your theme?