Image as a checkbox option in Contact Form 7 - wordpress

I wanted to use the image as an option to check in contact form 7. I searched and found the way to do that with radio button. I made the changes in the code, and it does work as a checkbox, but it is only sending one value when submitting the form, not the multiple values.
This is the code I am using. Please tell me what needs to be changed.
function add_shortcode_imagecheckbox() {
wpcf7_add_shortcode( 'imagecheckbox', 'imagecheckbox_handler', true );
}
add_action( 'wpcf7_init', 'add_shortcode_imagecheckbox' );
function imagecheckbox_handler( $tag ){
$tag = new WPCF7_FormTag( $tag );
$atts = array(
'type' => 'checkbox',
'name' => $tag->name,
'list' => $tag->name . '-options' );
$input = sprintf(
'<input %s />',
wpcf7_format_atts( $atts ) );
$datalist = '';
$datalist .= '<div class="imgcheckbox">';
foreach ( $tag->values as $val ) {
list($checkboxvalue,$imagepath) = explode("!", $val
);
$datalist .= sprintf(
'<label><input type="checkbox" name="%s" value="%s" class="hidecheckbox" /><img src="%s"></label>', $tag->name, $checkboxvalue, $imagepath
);
}
$datalist .= '</div>';
return $datalist;
}

it's a bit late but I've had this problem right now
in input name add []
$datalist .= sprintf(
'<label><input type="checkbox" name="%s[]" value="%s" class="hidecheckbox" /><img src="%s"></label>', $tag->name, $checkboxvalue, $imagepath
);

Related

Use image as radio button on Contact Form 7 but not receiving image in mail

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/

Wordpress dropdown categories as radio fields

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?

Unable to save frontend custom Post data

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

Metabox Data Isn't Saving

