I want to add some fields to customer profile and I have added following this question
This works good if my fields aren't required, but how can I do them required and show an error when customer doesn't fill them?
With this code I can add an error but it appears after another ok message and redirect to "my account" page:
add_action('woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form');
function add_favorite_color_to_edit_account_form()
{
$user = wp_get_current_user();
?>
<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
<label for="favorite_color"><?php _e('Favorite color', 'woocommerce'); ?>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color" id="favorite_color" value="<?php echo esc_attr($user->favorite_color); ?>" />
</p>
<?php
}
add_action('woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1);
function save_favorite_color_account_details($user_id)
{
$fav_color = $_POST['favorite_color'];
if (empty($fav_color)) {
wc_add_notice(__('Fav color is required!', 'xxxxx'), 'error');
} else {
update_user_meta($user_id, 'favorite_color', sanitize_text_field($_POST['favorite_color']));
}
}
I have been able to add a custom text input field, in woocommerce's order details page, but I can't save the data submitted data.
This is the code:
add_action( 'woocommerce_order_details_before_order_table', 'add_custom_Field',10,2 );
function add_custom_Field( $order_id) {
$user = wp_get_current_user();
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo esc_attr( $order_id->custom_URL ); ?>" />
</p>
<input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
<?php
function submit()
{
update_user_meta( $user_id, 'custom_URL', sanitize_text_field( $_POST['custom_URL'] ) );
echo "Your function on button click is working";
}
if(array_key_exists('test',$_POST)){
submit();
}
}
Do you know what I'm doing wrong?
The following will allow you to add a user custom field to customer order detail and to save its value on submission:
// Display user custom field
add_action( 'woocommerce_order_details_before_order_table', 'add_user_custom_url_field_to_order' );
function add_user_custom_url_field_to_order( $order ) {
global $current_user;
$custom_url = get_user_meta( $current_user->ID, 'custom_URL', true );
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo $custom_url; ?>" />
</p>
<input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
</form>
<?php
}
// Save the field as custom user data
add_action( 'template_redirect', 'save_user_custom_url_field_from_order' );
function save_user_custom_url_field_from_order() {
global $current_user;
if( isset($_POST['custom_URL']) ){
update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
wc_add_notice( __("Submitted data has been saved", "woocommerce") );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note based on your comment:
If the custom url has to be different for each order, you can't save it as user meta data, but instead as $order item meta data... But the code will be slightly different and you will need to define how will be your custom urls.
I need to create a custom form in a new tab in woocommerce my account page.¨
I need to save data and output them to the user
What I've tried:
add_action( 'init', 'misha_add_endpoint' );
function misha_add_endpoint() {
add_rewrite_endpoint( 'log-history', EP_PAGES );
}
add_action( 'woocommerce_account_log-history_endpoint', 'misha_my_account_endpoint_content' );
function misha_my_account_endpoint_content() {
get_current_user_id()
echo '<h3>User settings prize wheel</h3>';
$user = wp_get_current_user();
echo '
<form class="woocommerce-EditAccountForm edit-account" action="" method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_display_name">Steam ID <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="steam_id" id="account_display_name" value="'.esc_attr( $user->steam_id ).'"> <span><em>Vaše Steam ID, místo kam se budou posílat výherní item. </em></span>
</p>
<div class="clear"></div>
<p>
<button type="submit" class="woocommerce-Button button" name="save_prize_wheel" value="Uložit změny">Uložit změny</button>
<input type="hidden" name="action" value="save_prize_wheel">
</p>
</form>
';
}
add_action( 'woocommerce_save_prize_wheel', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
// For Favorite color
if( isset( $_POST['steam_id'] ) )
update_user_meta( $user_id, 'steam_id', sanitize_text_field( $_POST['steam_id'] ) );
}
The data did not save.
You miss semi Colin here get_current_user_id() it should be get_current_user_id();
IN QW question & Answer ( wordpress plugin ) ,how can user add question without registering(log in) or entering their email address ?
I want that all user(guest) can be able to ask their questions without entering their email address or even registering.
I had the same issue and I just resolved it.
DWQA version 1.3.3
You have to replace the function
function dwqa_require_field_submit_question()
with your own version. You find the old function at line 79 in your template-functions.php in the plugin folder.
Just removing the function from the queue will make it impossible to add new questions. So you just add the adjusted version of the function to your functions.php in your theme folder and and remove the standard function and add the adjusted function.
Add following to your functions.php:
function dwqa_require_field_submit_question_adjusted(){
?>
<input type="hidden" name="dwqa-action" value="dwqa-submit-question" />
<?php wp_nonce_field( 'dwqa-submit-question-nonce-#!' ); ?>
<?php
$subscriber = get_role( 'subscriber' );
?>
<?php if ( ! is_user_logged_in() && ! dwqa_current_user_can( 'post_question' ) ) { ?>
<input type="hidden" name="login-type" id="login-type" value="sign-up" autocomplete="off">
<div class="question-register clearfix">
<label for="user-email"><?php _e( 'You need an account to submit question and get answers. Create one:','dwqa' ) ?></label>
<div class="register-email register-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your email','dwqa' ) ?>" name="user-email">
</div>
<div class="register-username register-input">
<input type="text" size="20" value="" class="input" placeholder="Choose an username" name="user-name-signup" id="user-name-signup">
</div>
<div class="login-switch"><?php _e( 'Already a member?','dwqa' ) ?> <a class="credential-form-toggle" href="<?php echo wp_login_url(); ?>"><?php _e( 'Log In','dwqa' ) ?></a></div>
</div>
<div class="question-login clearfix dwqa-hide">
<label for="user-name"><?php _e( 'Login to submit your question','dwqa' ) ?></label>
<div class="login-username login-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your username','dwqa' ) ?>" id="user-name" name="user-name">
</div>
<div class="login-password login-input">
<input type="password" size="20" value="" class="input" placeholder="<?php _e( 'Type your password','dwqa' ) ?>" id="user-password" name="user-password">
</div>
<div class="login-switch"><?php _e( 'Not yet a member?','dwqa' ) ?> <a class="credential-form-toggle" href="javascript:void( 0 );" title="<?php _e( 'Register','dwqa' ) ?>"><?php _e( 'Register','dwqa' ) ?></a></div>
</div>
<?php } else if ( ! is_user_logged_in() && dwqa_current_user_can( 'post_question' ) ) { ?>
//Here was the old code which had shown the E-mail input and login information
<?php }
}
This function will replace the old function
function dwqa_require_field_submit_question()
Now just remove the old function and add the new to the queue in your functions.php :)
remove_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question' );
add_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question_adjusted' );
That is it :). Maybe not the best way, but it avoids changing the plugin code.
Have a nice day
//Removes BuddyBar from non-admins only
function splen_remove_admin_bar() { if( !is_super_admin() )
add_filter( ‘show_admin_bar’, ‘__return_false’ );
}
add_action(‘wp’, ‘splen_remove_admin_bar’);
Not sure what've I've done wrong, probably something really silly like I've missed an attribute or something.
Right I'm trying to build a feature on a theme that displays on the homepage. That bit I can do just fine. What I want to do is make this a little easier for the user. So they click image slider on the left hand side, add a title, add a description and! An image from the media library.
So far, to simplify my idea, I just want to add a custom meta field, as far as I understand, so the user can add an image address.
My problem is this meta box is not displaying in the admin panel and I'm not sure why? I've been reading and follow serval tutorials etc. Just can't seem to get it to display? Can anybody see anything wrong?
Here's what I've built
function post_support(){
add_theme_support('post-formats',array('aside','Image Slider'));
}
/**
* create slider, adding post register
*/
function slider_create_slider(){
register_post_type('Image Slider',
array('labels'=>
array('name'=>__('Image slider'),
'singular_name'=>__('Image slider'),
'add_new'=>__('Add New slider object'),
'edit_item'=>__('Edit Slide object'),
'new_item'=>__('Add New Slide object'),
'view_item'=>__('View slide object'),
'search_items'=>__('Search Slide Objects'),
'not_found'=>__('No Slide objects found'),
'not_found_in_trash'=>__('No slide Objects found in the bin.')),
'public'=>true,
'show_ui'=>true,
'capibility_type'=>'post','hierarchical'=>false,'rewrite'=>true,'menu_position'=>20,'supports'=>array('title','editor','thumbnail')));
}
/**
* adding slider's meta boxes
*/
function slider_add_meta_boxes($post){
//Only need to add image meta
add_meta_box('ImageSliderMeta',__('image'),'slider_image_meta_box',__('image'),'side','default');
}
function slider_image_meta_box(){
$image=get_post_meta($post->ID,'ImageSliderMeta',true);
//only testing atm
?>
<label>Image (url)</label><input name="ImageSliderMeta" value="<?echo$image;?>"/>
?>
}
function save_image_meta_box(){
global$post;
update_post_meta($post->ID,'ImageSliderMeta',$_POST['ImageSliderMeta']);
}
add_action('after_setup_theme','post_support');
add_action('init','slider_create_slider');
add_action('add_meta_boxes','slider_add_meta_boxes');
add_action('save_post','save_image_meta_box');
Is it because I've structured my add_action's incorrectly?
Update
Your add_meta_box function is not passing the correct arguments.
Argument #4, $screen is the post types you want to show the meta boxes for. Use the following;
function slider_create_slider(){
register_post_type('imageslider'
...
}
function slider_add_meta_boxes($post)
{
add_meta_box('ImageSliderMeta',__('image'),'slider_image_meta_box', 'imageslider');
}
http://codex.wordpress.org/Function_Reference/add_meta_box
I did it a few times. First, you must create the post type. In your plugin file, you must require the files, that define your custom post types and meta boxes. In your meta box file, you mostly generate a HTML code of the form element and add a function, that updates the post meta. I will give you an example (options_myplugin.php, required in the main file):
function myplugin_staff_meta_box_add() {
add_meta_box('myplugin-staff-edit', 'Staff Social Meta Box', 'myplugin_meta_box_staff_cb', 'staffs', 'normal', 'high');
}
function myplugin_meta_box_staff_cb($post) {
$position = get_post_meta($post->ID, 'position', true);
$facebook = get_post_meta($post->ID, 'facebook', true);
$twitter = get_post_meta($post->ID, 'twitter', true);
$gplus = get_post_meta($post->ID, 'gplus', true);
$linkedin = get_post_meta($post->ID, 'linkedin', true);
wp_nonce_field('staff_meta_box_nonce', 'staff_meta_box_nonce');
?>
<p class="myplugin_option_box">
<label for="position" class="myplugin-desc"><?php _e('Position', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="position" type="text" name="position"
class="myplugin-option-input" value="<?php echo $position; ?>">
<span>Ex: Project Manager</span>
</p>
<p class="myplugin_option_box">
<label for="facebook" class="myplugin-desc"><?php _e('Facebook', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="facebook" type="text" name="facebook"
class="myplugin-option-input" value="<?php echo $facebook; ?>">
<span>Ex: http://facebook.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="twitter" class="myplugin-desc"><?php _e('Twitter', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="twitter" type="text" name="twitter"
class="myplugin-option-input" value="<?php echo $twitter; ?>">
<span>Ex: http://twitter.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="gplus" class="myplugin-desc"><?php _e('Google Plus', 'myplugin-nictitate-toolkit'); ?>:</label>
<input id="gplus" type="text" name="gplus"
class="myplugin-option-input" value="<?php echo $gplus; ?>">
<span>Ex: http://plus.google.com/myplugintheme</span>
</p>
<p class="myplugin_option_box">
<label for="linkedin" class="myplugin-desc"><?php _e('LinkedIn', 'myplugin-nictiate-toolkit'); ?></label>
<input id="linkedin" type="text" name="linkedin" class="myplugin-option-input" value="<?php echo $linkedin; ?>"/>
<span>Ex: http://linkedin.com/myplugintheme</span>
</p>
<?php
}
add_action('save_post', 'myplugin_save_staff_data');
function myplugin_save_staff_data($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!isset($_POST['staff_meta_box_nonce']) || !wp_verify_nonce($_POST['staff_meta_box_nonce'], 'staff_meta_box_nonce'))
return;
if (!current_user_can('edit_post'))
$allowed = array(
'a' => array(
'href' => array()
)
);
if (isset($_POST['position']))
update_post_meta($post_id, 'position', wp_kses($_POST['position'], $allowed));
if (isset($_POST['facebook']))
update_post_meta($post_id, 'facebook', wp_kses($_POST['facebook'], $allowed));
if (isset($_POST['twitter']))
update_post_meta($post_id, 'twitter', wp_kses($_POST['twitter'], $allowed));
if (isset($_POST['gplus']))
update_post_meta($post_id, 'gplus', wp_kses($_POST['gplus'], $allowed));
if (isset($_POST['linkedin']))
update_post_meta($post_id, 'linkedin', wp_kses($_POST['linkedin'], $allowed));
}
From now you will be able to display the meta values via get_post_meta.