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;
}
}
Related
I have a front end posting to allow users to update their posts. Currently I'm using this to allow entering new tags:
This is my HTML form:
<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="new_image_upload" id="new_image_upload" multiple="false" />
<input type="text" name="newtags" id="newtags" value="" />
<input type="hidden" name="post_id" id="post_id" value="" />
<?php wp_nonce_field( 'new_image_upload', 'new_image_upload_nonce' ); ?>
<input id="submit_new_image_upload" name="submit_new_image_upload" type="submit" value="Upload" />
</form>
This is part of the code:
// If the form has been submitted with new tags
if ( isset( $_POST['newtags'] ) ) {
// get existing tags
$post_tags = get_the_tags();
// concatenates both existing and new tags
$concatenated_tags = array($post_tags, sanitize_text_field($_POST['newtags']));
// Add all the tags to the post
wp_set_post_tags(get_the_ID(), $concatenated_tags , true );
}
The problem with this is that is creating many unecessary tags on my database and I need my users to see the sugestion of tags already present in my database.
So, I need that <input type="text" name="newtags" id="newtags" value="" /> make them existing tags sugestions, just like wordpress does on the post editing backend. See the image bellow to check the desired result on my front-end form:
I would recommend using Select2 javascript library for the frontend, specifically the automatic tokenization tagging functionality. See here: https://select2.org/tagging#automatic-tokenization-into-tags
Once you have enqued the Select2 file into your frontend you could do something like...
Your frontend form:
Note that I have added the $has_tag variable which will automatically select all saved tags for the current post. See selected()
<?php $post_id = get_the_ID(); ?>
<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
...
<input type="hidden" name="post_id" value="<?= $post_id; ?>">
<select name="tags" multiple id="frontend-tagger">
<?php if ( $tags = get_terms( [ 'taxonomy' => 'post_tag', 'hide_empty' => false ] ) ): ?>
<?php foreach ( $tags as $tag ): ?>
<?php $has_tag = selected( has_tag( $tag->term_id, $post_id ), true, false ); ?>
<option value="<?= $tag->name; ?>"<?= $has_tag; ?>><?= $tag->name; ?></option>
<?php endforeach ?>
<?php endif ?>
</select>
...
</form>
JS to initiate the select2 field:
$( '#frontend-tagger' ).select2( {
tags: true,
tokenSeparators: [ ',' ]
} );
Saving process: Note because we are now including already saved tags in the pay load, we do not need to concat existing tags. We can now change the third paramater for wp_set_post_tags to false as we do not need to append tags.
$post_id = !empty( $_POST[ 'post_id' ] ) : (int)$_POST[ 'post_id' ] : null;
if ( isset( $_POST[ 'tags' ] ) ) {
// Sanitize array values
$tags = array_map( 'sanitize_text_field', $_POST[ 'tags' ] );
wp_set_post_tags( $post_id, $tags , false );
}
The above code hasn't been test, just a starting point for you to work from.
Hope this helps!
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 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;
}
I am new to wordpress widget development and learning from tutsplus videos on the go. I am trying to create a simple widget for my website. So far I am following every steps from the video but still I am getting following error and the form is not saving the values.
Warning: extract() expects parameter 1 to be array, null given in C:\Program Files\Ampps\www\test\wp-content\plugins\first\index.php on line 50
This is the from method
public function form($instance){
extract($instance);
?>
<p>
<lable for="<?php echo $this->get_field_id('ap_title_text');?>">Title Text: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" />
</p>
<?php
So far, I have just created the constructor and registered the widget. There was no error displayed till this step in the video. And every website I googled for this problem showed similar steps. I don't understand why it is showing error on my system.
I am using wordpress 3.5.2 downloaded yesterday and using php5.3.
EDIT ::
Here is the code.
class SidebarWidget extends WP_Widget{
function __construct()
{
$options = array(
'description' => 'Simplest way to start working from any page',
'name' => 'Sidebar Widget'
);
parent::__construct('appSidebarWidget', '', $options);
}
public function form($instance)
{
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('ap_title_text'); ?>" >Title Text: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ap_app_name'); ?>" >app Name: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_name');?>" name="<?php echo $this->get_field_name('ap_app_name');?>" value="<?php if(isset($ap_app_name)) echo esc_attr($ap_app_name);?>" />
</p>
<?php
}
public function widget($args, $instance)
{
extract($args);
extract($instance);
$display_gadget = "<iframe src='http://$appURL' width='150px' height='200px' scrolling='auto' frameborder='0' allowtransparency='true'></iframe>";
if(empty($ap_title_text)){$ap_title_text = "Schedule Now";}
echo $before_widget;
echo $before_title.$ap_title_text.$after_title;
echo $display_gadget;
echo $after_widget;
}
}
add_action('widgets_init','appRegisterWidget');
function appRegisterWidget()
{
register_widget('appSidebarWidget');
}
I still haven't been able to get it working. However the actual project for which i was learning this, works as expected. I would still like to know what is wrong with this one. I nearly lost my job because of this.
Also, can anyone guide me to call a custom javascript function from a button on widget being displayed. Basically my purpose is to display a button on the widget rendered by the information on the form. When someone click the button, It should display an overlay with message.
In the following code
add_action('widgets_init','appRegisterWidget');
function appRegisterWidget()
{
register_widget('appSidebarWidget'); <--
}
Change it to
register_widget('SidebarWidget');
I've added a url text box in the widget setup form and it's working, (full modified code given below)
class SidebarWidget extends WP_Widget{
function __construct()
{
$options = array(
'description' => 'Simplest way to start working from any page',
'name' => 'Sidebar Widget'
);
parent::__construct('appSidebarWidget', '', $options);
}
public function form($instance)
{
extract($instance);
?>
<p>
<label for="<?php echo $this->get_field_id('ap_title_text'); ?>" >Title Text: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_title_text');?>" name="<?php echo $this->get_field_name('ap_title_text');?>" value="<?php if(isset($ap_title_text)) echo esc_attr($ap_title_text);?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ap_app_name'); ?>" >App Name: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_name');?>" name="<?php echo $this->get_field_name('ap_app_name');?>" value="<?php if(isset($ap_app_name)) echo esc_attr($ap_app_name);?>" />
</p>
<!-- New text box added -->
<p>
<label for="<?php echo $this->get_field_id('ap_app_url'); ?>" >App Url: </lable>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('ap_app_url');?>" name="<?php echo $this->get_field_name('ap_app_url');?>" value="<?php if(isset($ap_app_url)) echo esc_attr($ap_app_url);?>" />
</p>
<?php
}
public function widget($args, $instance)
{
extract($args);
extract($instance);
$display_gadget = "<iframe src='http://" . $ap_app_url . "' width='150px' height='200px' scrolling='auto' frameborder='0' allowtransparency='true'></iframe>";
if(empty($ap_title_text)){$ap_title_text = "Schedule Now";}
echo $before_widget;
echo $before_title.$ap_title_text.$after_title;
echo $display_gadget;
echo $after_widget;
}
}
add_action('widgets_init','appRegisterWidget');
function appRegisterWidget()
{
register_widget('SidebarWidget');
}
Screen shot of working example given below
The code is crashing because the variable $instance is not an array. The extract() function has to receive an array in order to do it's job. That much is clear and very simple.
The problem is that nobody can help you explain why $instance is null unless you provide the code which calls the form() function. There could be a million reasons why $instance is not an array.
The form() function is being called by the WP_Widgets object.
$instance could also always be null in the form method if you have an empty update function. This may not relate to this question, but please check whether that is the issue if you run into a similar issue.
I have a custom post type for products. I have two TinyMCE editors (the standard and one for a summary field) loading correctly in the Dashboard. From the Dashboard side of things, everything is working correctly. Adds, updates. etc...
On the site though, output is loosing the line breaks (paragraph tags). Here is an example:
http://keg.brettatkin.com/products/complete-consulting-skills-learning-system/
I'm using the wp_editor function for this. (http://codex.wordpress.org/Function_Reference/wp_editor)
Here is my code:
<?php
function keg_product_fields (){
global $post;
$custom = get_post_custom($post->ID);
$keg_product_price = $custom["keg_product_price"][0];
$keg_product_link = $custom["keg_product_link"][0];
$keg_product_type = $custom["keg_product_type"][0];
$keg_product_featured = $custom["keg_product_featured"][0];
$keg_product_summary = $custom["keg_product_summary"][0];
$editor_id = "kegprodsummary"
?>
<p>
<label>Summary:</label><br />
<?php wp_editor( $keg_product_summary, $editor_id, $settings = array('textarea_name' => 'keg_product_summary') ); ?>
</p>
<p>
<label>Price:</label><br />
<input size="10" name="keg_product_price" value="<?php echo $keg_product_price; ?>" />
</p>
<p>
<label>Type:</label><br />
<select name="keg_product_type">
<option value="<?php echo $keg_product_type; ?>" selected="selected"><?php echo $keg_product_type; ?></option>
<option value="Book">Book</option>
<option value="CD">CD</option>
<option value="Downloadable">Downloadable</option>
<option value="Multimedia">Multimedia</option>
<option value="Virtual">Virtual</option>
</select>
</p>
<p>
<label>Link:</label><br />
<input size="65" maxlength="200" name="keg_product_link" value="<?php echo $keg_product_link; ?>" />
</p>
<p>
<input type="checkbox" name="keg_product_featured" value="Yes" <?php if (!(strcmp("$keg_product_featured","Yes"))) {echo "checked=\"checked\"";} ?>/>
<label>Featured Product</label>
</p>
<?php
}
function add_keg_product_box (){
add_meta_box(
"keg_product_info",
"Product Details",
"keg_product_fields",
"keg_products"
);
}
function save_keg_product_attributes ( $post_id )
{
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
global $post;
update_post_meta($post->ID, "keg_product_price", $_POST["keg_product_price"]);
update_post_meta($post->ID, "keg_product_link", $_POST["keg_product_link"]);
update_post_meta($post->ID, "keg_product_type", $_POST["keg_product_type"]);
update_post_meta($post->ID, "keg_product_featured", $_POST["keg_product_featured"]);
update_post_meta($post->ID, "keg_product_summary", $_POST["keg_product_summary"]);
}
add_action ('admin_init', 'add_keg_product_box' );
add_action ('save_post', 'save_keg_product_attributes');
add_action ('publish_post', 'save_keg_product_attributes');
?>
Any ideas here?
Thanks!
Brett
You have to apply the output filters with the apply_filters function in the page where you want to display your summary.
Example:
<?php
$summary = get_post_meta(...);
echo apply_filters('the_content', $summary);
?>
Try using wpautop() when saving the content or when showing it (I recommend you to use it when saving, for performance).
This function adds paragraphs throughout the text depending on the line breaks.