Learning my first php search form, be gentle!
I'm looking to fill the 'value' with the number input on the search form. Everything I have tried either breaks the page or has no effect.
I'm trying to input my $input_price into the value part of my argument.
Here is my search form
<form method="post" action="http://mysite/searchresults">
<fieldset>
<!-- TEXT INPUT SELECTION -->
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<!-- END TEXT INPUT SELECTION -->
<input type="text" name="input_price"/>
<input type="submit" id="searchsubmit" value="Search" />
</fieldset>
</form>
And here is the receiving page
<?php
$args['meta_query'][] = array(
'key' => 'price',
'value' => '($input_price)',
'type' => 'numeric',
'compare' => '<=',
'order' => 'DSC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
Thanks!
Firstly, your data is being sent to the receiving script via the POST HTTP method, so you can access those variables with PHP's $_POST superglobal using the element's name attribute as the key:
$input_price = $_POST['input_price'];
Next, the brackets around $input_price should not be used here. Finally, single quotes do not parse variables inside them, as in this example:
'value' => '($input_price)',
But double quotes will, or simply not using quotes at all:
'value' => "$input_price",
'value' => $input_price,
Related
I am trying to create a button "Approved" to change the post category from it's current one to the "approved" category. I don't mind if it reloads the page or not. I would also like to redirect the page afterward to the next post in the original category.
I have found some questions on this already but am ultimately lost on how to get this all together and working.
<?php add_shortcode('approved_button', 'brist_approved_button_function');
function brist_approved_button_function() {
ob_start(); ?>
<form method="post" action="approved.php">
<input type="submit" value="Approved" name="submit"> <!-- assign a name for the button -->
</form>
<?php
wp_set_object_terms( $post_id, intval( $_POST['approved'] ), 'category', false );
$output = ob_get_clean();
return $output;
}?>
I figured it out. Though it was a head-scratcher for me. If anyone else if having the same issue, note the code below. You have to create the select and then have the 'selected' option on the category you want the post to change to. Then, in the CSS, hide the select input, only leaving the button.
<?php add_shortcode('approved_button', 'approved_button_function');?>
<?php function approved_button_function() { ob_start();?>
<div class="approval">
<form action="" id="update-post" method="post">
<?php wp_dropdown_categories( "selected='categoryId'&exclude=21&class=approval-select&show_count=1&hierarchical=1&orderby=name&order=ASC&&hide_empty=0&show_option_all=Choose An Option" ); ?>
<input class="approval-button" type="submit" name="submit" value="Approve" />
</form>
</div>
<?php if ( array_key_exists('cat', $_POST )) {
global $post;
$post_id = $post->ID;
wp_set_object_terms( $post_id, intval( $_POST['cat'] ), 'category', false );
} ?>
I'm trying to add terms to my custom taxonomy from a front-end form. I managed to create the form populate the select by values from another custom post type's posts. But I think that my form handler is not working... When I hit submit the form just takes me to "http://therealaddress.com/wp-admin/admin-post.php" and nothing happens (no new term to the taxonomy, no forwarding to the page I want)... (and yes, I changed therealaddress.com to hide my real address, it's not the wrong address :D)
I would like to forward the user back to the page where the form is after the submission.
My form handling part looks like this in my themes funtions.php
function horse_taxonomy_adder() {
// splitting the select options value from "horses-real-name:Horses Real Name" to "horses-real-name" and "Horses Real Name"
$horsename = ( $_POST['horsename'] );
$hn_slug = split(':', $horsename)[0];
$hn_name = split(':', $horsename)[1];
wp_insert_term(
'hn_name', // the term
'hevoset', // the taxonomy
array(
'slug' => $hn_slug,
)
);
// add the admin notice
$admin_notice = "success";
// redirect the user to the page called "Hallinta"
$page = get_page_by_title('hallinta');
$this->wp_redirect(get_permalink($page->ID));
exit;
}
add_action('admin_post_add_horsetaxonomy','horse_taxonomy_adder');
My form looks like this:
<form action="http://therealaddress.com/wp-admin/admin-post.php" method="POST">
<input type="hidden" name="action" value="horse_taxonomy_adder">
<div class="form-group">
<label for="exampleInputEmail1">Valitse hevonen jolle päiväkirja luodaan</label>
<select name="horsename">
<?php
global $post;
$args = array( 'numberposts' => -1, 'post_type' => 'hevonen');
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<? echo $post->post_name; ?>:<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" name="submit" class="btn btn-primary">Lisää päiväkirja</button>
</form>
I'm trying to add a form with hidden inputs into the post content using external PHP fle.
When i try it from the browser the form added successfully but when i try it from command line, the form inputs are deleted.
Here is my code:
require('../wp-load.php');
$content = '<div class="buy-preowned">
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
<input name="CPOvehicle" type="text" value="1"/>
<input name="VIN" type="hidden" value="5YJSA1E1XHF210809"/>
<input name="vehicleMapId" type="hidden" value="1280359"/>
<input name="titleStatus" type="hidden" value="NEW"/>
<input name="form_id" type="hidden" value="tesla_cpo_marketing_buy_form"/>
</div>
</form>
<p class="small-text">
Requires a $2,500 deposit
</p>
</div>
';
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
When i checked the database, the form is stored as:
<form accept-charset="UTF-8" action="https://www.tesla.com//order?redirect=no" id="tesla-cpo-marketing-buy-form" method="post">
<div>
</div>
</form>
this is only happen when i rub the script from command line, any idea how to resolve it.
Thank you
It s because of built-in security filters. As it is not normal to store some form data inside content(there are shortcodes for that), WP disabled inserting some non-text tags.
But anyway, you can enable it manually.
kses_remove_filters();
$post_id = 1;
$data = array(
'ID' => $post_id,
'post_content' => $content,
);
wp_update_post($data);
kses_init_filters();
I installed the Front-End Publishing plugin on my website, and it displays this menu:
FEP menu
I want to remove the 'tags' and 'choose featured image' option from the menu.
Since I want users to upload media only for the post content, I would also like to prevent users from typing words in the content box.
This is the plugin's submission-form.php What code do I have to edit to achive this? (I tried to put all the code in code format but part of the code refused to be put in the code sample box)
<?php
$post = false;
$post_id = -1;
$featured_img_html = '';
if( isset($_GET['fep_id']) && isset($_GET['fep_action']) && $_GET['fep_action'] == 'edit' ){
$post_id = $_GET['fep_id'];
$p = get_post($post_id, 'ARRAY_A');
if($p['post_author'] != $current_user->ID) return 'You don\'t have permission to edit this post';
$category = get_the_category($post_id);
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) );
$featured_img = get_post_thumbnail_id( $post_id );
$featured_img_html = (!empty($featured_img))?wp_get_attachment_image( $featured_img, array(200,200) ):'';
$post = array(
'title' => $p['post_title'],
'content' => $p['post_content'],
'about_the_author' => get_post_meta($post_id, 'about_the_author', true)
);
if(isset($category[0]) && is_array($category))
$post['category'] = $category[0]->cat_ID;
if(isset($tags) && is_array($tags))
$post['tags'] = implode(', ', $tags);
}
?>
This form needs JavaScript to function properly. Please turn on JavaScript and try again!
<div id="fep-new-post">
<div id="fep-message" class="warning"></div>
<form id="fep-submission-form">
<label for="fep-post-title">Title</label><br/>
<input type="text" name="post_title" id="fep-post-title" value="<?php echo ($post) ? $post['title']:''; ?>"><br/>
<label for="fep-post-content">Content</label><br/>
<?php
$enable_media = (isset($fep_roles['enable_media']) && $fep_roles['enable_media'])?current_user_can($fep_roles['enable_media']):1;
wp_editor( $post['content'], 'fep-post-content', $settings = array('textarea_name'=>'post_content', 'textarea_rows'=> 7, 'media_buttons'=>$enable_media) );
wp_nonce_field('fepnonce_action','fepnonce');
?>
<?php if(!$fep_misc['disable_author_bio']): ?>
<label for="fep-about">Author Bio</label><br/>
<textarea name="about_the_author" id="fep-about" rows="5"><?php echo ($post) ? $post['about_the_author']:''; ?></textarea><br/>
<?php else: ?>
<input type="hidden" name="about_the_author" id="fep-about" value="-1">
<?php endif; ?>
<label for="fep-category">Category</label><br/>
<?php wp_dropdown_categories(array('id'=>'fep-category', 'hide_empty' => 0, 'name' => 'post_category', 'orderby' => 'name', 'selected' => $post['category'], 'hierarchical' => true, 'show_option_none' => __('None'))); ?><br/>
<label for="fep-tags">Tags</label><br/>
<input type="text" name="post_tags" id="fep-tags" value="<?php echo ($post) ? $post['tags']:''; ?>"><br/>
<div id="fep-featured-image">
<div id="fep-featured-image-container"><?php echo $featured_img_html; ?></div>
<a id="fep-featured-image-link" href="#">Choose Featured Image</a>
<input type="hidden" id="fep-featured-image-id" value="<?php echo (!empty($featured_img))?$featured_img:'-1'; ?>"/>
</div>
<input type="hidden" name="post_id" id="fep-post-id" value="<?php echo $post_id ?>">
<button type="button" id="fep-submit-post" class="active-btn">Submit</button><img class="fep-loading-img" src="<?php echo plugins_url( 'static/img/ajax-loading.gif', dirname(__FILE__) ); ?>"/>
</form>
For Question # 1:
Add a class to these elements (I have called the class removal)
<label for="fep-tags" class="removal">Tags</label><br/>
<input type="text" name="post_tags" class="removal" id="fep-tags" value="<?php echo ($post) ? $post['tags']:''; ?>"><br/>
<div class="removal" id="fep-featured-image">
<div id="fep-featured-image-container"><?php echo $featured_img_html; ?></div>
<a id="fep-featured-image-link" href="#">Choose Featured Image</a>
<input type="hidden" id="fep-featured-image-id" value="<?php echo (!empty($featured_img))?$featured_img:'-1'; ?>"/>
</div>
Add custom CSS to your theme:
.removal{
display:none !important;
}
For Question # 2:
You can do two things. You can completely remove the user's ability to type text in by removing the textarea altogether in CSS:
#fep-about{
display:none !important;
}
If for any reason you actually need the text box (not sure why). I would add custom Javascript to detect if the user has entered a key or copy and pasted any text. Considering the ID of the textarea element is fep-about:
var ele = document.getElementById('fep-about')
ele.addEventListener('keyup',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
ele.addEventListener('onchange',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
Here is the JSFiddle for this: https://jsfiddle.net/a3zsdo88/1/
Compatibility Issues
If there are some sort of issues in CSS classes overriding the above ones, just add the display statements inline with the elements:
<div id="fep-featured-image" style="display:none !important;">
<div id="fep-featured-image-container"><?php echo $featured_img_html; ?> </div>
<a id="fep-featured-image-link" href="#">Choose Featured Image</a>
<input type="hidden" id="fep-featured-image-id" value="<?php echo (!empty($featured_img))?$featured_img:'-1'; ?>"/>
</div>
See how I added the display statement to the parent element. Do that for the elements you do not want shown.
If you don't have a way to customize your Javascript easily, then add a <script></script> block directly in your HTML in your form file.
<script>
var ele = document.getElementById('fep-about')
ele.addEventListener('keyup',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
ele.addEventListener('onchange',function(){
this.value = "";
alert("Only direct media uploads are allowed!");
});
</script>
Current Situation
So, this is a follow up question from my previous question (Submit button to affect multiple files)
The approach was good in terms of how to "click" multiple buttons together by using a "super-button" (patent pending) by #oMiKeY. It does what it is supposed to do, clicking all buttons.
The mark up is the following:
Title.php
<form role="form" method="post">
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
<div class="update-button-wrap">
<input id="save_button" type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
</div>
</div>
</form>
Content.php
<form role="form" method="post">
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
<div class="update-button-wrap">
<input id="save_button" type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
</div>
</form>
Article.php
<button type='button' onclick="$('[type="submit"]').click()">Submit</button>
Problem
So, when the "Submit" button is clicked, both buttons in each title.php and content.php are also clicked (ie. in regards to clicking buttons, it works fine). However, because two forms are simultaneously clicked, only the second one is updated (either content or title) while the first one is ignored, when both data are needed to be updated.
My approach
Now, I can merge two files together and have both title and content within a single form, but that really messes up my overall setup and I heard it is better to have multiple smaller php files for updating and speed than a large one big file.
Or here is my another approach.
In the article.php, I will have the form and submit button while the title.php and content.php only has the editable forms. Then these two forms are somehow linked to the form in article.php, like the image below.
Do you think the second approach can be achieved or any other suggestions?
Thanks
Just add a global form on article.php and drop the title and content forms (and submit buttons). Every named input inside the global form will be submitted together no matter what php file generated them.
Edit:
Title.php
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
</div>
Content.php
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
Article.php
<form role="form" method="post">
<?php include "Title.php"; include "Content.php"; ?>
<button type='submit'>Submit</button>
</form>
Option 2:
Alternatively you could use some sort of functionality that allows you to create forms only once, even if you have "inner forms". This way Title.php and Content.php would also work as standalone code snippets.
$formDeep = 0;
function openForm() {
global $formDeep;
if ($formDeep == 0) {
echo "<form role=\"form\" method=\"post\">";
}
$formDeep++;
}
function closeForm() {
global $formDeep;
$formDeep--;
if ($formDeep == 0) {
echo "</form>";
}
}
Title.php
<?php openForm(); ?>
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
</div>
<?php closeForm(); ?>
Content.php
<?php openForm(); ?>
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
<?php closeForm(); ?>
Article.php
<?php openForm(); ?>
<?php include "Title.php"; include "Content.php"; ?>
<button type='submit'>Submit</button>
<?php closeForm(); ?>