I created a metabox within a class. I needed it for a custom post type that I had. The metabox is displaying fine, but the values refuse to save. I've altered the code several times, but it still isn't working for me. Do you know what I'm doing wrong? Here is the full code:
<?php
class portfolio_metabox {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'details',
__( 'Project Details', 'chic' ),
array( $this, 'render_project_metabox' ),
'portfolio',
'advanced',
'default'
);
}
public function render_project_metabox( $post ) {
// Retrieve an existing value from the database.
$portfolio_web_design = get_post_meta( $post->ID, 'portfolio_web_design ', true );
$portfolio_web_dev = get_post_meta( $post->ID, 'portfolio_web_dev ', true );
$portfolio_digital_art = get_post_meta( $post->ID, 'portfolio_digital_art ', true );
$portfolio_graphic_design = get_post_meta( $post->ID, 'portfolio_graphic_design ', true );
// Set default values.
// Form fields.
echo '<table class="form-table">';
echo '<tr>';
echo '<th><label for="portfolio_project" class="portfolio_project_label">' . __( 'Project Type', 'chic' ) . '</label></th>';
echo '<td>';
echo '<label><input type="checkbox" name="portfolio_web_design " class="portfolio_project_field" value="' . $portfolio_web_design . '" ' . checked( $portfolio_web_design , 'checked', false ) . '> ' . __( ' Web Design', 'chic' ) . '</label><br>';
echo '<label><input type="checkbox" name="portfolio_web_dev " class="portfolio_project_field" value="' . $portfolio_web_dev . '" ' . checked( $portfolio_web_dev , 'checked', false ) . '> ' . __( ' Web Development', 'chic' ) . '</label><br>';
echo '<label><input type="checkbox" name="portfolio_digital_art " class="portfolio_project_field" value="' . $portfolio_digital_art . '" ' . checked( $portfolio_digital_art , 'checked', false ) . '> ' . __( ' Digital Art', 'chic' ) . '</label><br>';
echo '<label><input type="checkbox" name="portfolio_graphic_design " class="portfolio_project_field" value="' . $portfolio_graphic_design . '" ' . checked( $portfolio_graphic_design , 'checked', false ) . '> ' . __( ' Graphic Design', 'chic' ) . '</label><br>';
echo '<p class="description">' . __( 'Project Type', 'chic' ) . '</p>';
echo '</td>';
echo '</tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$portfolio_new_web_design = isset( $_POST[ 'portfolio_web_design' ] ) ? 'checked' : '';
$portfolio_new_web_dev = isset( $_POST[ 'portfolio_web_dev' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_digital_art' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_graphic_design' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'portfolio_web_design ', $portfolio_new_web_design );
update_post_meta( $post_id, 'portfolio_web_dev ', $portfolio_new_web_dev );
update_post_meta( $post_id, 'portfolio_digital_art ', $portfolio_new_digital_art );
update_post_meta( $post_id, 'portfolio_graphic_design ', $portfolio_new_digital_art );
}
}
new portfolio_metabox;
?>
UPDATE:
The code given by Mukesh Panchal worked great. It saves just like I wanted it to, but I still can't get the metadata to display on the template. Any ideas why that's going on?
There's a extra space in your input name="" attributes, like here:
name="portfolio_web_design "
This should be:
name="portfolio_web_design"
After that your isset should work properly ;)
Can you please try below updated code:
<?php
class portfolio_metabox {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'details',
__( 'Project Details', 'chic' ),
array( $this, 'render_project_metabox' ),
'portfolio',
'advanced',
'default'
);
}
public function render_project_metabox( $post ) {
// Retrieve an existing value from the database.
$portfolio_web_design = get_post_meta( $post->ID, 'portfolio_web_design ', true );
$portfolio_web_dev = get_post_meta( $post->ID, 'portfolio_web_dev ', true );
$portfolio_digital_art = get_post_meta( $post->ID, 'portfolio_digital_art ', true );
$portfolio_graphic_design = get_post_meta( $post->ID, 'portfolio_graphic_design ', true );
// Set default values.
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="portfolio_project" class="portfolio_project_label">' . __( 'Project Type', 'chic' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="' . $portfolio_web_design . '" ' . checked( $portfolio_web_design , 'checked', false ) . '> ' . __( ' Web Design', 'chic' ) . '</label><br>';
echo ' <label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="' . $portfolio_web_dev . '" ' . checked( $portfolio_web_dev , 'checked', false ) . '> ' . __( ' Web Development', 'chic' ) . '</label><br>';
echo ' <label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="' . $portfolio_digital_art . '" ' . checked( $portfolio_digital_art , 'checked', false ) . '> ' . __( ' Digital Art', 'chic' ) . '</label><br>';
echo ' <label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="' . $portfolio_graphic_design . '" ' . checked( $portfolio_graphic_design , 'checked', false ) . '> ' . __( ' Graphic Design', 'chic' ) . '</label><br>';
echo ' <p class="description">' . __( 'Project Type', 'chic' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$portfolio_new_web_design = isset( $_POST[ 'portfolio_web_design' ] ) ? 'checked' : '';
$portfolio_new_web_dev = isset( $_POST[ 'portfolio_web_dev' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_digital_art' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_graphic_design' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'portfolio_web_design', $portfolio_new_web_design );
update_post_meta( $post_id, 'portfolio_web_dev', $portfolio_new_web_dev );
update_post_meta( $post_id, 'portfolio_digital_art', $portfolio_new_digital_art );
update_post_meta( $post_id, 'portfolio_graphic_design', $portfolio_new_digital_art );
}
}
new portfolio_metabox;
?>
This should do it:
class portfolio_metabox {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'details',
__( 'Project Details', 'chic' ),
array( $this, 'render_project_metabox' ),
'portfolio',
'advanced',
'default'
);
}
public function render_project_metabox( $post ) {
// Retrieve an existing value from the database.
$post_meta = get_post_meta( $post->ID );
$portfolio_web_design = ( isset($post_meta['portfolio_web_design'][0]) && $post_meta['portfolio_web_design'][0] != '' ) ? $post_meta['portfolio_web_design'][0] : 0;
$portfolio_web_dev = ( isset($post_meta['portfolio_web_dev'][0]) && $post_meta['portfolio_web_dev'][0]!= '' ) ? $post_meta['portfolio_web_dev'][0] : 0;
$portfolio_digital_art = ( isset($post_meta['portfolio_digital_art'][0]) && $post_meta['portfolio_digital_art'][0]!= '' ) ? $post_meta['portfolio_digital_art'][0] : 0;
$portfolio_graphic_design = ( isset($post_meta['portfolio_graphic_design'][0]) && $post_meta['portfolio_graphic_design'][0]!= '' ) ? $post_meta['portfolio_graphic_design'][0] : 0;
// Set default values.
// Form fields.
?>
<table class="form-table">
<tr>
<th><label for="portfolio_project" class="portfolio_project_label"><?php esc_html_e( 'Project Type', 'chic' ) ?></label;></th>
<td>
<label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="1" <?php checked( $portfolio_web_design , 1 ) ?>> <?php esc_html_e( ' Web Design', 'chic' ); ?></label><br>
<label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="1" <?php checked( $portfolio_web_dev , 1 ) ?>> <?php esc_html_e( ' Web Development', 'chic' ); ?></label><br>
<label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="1" <?php checked( $portfolio_digital_art , 1 ) ?>> <?php esc_html_e( ' Digital Art', 'chic' ); ?></label><br>
<label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="1" <?php checked( $portfolio_graphic_design , 1 ) ?>> <?php esc_html_e( ' Graphic Design', 'chic' ); ?></label><br>
<p class="description"><?php esc_html_e( 'Project Type', 'chic' ) ?></p;>
</td>
</tr>
</table>
<?php
}
public function save_metabox( ) {
global $post;
// Sanitize user input.
$portfolio_new_web_design = ( isset( $_POST['portfolio_web_design'] ) && $_POST['portfolio_web_design'] == 1 ) ? 1 : 0;
$portfolio_new_web_dev = ( isset( $_POST['portfolio_web_dev'] ) && $_POST['portfolio_web_dev'] == 1 ) ? 1 : 0;
$portfolio_new_digital_art = ( isset( $_POST['portfolio_digital_art'] ) && $_POST['portfolio_digital_art'] == 1 ) ? 1 : 0;
$portfolio_new_digital_art = ( isset( $_POST['portfolio_graphic_design'] ) && $_POST['portfolio_graphic_design'] == 1 ) ? 1 : 0;
// Update the meta field in the database.
update_post_meta( $post->ID, 'portfolio_web_design', $portfolio_new_web_design );
update_post_meta( $post->ID, 'portfolio_web_dev', $portfolio_new_web_dev );
update_post_meta( $post->ID, 'portfolio_digital_art', $portfolio_new_digital_art );
update_post_meta( $post->ID, 'portfolio_graphic_design', $portfolio_new_digital_art );
}
}
new portfolio_metabox;
I cleaned up your code a bit. Try not to put space after everything, it's not necessary. The problem was in the way you're pulling the post meta out. The variables like $portfolio_web_design were empty.
I tested this and it worked.
Hope this helps :)

