I've got my own custom registration form, and it allows people to register except for one small issue: the checkboxes don't do anything. When I check the meta_key fields in the database, they don't exist. Everything else is OK though.
I'm trying to have 'comps' and 'newsletter' get added to the meta_key fields (with a value of 1) when they checkboxes are ticked. My form looks like this:
<form id="form" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
<fieldset>
<label for="user-login">Username<br />
<input type="text" name="user_login" id="user-login" class="input" value="" size="20" tabindex="10" />
<label for="user-email">Email address<br />
<input type="text" name="user_email" id="user-email" class="input" value="" size="25" tabindex="20" />
<label for="comps">Competitions<br />
<input type="checkbox" name="comps" id="comps" class="" value="1" tabindex="30" /></label>
<label for="newsletter">Newsletter<br />
<input type="checkbox" name="newsletter" id="newsletter" class="" value="1" tabindex="40" /></label>
<input type="hidden" name="redirect_to" value="<?php echo get_settings('home'); ?>/registration-succeeded"/>
<input type="submit" name="wp-submit" class="button" value="Register Me!" tabindex="100" />
</fieldset>
Is there something I need to do in functions.php of my theme to get these meta_fields to go into the database when the user registers (and when the checkboxes are ticked of course)?
Thanks in advance.
I've worked it out. Just needed to register these extra fields for metadata use:
function at_register_custom_fields( $user_id, $password = "", $meta = array() ) {
// custom fields
$fields = array(
'comps',
'newsletter',
);
// cleans and updates the custom fields
foreach ( $fields as $field ) {
$value = stripslashes( trim( $_POST[$field] ) ) ;
if ( ! empty( $value ) ) {
update_user_meta( $user_id, $field, $value );
}
}
}
add_action( 'user_register', 'at_register_custom_fields' );
Related
Custom Wordpress Search Result Page not working and redirects to 404.
Target Url: https://example.com/blog/search/?post_type=post&s=a
// CUSTOM BLOG POST SEARCH FORM
function blog_search_form( $blog_form ) {
$blog_form = '
<form class="form-inline" id="search" action="'.home_url( "/blog/search/" ).'" method="get">
<div class="form-group mx-sm-1 mb-2 w-100">
<input type="hidden" name="post_type" value="post" />
<input class="form-control w-100" id="s" name="s" type="text" placeholder="Search blog" value="' . get_query_var('s') . '" required />
</div>
<input id="searchsubmit" class="main-btn btn mb-2" type="submit" value="Search" />
</form>';
return $blog_form;
}
add_filter( 'get_search_form', 'blog_search_form' );
// CHANGE URL FUNCTION
/function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
I have create multiple checkbox in meta box using this code
add_action ( 'edit_category_form_fields', 'extra_category_fields');
function extra_category_fields( $tag ) {
$t_id = $tag->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1"><?php _e('extra field'); ?></label></th>
<td>
<input type="text" name="Cat_meta[extra1]" id="Cat_meta[extra1]" size="25" style="width:60%;" value="<?php echo $cat_meta['extra1'] ? $cat_meta['extra1'] : ''; ?>"><br />
<span class="description"><?php _e('extra field'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label><?php _e('extra field'); ?></label></th>
<td>
<input type="radio" name="radio" value="ongoing" <?php checked( 'ongoing', get_option('radio') ); ?> >
<label class="description" for="ongoing">ongoing</label><br>
<input type="radio" name="radio" value="complated" <?php checked( 'complated', get_option('radio') ); ?> >
<label class="description" for="complated">complated</label>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label><?php _e('extra field'); ?></label></th>
<td>
<label class="checkbox-inline" for="1">
<input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 1</label>
<label class="checkbox-inline" for="2">
<input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 2</label>
<label class="checkbox-inline" for="3">
<input type="checkbox" name="checkbox"<?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 3</label>
</td>
</tr>
<?php
}
add_action ( 'edit_category_form_fields', 'extra_category_fields');
for saving:
function save_extra_category_fileds( $term_id) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
if (isset($_POST['radio'])) {
update_option('radio', $_POST['radio']);
}
if (isset($_POST['checkbox'])); {
update_option('checkbox', $_POST['checkbox']);
}
}
update_option( "category_$t_id", $cat_meta );
}
}
add_action ( 'edited_category', 'save_extra_category_fileds');
radio option working fine but when I check one of checkbox and refresh the page it goes back to unchecked.
Category fields not same normal pages so I cant use update_post_meta or $post_id
so plz help me in this ... thank you
It doesn't appear that there is anything to save for your checkboxes.
<td>
<label class="checkbox-inline" for="1">
<input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 1</label>
<label class="checkbox-inline" for="2">
<input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 2</label>
<label class="checkbox-inline" for="3">
<input type="checkbox" name="checkbox"<?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 3</label>
</td>
They don't have a value="", so there's nothing to get saved? What you have above is 3 identical checkboxes all of which have no value.
This should work:
<td>
<label class="checkbox-inline" for="1">
<input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?> value="Option 1"> Option 1
</label>
<label class="checkbox-inline" for="2">
<input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?> value="Option 2"> Option 2
</label>
<label class="checkbox-inline" for="3">
<input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?> value="Option 3"> Option 3
</label>
</td>
Update:
Initial question asked why the checkboxes weren't being saved, so I only looked to address that, but here's the rest of the information you need... ...checkboxes will allow more than one selection unlike radio buttons which only allow one. So your option is to either give them their own name and thus key/value pair, or you make checkbox an array. Looks like you're serializing this data so think of it as an array within an array. In your database, the key checkbox will be saved as 'checkbox:array('Option 1', 'Option 2', 'Option 3')`.
The reason your page reload now shows them all checked is because all of the checkboxes are checking for the exact same value checkbox == true - so basically if one is there, then they're all checked. Just like giving them different values is required so that the code has something to save, you have to check for different values to determine which one to show as checked.
Here you go:
<td>
<label class="checkbox-inline" for="1">
<input type="checkbox" name="checkbox[]" value="Option 1" <?php if( in_array( 'Option 1', $cat_meta['checkbox'] ) ) { echo 'checked'; } ?>/> Option 1
</label>
<label class="checkbox-inline" for="2">
<input type="checkbox" name="checkbox[]" value="Option 2" <?php if( in_array( 'Option 2', $cat_meta['checkbox'] ) ) { echo 'checked'; } ?>> Option 2
</label>
<label class="checkbox-inline" for="3">
<input type="checkbox" name="checkbox[]" value="Option 3" <?php if( in_array( 'Option 3', $cat_meta['checkbox'] ) ) { echo 'checked'; } ?>> Option 3
</label>
</td>
You'll notice that now the name=checkbox[] has those square brackets at the end, that's signifying that its an array and that multiple values may be stored. And now the check has to verify if the value specified is equal to any of the values located within the array, so we use
if( in_array( 'Option 3', $cat_meta['checkbox'] ) ) { echo 'checked'; } which basically says IF this string (Option 3) is found in the array $cat_meta[checkbox'], then echo the word 'checked'.
It's late here and I've had a long day but I'm also thinking that you may encounter an issue because its an array within an array - but to address that all you would have to do is extract the $cat_meta['checkbox'] array from the whole serialized $cat_meta and basically have a new array variable like:
$cat_checkbox = $cat_meta['checkbox'];
Then you'd run your IF statement to to see which checkboxes are checked using:
<?php if( in_array( 'Option 2', $cat_checkbox ) ) : echo checked; endif; ?>
I've never done it exactly like this so I'm applying what I know generally works in WP from different methods - so you may still need to debug a bit.
I have registered a custom post type named fav_songs in functions.php. It has three metaboxes defined - Artist, Genre and Year of Release. I am able to save data from admin interface. Now I want to do the similar thing from front end.
For this I first created the following template file:
<?php
/* Template Name: Song Entry Form */
get_header();
?>
<form id="song-entry" name="song-entry" method="post" action="">
<p>
<label>Title</label><br />
<input type="text" id="song_title" name="song_title" />
</p>
<p>
<label>Description</label><br />
<input type="text" id="song_desc" name="song_desc" />
</p>
<p>
<label>Artist</label><br />
<input type="text" id="song_artist" name="song_artist" />
<input type="hidden" name="post_type" id="post_type" value="post" />
<input type="hidden" name="action" value="post" />
</p>
<p>
<input type="submit" value="Submit" />
</p>
<?php wp_nonce_field( 'new_song_nonce' ); ?>
</form>
<?php
function save_song()
{
if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) {
if (!isset($_POST['new_song_nonce'])) {
return;
}
if (!isset($_POST['song_title'])) {
return;
}
if (!isset($_POST['song_desc'])) {
return;
}
$post = array(
'post_title' => $_POST['song_title'],
'post_content' => $_POST['song_desc'],
'post_type' => 'fav_songs'
);
wp_insert_post($post);
update_post_meta($post->ID, '_song_artist_name', $_POST['song_artist']);
}
}
?>
<?php
get_footer();
?>
In my functions.php I hooked save_post action for save_song.
add_action('save_post', 'save_song');
then I added a page in admin and used the above template file. When the page renders the all fields are coming up. But when I hit submit nothing gets saved in wp_posts and wp_postmeta. For now I only want to store title and description in wp_posts table and meta data artist in wp_postmeta table.
Something must have gone wrong in my above approach but I don't know what! I have started learning WordPress recently and don't have much knowledge to sort this out. Please help!
UPDATE (Modified template page code)
<?php
/* Template Name: Song Entry Form */
get_header();
if($_POST['post_submit'] == 'Submit') {
$args = array(
'post_title' => $_POST['post_title'],
'post_content' => $_POST['post_desc'],
'post_type' => 'fav_songs',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed'
);
$pid = wp_insert_post($args);
add_post_meta($pid, "_song_artist", $_POST['post_artist']);
}
?>
<form id="post_entry" name="post_entry" method="post" action="<?php echo get_page_link('354') ?>">
<p>
<label>Title</label><br />
<input type="text" id="post_title" name="post_title" />
</p>
<p>
<label>Description</label><br />
<input type="text" id="post_desc" name="post_desc" />
</p>
<p>
<label>Artist</label><br />
<input type="text" id="post_artist" name="post_artist" />
<input type="hidden" name="post_type" id="post_type" value="fav_songs" />
<input type="hidden" id="post_action" name="post_action" value="post" />
</p>
<p>
<input type="submit" name="post_submit" value="Submit" />
</p>
<?php wp_nonce_field( 'new_song_nonce' ); ?>
</form>
<?php
get_footer();
?>
I checked View Source. The form now renders as:
<form id="post-entry" name="post-entry" method="post" action="http://local.tourplanner.com/add-song/">...</form>
Even with the action set to a specific page why I am jumping back to homepage without saving any data?
All form elements should always be prefixed with something unique to prevent clashes. In your form we put common prefix 'post_' and it is working:
Try below code:
On same page you can get form field and insert into db:
you can further use generated post id for save custom meta.
if($_POST['post_submit']=='Submit'){
;
$id = wp_insert_post(array('post_title'=>$_POST['post_title'], 'post_type'=>'fav_songs', 'post_content'=>$_POST['post_desc'],'post_status' => 'publish','comment_status' => 'closed','ping_status' => 'closed'));
}
<form id="song-entry" name="post_entry" method="post" action="<?php echo get_page_link('your template id') ?>">
<p>
<label>Title</label><br />
<input type="text" id="post_title" name="post_title" />
</p>
<p>
<label>Description</label><br />
<input type="text" id="post_desc" name="post_desc" />
</p>
<p>
<label>Artist</label><br />
<input type="text" id="post_artist" name="post_artist" />
<input type="hidden" name="post_type" id="post_type" value="post_type" />
<input type="hidden" name="post_action" id="post_action" value="post_action" />
</p>
<p>
<input type="submit" name="post_submit" value="Submit" />
</p>
<?php wp_nonce_field( 'new_song_nonce' ); ?>
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;
}
In woocommerce form-edit-account.php, i have the following but would only like to retain the Password and Confirm new password fields. I deleted all fields except Password and Confirm new password field but there is a validator that prompts me to fill up First Name, Last Name, Email address fields. How do i disable the validator for that? Really need help on this. Thanks in advance.
<?php
/**
* Edit account form
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
?>
<?php wc_print_notices(); ?>
<form action="" method="post">
<p class="form-row form-row-first">
<label for="account_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="account_first_name" id="account_first_name" value="<?php esc_attr_e( $user->first_name ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="account_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="account_last_name" id="account_last_name" value="<?php esc_attr_e( $user->last_name ); ?>" />
</p>
<p class="form-row form-row-wide">
<label for="account_email"><?php _e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="input-text" name="account_email" id="account_email" value="<?php esc_attr_e( $user->user_email ); ?>" />
</p>
<p class="form-row form-row-first">
<label for="password_1"><?php _e( 'Password (leave blank to leave unchanged)', 'woocommerce' ); ?></label>
<input type="password" class="input-text" name="password_1" id="password_1" />
</p>
<p class="form-row form-row-last">
<label for="password_2"><?php _e( 'Confirm new password', 'woocommerce' ); ?></label>
<input type="password" class="input-text" name="password_2" id="password_2" />
</p>
<div class="clear"></div>
<p><input type="submit" class="button" name="save_account_details" value="<?php _e( 'Save changes', 'woocommerce' ); ?>" /></p>
<?php wp_nonce_field( 'save_account_details' ); ?>
<input type="hidden" name="action" value="save_account_details" />
</form>
Well, I do that using this:
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $fields ) {
unset($fields['first_name']);
return $fields;
}
It will remove the "First Name" field from both, billing and shipping address.
After some quick research, the solution to exactly your problem, and also resently my problem.
Just add this to functions.php
add_filter( 'woocommerce_save_account_details_required_fields','custom_woocommerce_save_account_details_required_fields' );
function custom_woocommerce_save_account_details_required_fields( $required_fields ) {
unset($required_fields["account_first_name"]);
unset($required_fields["account_last_name"]);
return $required_fields;
}
It will remove the First Name field and Last Name field from the condition of required fields. Of course you can do that with all of your required inputs.
You can remove these validation manually.
Woocomaerce->includes->class-wc-form-handler.php
find function
save_account_details()
edit this function or you can remove validation form here.