I an building custom plugin so on meantime So on the meantime, I am stuck on frontend form, but currently, I am unable to post or save data to my database.
I have scoured the Google-verse and I have the die() function on the PHP callback and I believe the add_actions are correct.
I am working on a local host, though I doubt that affects it and this is all in the admin, not front end. I also checked that the js is enqueued and localised.
I get a 200 OK message in the chrome developer area.
Maybe someone can help me with a small problem I'm having. I'm trying to use below code to create a submit form for Wordpress post.
<?php
/**
* The Template for displaying car archives, including the main car-data page which is a post type archive.
*
* Override this template by copying it to yourtheme/car_data/archive-car-data.php
*
* #author Shaikh Nadeem
* #package Car_Data
* #subpackage Car_Data/Templates
* #version 1.0.0
*/
/**
*
*/
class Car_Frontend_Form
{
function __construct()
{
add_shortcode( 'submit_car', array($this, 'submit_car') );
add_action( 'wp_enqueue_scripts', array( $this, 'my_enqueue' ) );
add_action( 'save_post_submit_car_data_post', array( $this, 'submit_car_data_post' ) );
add_action( 'wp_ajax_nopriv_submit_car_data_post', array( $this, 'submit_car_data_post' ) );
add_action( 'wp_ajax_submit_car_data_post', array( $this, 'submit_car_data_post' ) );
}
public function submit_car() {
$html = '<div id="submit_car_form">';
$html .= '<input type="hidden" name="author_id" value="<?php echo get_current_user_id(); ?>" />';
$html .= '<form id="cd_car" name="cd_car" class="cd_car" method="post" action=" " >';
$html .= '<p><label for="title">Title</label><br />';
$html .= '<input type="text" id="title" value="" tabindex="1" size="40" name="title" />';
$html .= '</p>';
$html .= '<p>';
$html .= '<label for="content">Post Content</label><br />';
$html .= '<textarea id="content" tabindex="3" name="content" cols="50" rows="6"></textarea>';
$html .= '</p>';
$html .= '<p>'. wp_dropdown_categories( "show_option_none=Category&tab_index=4&taxonomy=feature" ) .'</p>';
$html .= '<p><label for="post_tags">Tags</label>';
$html .= ' <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>';
$html .= wp_nonce_field( "car-frontend-post" );
$html .= '<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit_car" name="submit_car" /></p>';
$html .= '</form>';
$html .= '</div>';
return $html;
}
public function submit_car_data_post() {
// check_ajax_referer( 'ajax_securiy_nonce', 'security' );
if ( isset($_POST['title']) ) {
$title = sanitize_text_field( $_POST['post_title'] );
$content = sanitize_text_field( $_POST['content'] );
$author_id = sanitize_text_field( $_POST['author_id'] );
$tags_input = sanitize_text_field( $_POST['post_tags'] );
$category = sanitize_text_field( $_POST['cat'] );
$args = array(
'post_title' => $title,
'post_content' => $content,
'post_category' => $category,
'tags_input' => $tags_input,
'author' => $author_id,
'post_status' => 'draft',
'post_type' => 'car-data',
);
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'car-frontend-post') ) {
echo '<script>alert("Did not save because your form seemed to be invalid. Sorry");</scipt>';
return;
}
if( is_wp_error( $posts ) ){
echo json_encode( $posts->get_error_messages() );
echo '<script>alert("is_wp_error!");</script>';
} else {
$post_preview = get_preview_post_link($posts);
echo $post_preview;
echo '<script>alert("'. $post_preview .'");</script>';
}
$posts = wp_insert_post( $args );
echo '<script>alert("Saved your post successfully!");</script>';
echo $posts;
wp_die();
}
}
public function my_enqueue() {
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
}
```
Also i have custom post type to save on the same form but till here i not aed that field
Related
I used this code below which is very useful in wordpress function.php for contact form 7 to select images as checkbox but its not receiving in email, please help me in this matter, thanks
Here is the code
function add_shortcode_imageradio() {
wpcf7_add_shortcode( 'imageradio', 'imageradio_handler', true );
}
add_action( 'wpcf7_init', 'add_shortcode_imageradio' );
function imageradio_handler( $tag ){
$tag = new WPCF7_FormTag( $tag );
$atts = array(
'type' => 'radio',
'name' => $tag->name,
'list' => $tag->name . '-options' );
$input = sprintf(
'<input %s />',
wpcf7_format_atts( $atts ) );
$datalist = '';
$datalist .= '<div class="imgradio">';
foreach ( $tag->values as $val ) {
list($radiovalue,$imagepath) = explode("!", $val
);
$datalist .= sprintf(
'<label><input type="radio" name="%s" value="%s" class="hideradio" /><img src="%s"></label>', $tag->name, $radiovalue, $imagepath
);
}
$datalist .= '</div>';
return $datalist;
}
Here is the page I am working on:
https://www.commforceintl.com/lost-and-found-english/
I'm trying to make the default wp_dropdown_categories select dropdown as a radio input with submit button.
This is on the front end.
<form id="filter-select" class="filter-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
<?php wp_dropdown_categories(); ?>
<input type="submit" name="submit" value="view" />
</form>
Then in my functions.php I tried to do this
add_filter( 'wp_dropdown_cats', 'dropdown_filter', 10, 2);
function dropdown_filter( $output, $r ) {
$output = preg_replace( '/<option/i', '<input type="radio"', $output );
$output = str_replace( 'class="level-0"', 'name="filter"', $output );
$output = str_replace( "value=\"{$value}\"", "value=\"{$value}\" selected", $output );
return $output;
}
This actually works in swapping out the select dropbox for a radio field. But it does not actually work correctly.
Instead of going to /category/CATEGORYNAME - Like the default select box does.
It goes /?filter=6&submit=view
Any advice is greatly appreciated, thank you.
Try with a custom Walker to output category names instead of IDs.
<div class="radiobuttons">
<?php
$args = array(
'orderby' => 'name',
'hide_title_if_empty' => true,
'title_li' => '',
'walker' => new List_Categories_Radiobuttons
);
wp_list_categories( $args );
?>
</div>
The custom Walker in functions.php:
/**
* Custom Walker to list categories with radio buttons
*/
class List_Categories_Radiobuttons extends Walker_Category {
function start_el(&$output, $category, $depth=0, $args=array()) {
$category_name = esc_attr( $category->name );
$radiobutton = '<input type="radio" name="filtercategory" value="' . $category_name . '">';
$output .= '<div class="radiobutton">' . $radiobutton;
}
function end_el(&$output, $category, $depth=0, $args=array()) {
$output .= "</div>\n";
}
}
Does this point you in the right direction?
I want to create a widget that will be created in wordpress when the shortcode is activated in a post. I tried to get some things to $all and then return it but didnt work out. I want to do it without using any plugins.
function movie($atts) {
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
global $wpdb;
$b = shortcode_atts(array(
'name' => 'Deadpool',
), $atts);
$vypis = $wpdb->get_row ("SELECT * FROM hodnoceni WHERE nazev = '" . $atts['name'] . "'", ARRAY_A);
$all = $args['before_widget'];
if ( ! empty( $vypis['nazev'] ) )
// This is where you run the code and display the output
$all .= $args['before_title'] . $title . $args['after_title'] . '<p style="margin:0px;">' . $vypis['rok'] . '<br>' . $vypis['delka'] . ' min</p><div class="my-image"><img src="' . $vypis['src'] . '" height="130" alt="' . $vypis['nazev'] . '"> </div> <div class="my-content">' . $vypis['clanek'] . '</div> </div>';
$all .= $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
return $all;
}
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
function register_shortcodes(){
add_shortcode('bacon', 'bacon_function');
add_shortcode('movie_form', 'movie_form');
add_shortcode('movie', 'movie');
}
add_action( 'init', 'register_shortcodes');
For your widget class "wpb_widget" you can use this code:
<?php add_shortcode( 'show_movie', 'movie_shortcode' );
function movie_shortcode( $atts ) {
// Configure defaults and extract the attributes into variables
extract( shortcode_atts(
array(
'type' => 'wpb_widget'
),
$atts
));
$args = array( //optional markup:
'before_widget' => '<div class="box widget scheme-' . $scheme . ' ">',
'after_widget' => '</div>',
'before_title' => '<div class="widget-title">',
'after_title' => '</div>',
);
ob_start();
the_widget( $type, $atts, $args );
$output = ob_get_clean();
return $output;
}
and then use this code
[show_movie]
to run the widget.
I'm very new to coding Wordpress plugins/widgets (this is my first time!). I've built a very basic Wordpress plugin which consists of 2 text inputs and a select field. The text inputs work fine however the select box doesn't appear to be saving when I hit the "Save" button.
Here is my plugin code:
<?php
/* Plugin Name: Sidebar Box
Plugin URI: http://www.website.com
Description: Displays contact box in sidebar
Version: 1.0
Author: JM
Author URI: N/A
*/
// use widgets_init action hook to execute custom function
add_action( 'widgets_init', 'jm_box_widget' );
//register our widget
function jm_box_widget() {
register_widget( 'jm_box_widget_my_info' );
}
//boj_widget_my_info class
class jm_box_widget_my_info extends WP_Widget {
//process the new widget
function jm_box_widget_my_info() {
$widget_ops = array(
'classname' => 'jm_box_widget_class',
'description' => 'Sidebar Box Widget.'
);
$this->WP_Widget( 'jm_box_widget_my_info', 'Box Widget', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array( 'title' => 'Box Page Widget', 'description' => '', 'boxtype' => '' );
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$description = $instance['description'];
$boxtype = $instance['boxtype'];
?>
<p>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<p>Description: <textarea class="widefat" name="<?php echo $this->get_field_name( 'description' ); ?>" / ><?php echo esc_attr( $description ); ?></textarea></p>
<p>Sex:
<select id="<?php echo $this->get_field_id( 'boxtype' ); ?>" name="<?php echo $this->get_field_name( 'boxtype' ); ?>" class="widefat" style="width:100%;">
<option <?php if ( 'box1' == $instance['format'] ) echo 'selected="selected"'; ?> value="box1">box1</option>
<option <?php if ( 'box2' == $instance['format'] ) echo 'selected="selected"'; ?> value="box2">box2</option>
</select>
</p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['description'] = strip_tags( $new_instance['description'] );
$instance['boxtype'] = ( $new_instance['boxtype'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
$description = empty( $instance['description'] ) ? ' ' : $instance['description'];
$boxtype = empty( $instance['boxtype'] ) ? ' ' : $instance['boxtype'];
echo '<div class="sidebar-box" id="' . $boxtype . '" onmouseover="this.style.cursor=\'pointer\'" onmouseup="window.location=\'' . $boxtype . '\'">
<h3>' . $title . '</h3>
<p>' . $description . '</p>
</div>';
echo $after_widget;
}
}
?>
I can't for the life of me workout why it's not saving.
Any help would be greatly appreciated.
Thanks,
James
Somewhere along the line you must have changed $instance['format'] to $instance['boxtype'] but not in the form. The options need changing.
<option <?php if ('box1' == $boxtype )
I have a very strange issue with the wordpress widget. I am editing an existed wordpress widget form Phomedia theme, and I am trying to add one more instance so I can use it to filter posts. In the code it is $instance['custom']. But everytime I hit save the value is dissapearing. I got really stuck with it for several, trying to figure out what is going wrong, but couldn't find it. Here is the code
class MZ_Widget_Latest_Products extends WP_Widget {
/**
* Widget Constuctor
*/
function MZ_Widget_Latest_Products() {
$widget_ops = array( 'classname' => 'widget_mz_wpsc_latest_products','description' => __( 'Phomedia Latest Products Widget', 'wpsc' ) );
$this->WP_Widget( 'wpsc_latest_products', __( 'Phomedia Latest Products', 'wpsc' ), $widget_ops );
}
/**
* Widget Output
*
* #param $args (array)
* #param $instance (array) Widget values.
*/
function widget( $args, $instance ) {
global $wpdb, $table_prefix;
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Products', 'wpsc' ) : $instance['title'] );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
wpsc_phomedia_latest_product( $args, $instance );
echo $after_widget;
}
/**
* Update Widget
*
* #param $new_instance (array) New widget values.
* #param $old_instance (array) Old widget values.
*
* #return (array) New values.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = (int)$new_instance['number'];
$instance['image'] = (bool)$new_instance['image'];
$instance['height'] = (int)$new_instance['height'];
$instance['width'] = (int)$new_instance['width'];
$instance['custom'] = $instance['custom'];
return $instance;
}
/**
* Widget Options Form
*
* #param $instance (array) Widget values.
*/
function form( $instance ) {
global $wpdb;
// Defaults
$instance = wp_parse_args( (array)$instance, array(
'title' => '',
'number' => 4,
'width' => 173,
'height' => 151
) );
// Values
$title = esc_attr( $instance['title'] );
$number = (int)$instance['number'];
$image = (bool)$instance['image'];
$width = (int) $instance['width'];
$height = (int) $instance['height'];
$custom = $instance['custom']; ?>
<p><label for="<?php echo $this->get_field_id('custom'); ?>">Custom: <input class="widefat" id="<?php echo $this->get_field_id('custom'); ?>" name="<?php echo $this->get_field_name('custom'); ?>" type="text" value="<?php echo attribute_escape($custom); ?>" /></label></p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'wpsc' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of products to show:', 'wpsc' ); ?></label>
<input type="text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $number; ?>" size="3" />
</p>
<p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'image' ); ?>" name="<?php echo $this->get_field_name( 'image' ); ?>"<?php checked($image); ?> onclick="jQuery('.wpsc_latest_image').toggle()">
<label for="<?php echo $this->get_field_id( 'image' ); ?>"><?php _e( 'Show Thumbnails', 'wpsc' ); ?></label>
</p>
<div class="wpsc_latest_image"<?php if( !checked( $image ) ) { echo ' style="display:none;"'; } ?>>
<p>
<label for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Width:', 'wpsc'); ?></label>
<input type="text" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" value="<?php echo $width ; ?>" size="3" />
<label for="<?php echo $this->get_field_id('height'); ?>"><?php _e('Height:', 'wpsc'); ?></label>
<input type="text" id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" value="<?php echo $height ; ?>" size="3" />
</p>
</div>
<?php
}
}
add_action( 'widgets_init', create_function( '', 'return register_widget("MZ_Widget_Latest_Products");' ) );
function wpsc_phomedia_latest_product( $args = null, $instance ) {
global $wpdb;
$args = wp_parse_args( (array)$args, array( 'number' => 3 ) );
$siteurl = get_option( 'siteurl' );
$options = get_option( 'wpsc-widget_latest_products' );
$number = isset($instance['number']) ? (int)$instance['number'] : 3;
$image = isset($instance['image']) ? (bool)$instance['image'] : FALSE;
if ( isset($instance['width'] ) )
$width = $instance['width'];
if ( isset( $instance['height'] ) )
$height = $instance['height'];
$latest_products = get_posts( array(
'post_type' => 'wpsc-product',
'numberposts' => $number,
'orderby' => 'post_date',
'post_parent' => 0,
'post_status' => 'publish',
'order' => 'ASC'
) );
$output = '';
$total = count( $latest_products );
$i = 0;
if ( count( $latest_products ) > 0 ) {
$output .= '<div class="grid no-border" ><ul>';
foreach ( $latest_products as $latest_product ) {
//print_r($latest_product);
$i++;
if ( !($i%3)) $class = 'last'; else $class = '';
$output .= '<li class="item '.$class.' ">';
// Thumbnails, if required
if ($image) {
$output .= '<a class="product-image" href="' . wpsc_product_url( $latest_product->ID, null ) . '">';
$attached_images = (array)get_posts( array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $latest_product->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
) );
$attached_image = $attached_images[0];
if ( $attached_image->ID > 0 )
$output .= '<img class="product_image" src="' . wpsc_product_image( $attached_image->ID, $width, $height ) . '" title="' . $latest_product->post_title . '" alt="' . $latest_product->post_title . '" />';
else
$output .='<img class="no-image product_image" id="product_image_'.wpsc_the_product_id().'" alt="No Image" title="'.wpsc_the_product_title().'" src="'.WPSC_URL.'/wpsc-theme/images/noimage.png" width="' . $width . '" height="' . $height . '" />';
$output .= '</a>';
}
// Link
$output .= '<h3 class="product-name">'.stripslashes( $latest_product->post_title ).'</h3>';
$output .= '<p class="product-desc">'. $latest_product->post_content.'</p>';
$output .= '<div class="action">';
$output .= '<div class="price-box left">';
//$output .= $latest_product->ID;
//$output .= wpsc_calculate_price( wpsc_the_product_id(),null,false );
$output .= wpsc_currency_display(get_post_meta( $latest_product->ID, '_wpsc_price', true ));
$output.= '</div>';
$output .= '<a class="button right" href="' . wpsc_product_url( $latest_product->ID, null ) . '"><span>'.__( 'Details', 'wpsc' ).'</span></a>';
$output .= '<div class="clear"></div></div>';
$output .= '</li>';
}
$output .= "</ul></div><div class=\"clear\"></div>";
}
echo $output;
}
UPDATE: Fixed this problem. It didn't save because of wrong class name as a parameter.
$widget_ops = array( 'classname' => 'widget_mz_wpsc_latest_products','description' => __( 'Phomedia Latest Products Widget', 'wpsc' ) );
$this->WP_Widget( 'wpsc_latest_products', __( 'Phomedia Latest Products', 'wpsc' ), $widget_ops );