I am using dokan plugin for multivendor website. I want to add some extra field in dokan vendor setting page. I use this
add_filter( 'dokan_settings_form_bottom', 'extra_fields', 10, 2);
function extra_fields( $current_user, $profile_info ){
$seller_url= isset( $profile_info['seller_url'] ) ?
$profile_info['seller_url'] : '';?>
<label class="dokan-w3 dokan-control-label" for="setting_address">
<?php _e( 'Website', 'dokan' ); ?>
</label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="seller_url" id="reg_seller_url" value="<?php echo $seller_url; ?>" />
</div>
</div>
<?php }
//save the field value
add_action( 'dokan_store_profile_saved', 'save_extra_fields', 15 );
function save_extra_fields( $store_id ) {
if ( isset( $_POST['seller_url'] ) ) {
$dokan_settings = dokan_get_store_info($store_id);
$dokan_settings['seller_url'] = $_POST['seller_url'];
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings
);
}
But when i use this i am unable to update the payment method.please help me
Thanks
You can try this way to save the filed value and it should solve your problem -
add_action( 'dokan_store_profile_saved', 'save_extra_fields', 15 );
function save_extra_fields( $store_id ) {
$dokan_settings = dokan_get_store_info($store_id);
if ( isset( $_POST['seller_url'] ) ) {
$dokan_settings['seller_url'] = $_POST['seller_url'];
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}
If the code works then I hope you will mark this topic as resolved!
I have tried this code - https://gist.github.com/nayemDevs/b629d4b1c27c794bdfb729ce6927069e#file-extra-field-php and it is working for me. However, I have checked with Dokan theme and Hestia pro theme. Did you check if there is any theme conflict or not?
Related
WooCommerce decided to remove the + and - buttons from the product and cart pages to increase or decrease quantity. They say it was redundant to have and if anyone wants them back just install another plugin from them.
I, like others, don't wish to install a plugin when using code is the wiser option. Better yet, we should've been given the choice to keep them or not. I digress...
I've scoured the net for a solution, tried a couple, but no joy. Would really appreciate assistance with code needed to bring them back and where that code should be placed.
Found an answer on another thread here, though not sure exactly where it goes or if this is what I need to bring the buttons back
// Input +- tweak
$(function(a){
a(".woocommerce-ordering").on("change", "select.orderby", function(){
a(this).closest("form").submit();
}),
a("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").addClass("buttons_added").append('<input type="button" value="+" class="plus" />').prepend('<input type="button" value="-" class="minus" />'), a("input.qty:not(.product-quantity input.qty)").each(function(){
var b=parseFloat(a(this).attr("min"));b&&b>0&&parseFloat(a(this).val())<b&&a(this).val(b);
}),
a(document).on("click", ".plus, .minus", function(){
var b=a(this).closest(".quantity").find(".qty"),
c=parseFloat(b.val()),
d=parseFloat(b.attr("max")),
e=parseFloat(b.attr("min")),
f=b.attr("step");c&&""!==c&&"NaN"!==c||(c=0),
(""===d||"NaN"===d)&&(d=""),
(""===e||"NaN"===e)&&(e=0),
("any"===f||""===f||void 0===f||"NaN"===parseFloat(f))&&(f=1),
a(this).is(".plus")?b.val(d&&(d==c||c>d)?d:c+parseFloat(f)):e&&(e==c||e>c)?b.val(e):c>0&&b.val(c-parseFloat(f)),
b.trigger("change");
});
});
Thanks in advance!
Yes, I know the issue, really anoying, every theme that I create I need to fix this... Here is how I did it:
Create a folder in your theme folder: /woocommerce/global/
Create a file: quantity-input.php
Put the following content inside this file:
<?php
/**
* Product quantity inputs
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<div class="quantity">
<input type="text" pattern="[0-9]*" step="<?php echo esc_attr( $step ); ?>" <?php if ( is_numeric( $min_value ) ) : ?>min="<?php echo esc_attr( $min_value ); ?>"<?php endif; ?> <?php if ( is_numeric( $max_value ) ) : ?>max="<?php echo esc_attr( $max_value ); ?>"<?php endif; ?> name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php _ex( 'Qty', 'Product quantity input tooltip', 'moto' ) ?>" class="input-text qty text" size="4" />
<span class="td-quantity-button plus">+</span>
<span class="td-quantity-button min">-</span>
</div>
And of course you would need some jQuery to make the buttons work:
$('.td-quantity-button').on('click', function () {
var $this = $(this);
var $input = $this.parent().find('input');
var $quantity = $input.val();
var $new_quantity = 0;
if ($this.hasClass('plus')) {
var $new_quantity = parseFloat($quantity) + 1;
} else {
if ($quantity > 0) {
var $new_quantity = parseFloat($quantity) - 1;
}
}
$input.val($new_quantity);
});
Please note that you will have to style these buttons and input field yourself.
Please also note you need jquery enqueud in your theme or plugin:
function theme_name_scripts() {
wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
If you want a clean solution to add "-" and "+" increment buttons to WooCommerce product and cart page, with easy customization options, I created a plugin which does it without overriding templates, through hooks only:
Qty Increment Buttons for WooCommerce
PHP and jQuery code is only part of solution, because multiple CSS manipulations are required to make these inserted buttons presentable. WooCommerce 3.0 comes with additional hooks for product page, so implementation is even easier, but they are not a must and I got it to work for older versions as well.
Here is my jQuery code:
// Make code work on page load (this js file is executed only on product and cart page).
$(document).ready(function(){
QtyChng();
});
// Make code work after executing AJAX on cart page. Support for default WooCommerce and plugins using AJAX on cart page.
$( document.body ).on( 'updated_cart_totals', function(){
QtyChng();
});
function QtyChng() {
$('.woocommerce form.cart, .woocommerce td.product-quantity').on( 'click', '.qib-button', function() {
// Find quantity input field corresponding to increment button clicked.
var qty = $( this ).siblings( '.quantity' ).find( '.qty' );
// Read value and attributes 'min', 'max', 'step'.
var val = parseFloat(qty.val());
var max = parseFloat(qty.attr( 'max' ));
var min = parseFloat(qty.attr( 'min' ));
var step = parseFloat(qty.attr( 'step' ));
// Change input field value if result is in min and max range.
if ( $( this ).is( '.plus' ) ) {
if ( val === max ) return false;
if ( val + step > max ) {
qty.val( max );
} else {
qty.val( val + step );
}
} else {
if ( val === min ) return false;
if ( val - step < min ) {
qty.val( min );
} else {
qty.val( val - step );
}
}
$( this ).siblings( '.quantity' ).find( '.qty' ).trigger("change");
});
}
})(jQuery);
<?php
/**
* Product quantity inputs
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<div class="quantity">
<input type="text" pattern="[0-9]*" step="<?php echo esc_attr( $step ); ?>" <?php if ( is_numeric( $min_value ) ) : ?>min="<?php echo esc_attr( $min_value ); ?>"<?php endif; ?> <?php if ( is_numeric( $max_value ) ) : ?>max="<?php echo esc_attr( $max_value ); ?>"<?php endif; ?> name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php _ex( 'Qty', 'Product quantity input tooltip', 'moto' ) ?>" class="input-text qty text" size="4" />
<span class="td-quantity-button plus">+</span>
<span class="td-quantity-button min">-</span>
</div>
I'm working on a Wordpress theme and I need to add a meta box (checkbox) to the category admin panel.
I've written the code to add the meta box to the panel but there is 2 problems:
1- First it appears below the "Add category button"
2- What functions should I use to save the checkbox value in the database ?
and there is the code to add the checkbox
add_action ( 'category_add_form_fileds', 'add_to_main_page');
add_action('category_edit_form', 'add_to_main_page');
function add_to_main_page() {
?>
<input type="checkbox" name="add_to_main" id="add_to_main" value="1">
<label for="add_to_main">This category on main page</label>
<?php }
Thanks in advance
After fixing one typo—"category_add_form_fileds" to "category_add_form_fields"—your code worked fine for me. I get a checkbox on both forms above the "Add New Category" / "Update" buttons. This is a complete version and should do the trick:
add_action( 'category_add_form_fields', 'add_to_mainpg_fields' );
add_action( 'category_edit_form', 'add_to_mainpg_fields' );
function add_to_mainpg_fields() {
?>
<input type="checkbox" name="add_to_main" id="add_to_main" value="1" />
<label for="add_to_main">This category on main page</label>
<?php
}
add_action( 'created_category', 'add_to_mainpg_save' );
add_action( 'edited_category', 'add_to_mainpg_save' );
function add_to_mainpg_save( $term_id ) {
if( !isset( $_POST['add_to_main'] ) )
return;
$stickies = get_option( 'main_page_cats' );
if( !is_array( $stickies ) )
$stickies = array( $term_id );
if( !in_array( $term_id, $stickies ) )
$stickies[] = $term_id;
update_option( 'main_page_cats', $stickies );
}
This is a modified version of the stick_post function used for sticky posts.
This tutorial is instructive for saving multiple options. Two robust solutions are this plugin and this library. I understand if you don't want all that for a single field, but others might. :)
I created a custom meta box where you can choose a value from some radio buttons and save it to the post_meta table in the wordpress database. With the following code I save the value:
function save_value_of_my_custom_metabox ($post_id, $post){
$post_id = get_the_ID();
$new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_class( $_POST['the_name_of_the_radio_buttons'] ) : '' );
$meta_key = 'my_key';
update_post_meta( $post_id, $meta_key, $new_meta_value );
}
But if the post will be edited again I want the radio button with the current value to set checked. What is the best way to do that? Here is the function to display the meta box:
function my_custom_meta_box( $object, $box ) {
$post_id=get_the_ID();
$key='my_key';
$the_value_that_should_be_set_to_checked=get_post_meta( $post_id, $key);
//$the_value_that_should_be_set_to_checked[0] returns the value as string
?>
<label for="my_custom_metabox"><?php _e( "Choose value:", 'choose_value' ); ?></label>
<br />
<input type="radio" name="the_name_of_the_radio_buttons" value="value1">Value1<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value2">Value2<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value3">Value3<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value4">Value4<br>
<?php
}
I could write something like if(isset($the_value_that_should_be_set_to_checked[0])=="value of that line") echo "checked='checked'"; in every line but that doesn't seem very elegant to me. Using javascript is also pretty complicated in wordpress because I would have to use the hooks, enqueue the script and just for changing the checked property with one line of javascript it's not worth it. What's the best practice for that?
I am assuming that you are trying to add custom meta box for 'Posts'. Below code will work for you. It will show Radio buttons on add new post or edit post screen. Please read the comments in the code. It will help you in understanding the code.
You can use WordPress's checked function to decide whether to select the radio button or not.
Feel free to ask if you have any doubts.
/**
* Adds a box to the main column on the Post add/edit screens.
*/
function wdm_add_meta_box() {
add_meta_box(
'wdm_sectionid', 'Radio Buttons Meta Box', 'wdm_meta_box_callback', 'post'
); //you can change the 4th paramter i.e. post to custom post type name, if you want it for something else
}
add_action( 'add_meta_boxes', 'wdm_add_meta_box' );
/**
* Prints the box content.
*
* #param WP_Post $post The object for the current post/page.
*/
function wdm_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'wdm_meta_box', 'wdm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, 'my_key', true ); //my_key is a meta_key. Change it to whatever you want
?>
<label for="wdm_new_field"><?php _e( "Choose value:", 'choose_value' ); ?></label>
<br />
<input type="radio" name="the_name_of_the_radio_buttons" value="value1" <?php checked( $value, 'value1' ); ?> >Value1<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value2" <?php checked( $value, 'value2' ); ?> >Value2<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value3" <?php checked( $value, 'value3' ); ?> >Value3<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value4" <?php checked( $value, 'value4' ); ?> >Value4<br>
<?php
}
/**
* When the post is saved, saves our custom data.
*
* #param int $post_id The ID of the post being saved.
*/
function wdm_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( !isset( $_POST['wdm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( !wp_verify_nonce( $_POST['wdm_meta_box_nonce'], 'wdm_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Sanitize user input.
$new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_class( $_POST['the_name_of_the_radio_buttons'] ) : '' );
// Update the meta field in the database.
update_post_meta( $post_id, 'my_key', $new_meta_value );
}
add_action( 'save_post', 'wdm_save_meta_box_data' );
I found a working solution but I think this is not how you should do it. Still open for better solutions ;)
This code was added under the php code from above:
if(isset($$the_value_that_should_be_set_to_checked[0])){
$the_value_that_should_be_set_to_checked= $the_value_that_should_be_set_to_checked[0];
}
else{
$the_value_that_should_be_set_to_checked='';
}
Here's the code that I added below the radiobuttons:
<script type="text/javascript">
jQuery(document).ready(function () {
var checked_value= <?php echo json_encode($the_value_that_should_be_set_to_checked);?>;
if(checked_value!==''){
jQuery("input[name=the_name_of_the_radio_buttons][value="+checked_value+"]").attr('checked', 'checked');
}
});
</script>
P.S.: The $ selector will not work but that maybe depends on the theme you use.
I am trying to add a custom meta box to a wordpress page which stores a value in a custom field. It is not working. The meta box is displayed but when you press update the value entered ito the text box is lost and nothing is written to the wp_postmeta table (no _c3m_sponsor_ur meta_key is created)
I have adapted this from an example online. I also tried adding a die statement to see if the save post is even called but nothing dies. I also dont understand why the add_post_meta isn't being created for the page
add_action( 'add_meta_boxes', 'c3m_sponsor_meta' );
function c3m_sponsor_meta() {
add_meta_box( 'c3m_meta', 'Sponsor URL Metabox', 'c3m_sponsor_url_meta', 'page', 'side', 'high' );
}
function c3m_sponsor_url_meta( $post ) {
$c3m_sponsor_url = get_post_meta( $post->ID, '_c3m_sponsor_url', true);
if (!isset($c3m_sponsor_url))
add_post_meta($post->ID, '_c3m_sponsor_url', '', false);
echo 'Please enter the sponsors website link below';
?>
<input type="text" name="c3m_sponsor_url" value="<?php echo esc_attr( $c3m_sponsor_url ); ?>" />
<?php
}
add_action( 'save_post', 'c3m_save_project_meta' );
function c3m_save_project_meta( $post_ID ) {
die('here');
global $post;
if( $post->post_type == "page" ) {
if (isset( $_POST ) ) {
update_post_meta( $post_ID, '_c3m_sponsor_url', strip_tags( $_POST['c3m_sponsor_url'] ) );
}
}
}
Any help in fixing this is muh appreciated
Thanks a lot
This may be help you:--
add_action( 'save_post', 'c3m_save_project_meta' );
function c3m_save_project_meta( $post_ID ) {
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] )
{
if (isset( $_POST ) ) {
update_post_meta( $post_ID, '_c3m_sponsor_url', strip_tags( $_POST['c3m_sponsor_url'] ) );
}
}
}
Edited:-
You can simply use Advanced Custom Fields plugin instead of using code. This plugin much helpful for custom meta fields.
From what I read in the net, the following code should be adding a "City" field in the user registration form of Wordpress.
Thing is that, it seems to be not working - I don't see the additional "City" field in the user form.
Any help appreciated.
add_action( 'register_form', 'extended_register_form' );
add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
add_action('user_register', 'myplugin_user_register');
function extended_register_form() {
$city = ( isset( $_POST['city'] ) ) ? $_POST['city']: '';
?>
<p>
<label for="city">City<br />
<input type="text" name="city" id="city" class="input" value="<?php echo esc_attr(stripslashes($city)); ?>" size="25" /></label>
</p>
<?
}
function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {
if ( empty( $_POST['city'] ) )
$errors->add( 'city_error', __('<strong>ERROR</strong>: You must include a city.') );
return $errors;
}
function myplugin_user_register ($user_id) {
if ( isset( $_POST['city'] ) )
update_user_meta($user_id, 'city', $_POST['city']);
}
Since Wordpress 3.0.0, the action to call is "signup_extra_fields" instead of "register_form", so you should use:
add_action( 'signup_extra_fields', 'extended_register_form' );
I tried your code and it worked perfectly, perhaps it is because of the <? in line 12... Try <?php instead...
Where have you added the code? As a plugin or in the functions.php of your theme?