The problem is i need a custom field for my buyers to input some dates in a textarea, and when i make the code for it with a plugin or hardcode a function for it, the code makes 2 input fields as shown in this picture/gyazo, i i cant seem to figure out why :/
https://gyazo.com/97e61de41071324491268258d84f8aae
You can see the code in the next gyazo:
https://gyazo.com/e811efd8b0e9c60b361dc52027693797
I have tried everything that i could figure out with my compatebilities
//Custom Product Option
function custom_field_by_category() {
global $product;
if ( is_product_category( 'work-date' ) ) {
return;
}
?>
<div class="custom-date-field">
<label for="custom-date"><?php _e( 'Engraving (10 characters)', 'iconic' ); ?></label>
<input type="text" id="custom-date" name="dato_oensker" placeholder="<?php _e( 'Dato Ønsker', 'iconic' ); ?>" maxlength="10">
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'custom_field_by_category', 10 );
Related
I newby here ... Woocommerce Wishlist plugin so that I can have the "add to wishlist" on my products page and not just the single page.
I added this to my functions file but it creates errors. I have done a ton of searching, but unable to find a solution.
Any ideas would be very much appreciated.
Code: add_action( 'woocommerce_after_shop_loop_item_title', array( WC_Wishlists_Plugin, 'add_to_wishlist_button' ), 10 );
Error: Warning: Use of undefined constant WC_Wishlists_Plugin - assumed 'WC_Wishlists_Plugin' (this will throw an Error in a future version of PHP)
Cheers
most probably your code is deprecated.
Try the bellow code and check if it would achieve the result that you wants.
It worked out for me but the design wasn't good enough
function addWishlistButton() {
global $wishlists;
?>
<form class="cart wishlist-cart-form" method="post" enctype='multipart/form-data'
action="<?php echo add_query_arg( array( 'add-to-wishlist-itemid' => get_the_ID() )); ?>">
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( get_the_ID() ); ?>" />
<?php $wishlists->add_to_wishlist_button(); ?>
</form>
<?php
}
add_action( 'woocommerce_after_shop_loop_item', 'addWishlistButton', 10 );
**PS: ** You need to add the code inside your functions.php
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.
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?
WordPress 3.2.1
I'm creating separate page templates, however, there are some page templates that should have default custom fields. That is, if someone created a new page of template type "Contact Us", it should by default have custom fields:
Success Message
Email To
Phone Number
etc.
Right now, the only way I can think of knocking this out is by having the admin add those custom fields to the page, and then fill them in. However, this isn't the best method for basically giving an admin a "turn-key" type of feature, i.e., they create a page with a specific template and just fill in the fields.
Thanks guys!
The following will add 1 extra meta box to your (insert-edit) page.
In it you can add custom fields to save.
It does not switch depending on the selected template.
I would recomend using javascript to show/hide fields to do this. Based on ID of the <p>
If you get the meta box working with ALL possible fields you can show I'm willing to help with the javascript to hide and show based on the template.
GL
<?php
add_action( 'add_meta_boxes', 'carrousel_build' );
function carrousel_build()
{
add_meta_box('carrousel', 'Carrousel opties', 'carrousel_options', 'page',/*show on 'pages'*/ 'normal', 'high');
}
//this will add a meta box with custom fields
function carrousel_options ($post)
{ ?>
<p>
Description
</p>
<p id="field1_container">
<label for="field1">Custom field 1</label>
<br/>
<input type="text" id="field1" name="field1" value="<?php echo get_post_meta($post->ID, 'field1', true) ?>" size="25" />
</p>
<p id="field2_container">
<label for="field2">Custom field 2</label>
<br/>
<input type="text" id="field2" name="field2" value="<?php echo get_post_meta($post->ID, 'field2', true) ?>" size="25" />
</p>
<?php }
//save the values of the meta box
add_action( 'save_post', 'post_save' );
function post_save($post_id)
{
// Check permissions
if (isset ($_POST['post_type']) && 'carrousel' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ))
{
return;
}
if (isset($_POST['field1'])) {
$subtitle = $_POST['field1'];
update_post_meta($post_id, 'field1', $subtitle);
}
if (isset($_POST['field2']))
{
$link = $_POST['field2'];
update_post_meta($post_id, 'field2', $link);
}
}