How do I customize my own select options in Gravity Forms?

I have two select fields in this custom Gravity Form that I've been struggling with. The first field is a list of webinars. The second field is a list of the webinar dates. Which dates show depend on which webinar you choose. I'm planning on sorting that list with jQuery but in order to do that I need to add an attribute to the select options in the webinar dates select field. At present, I can't figure out how to modify GF's select options function. This is what my custom function looks like currently:
//Populate Webinars form field for Webinar dates
add_filter( 'gform_pre_render_2', 'populate_webinar_dates' );
function populate_webinar_dates( $form ) {
foreach( $form[ 'fields' ] as &$field ) {
if( $field[ 'type' ] != 'select' || strpos( $field[ 'cssClass' ], 'populate-dates' ) === false )
continue;
$posts = new WP_Query( 'numberposts=-1&post_status=publish&post_type=vtl_webinar' );
$choices = '<option value=" ">Select a date</option>';
while ( $posts->have_posts() ) : $posts->the_post();
while ( has_sub_field( 'dates_available' ) ) :
$post_dates = array( 'date' => get_sub_field( 'date' ) );
$post_title = array( 'name' => str_replace( " ", "-", get_the_title() ) );
//$choices[] = array( 'text' => $post_dates['date'], 'value' => $post_dates['date'] );
$choices = '<option value="' . $post_dates['date'] . '" data-id="' . $post_title . '">' . $post_dates['date'] . '</option>';
endwhile;
endwhile;
$field[ 'choices' ] = $choices;
}
return $form;
}
Obviously, this line doesn't work:
$choices = '<option value="' . $post_dates['date'] . '" data-id="' . $post_title . '">' . $post_dates['date'] . '</option>';
It's expecting parameters similar to the commented form above that line but you can't just add your own attributes to that line. You have to work according to an existing set of parameters that GF is looking for. Can someone help me out with this? I just need to get the data attribute in the options.
I was going about this the wrong way. To get the custom options that I was trying to create, I used the following code instead of what I posted above.
add_filter("gform_field_input", "webinar_date_option", 10, 2);
function webinar_date_option($input, $field, $value, $lead_id, $form_id) {
if ( $field["cssClass"] == "populate-dates" ) {
$input = '<select name="input_2" id="input_2_2" class="medium gfield_select" tabindex="2">';
$posts = new WP_Query( 'numberposts=-1&post_status=publish&post_type=vtl_webinar' );
$input .= '<option value=" ">Select Date</option>';
while ( $posts->have_posts() ) : $posts->the_post();
while ( has_sub_field( 'dates_available' ) ) :
$post_dates = array( 'date' => get_sub_field( 'date' ) );
$date_id = array( 'id' => get_the_ID() );
$input .= '<option value="'. $post_dates['date'] .'" data-id="'. $date_id['id'] .'">'. $post_dates['date'] .'</option>';
endwhile;
endwhile;
$input .= '</select>';
}
return $input;
}

Resources