No access to the created input - wordpress

I added an extra field to the cart.php using Snippet plugin.
function action_woocommerce_cart_coupon() {
echo '<input type="text" name="card" class="input-text" id="card" value="" placeholder="Card" />';
};
add_action( 'woocommerce_cart_coupon', 'action_woocommerce_cart_coupon');
And I would my own validation when the Apply coupon button is clicked. Unfortunately, There is no way I can get value of this new field.
I downloaded the post in several places but I don't have access to this field.
if ( ! empty( $_POST['card'] ) ) {
echo '<script>console.log("' . $_POST['card'] . '")</script>';
} else {
echo '<script>console.log("PHP error")</script>';
}
function action_woocommerce_applied_coupon( $coupon_code ) {
foreach ($_POST as $key => $value) {
echo '<script>console.log("' . $key . " " . $value . '")</script>';
}
};
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon');
Output:
security ac228a43c2
coupon_code 1
I understand this is called in class-wc-cart.php but I can't understand how I could pass this field to this method. Is anyone able to tell me something about this?
I will also appreciate any other ideas to solve this problem.

Related

How to display some attributes directly on (single) shop page/product page in Woocommerce?

I'm trying to display some attributes directly on the shop page. The attributes are all collected in the database. To make it easier to show, I made a screenshot so you can get a better idea. the thing is now, with which code I can do that. the attributes, there are always 4, should be display centered under the block with the pictures and the buy button. I would be very happy if we could find a way to do this. Thanks very much
Ok, i've found some code here on stackoverflow... the good news are, i get the results/attributes i want, the bad news are, on the wrong page (shop page instead of shop page single).
this is the code:
add_action('woocommerce_after_shop_loop_item_title', 'display_custom_product_attributes_on_loop', 5 );
function display_custom_product_attributes_on_loop() {
global $product;
// Settings: Here below set your product attribute label names
$attributes_names = array('alter', 'bausteine', 'publicationdate', 'sku');
$attributes_data = array(); // Initializing
// Loop through product attribute settings array
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<div class="items" style="color: red;"><p>' . implode( '<br>', $attributes_data ) . '</p></div>';
}
}
this code shows me the attributes and the results on the shoppage, but i need it on the single shop page/product page.
Thank you!
Change the hook name and then check
add_action('woocommerce_single_product_summary', 'display_custom_product_attributes_on_loop', 5 );
function display_custom_product_attributes_on_loop() {
global $product;
// Settings: Here below set your product attribute label names
$attributes_names = array('alter', 'bausteine', 'publicationdate', 'sku');
$attributes_data = array(); // Initializing
// Loop through product attribute settings array
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<div class="items" style="color: red;"><p>' . implode( '<br>', $attributes_data ) . '</p></div>';
}
}

Wordpress CF7 generate random unique string for each submision

So I`m trying to generate a dynamic hidden text field that will have a random string of letters and numbers into Contact Form 7.
I have tried the following code
add_action('wpcf7_init', 'custom_code_generator');
function custom_code_generator(){
wpcf7_add_form_tag('coupon_code', 'custom_code_handler');
}
function custom_code_handler($tag){
$input_code_name = 'rand_string';
$charsList = '1234567890abcdefghijklmnopqrstuvwxyz';
$randomString = '';
for ($i=4; $i < 10; $i++){
$randomString .= $charsList[rand(0, strlen($charsList))];
}
$finalCode = strtoupper($randomString);
$html = '<input type="hidden" name="'.$input_code_name.' "value=" '.$finalCode . ' " />';
return $html;
}
Based on this method
https://www.wpguru.com.au/generate-dynamic-tag-contact-form-7/
However it does not seem to work for me, I added the code to functions and tried a bunch of editing to it with no definitive results.
Main idea is that I need a unique string of characters for each CF7 submission, all submissions are stored into Wordpress and also a copy of the data is sent to another DB.
Adding the shortcode [coupon_code] to mail template returns nothing and no data seems to get stored trough the field at all.
What you have should essentially work. I made some tweaks and optimized some of your code by removing some extraneous declarations and there were also extra spaces around the values.
add_action( 'wpcf7_init', 'custom_code_generator' );
function custom_code_generator() {
wpcf7_add_form_tag( 'coupon_code', 'custom_code_handler' );
}
function custom_code_handler() {
$characters = '1234567890abcdefghijklmnopqrstuvwxyz';
$random_string = '';
for ( $i = 4; $i < 10; $i++ ) {
$random_string .= $characters[ wp_rand( 0, strlen( $characters ) ) ];
}
return '<input type="hidden" name="rand_string" value="' . strtoupper( $random_string ) . '" />';
}

Saving add_meta_box data to multiple post_types

I have the following code to create and save custom meta boxes within my Wordpress site. This solution works fine for my 'work' post_type, but will not save on a standard 'post'. The meta boxes appear fine on both post_types, but will only save on 'work'. Therefore I believe the issue lies within the save function, but I cannot decipher any reason why this works on only the one post_type?
I have pared the below code down to what I believe is the essential code.
Thanks.
// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_custom_metaboxes' );
// Register the Project Meta Boxes
function add_custom_metaboxes() {
$post_types = array ( 'post', 'work' );
foreach( $post_types as $post_type )
{
add_meta_box('xx_introduction', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
add_meta_box('xx_more', 'More', 'xx_more', 'work', 'normal', 'high');
}
}
// Add Introduction Metabox
function xx_introduction() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="introductionmeta_noncename" id="introductionmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the introduction data if its already been entered
$introduction = get_post_meta($post->ID, '_introduction', true);
// Echo out the field
echo '<textarea name="_introduction" class="widefat" rows="5">' . $introduction . '</textarea>';
}
// Add More Metabox
function xx_more() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="moremeta_noncename" id="moremeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the more data if its already been entered
$more = get_post_meta($post->ID, '_more', true);
// Echo out the field
echo '<textarea name="_more" class="widefat" rows="5">' . $more . '</textarea>';
}
// Save the Metabox Data
function xx_save_custom_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['introductionmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$introduction_meta['_introduction'] = $_POST['_introduction'];
$more_meta['_more'] = $_POST['_more'];
// Add values of $introduction_meta as custom fields
foreach ($introduction_meta as $key => $value) { // Cycle through the $testimonial_meta array
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
// Add values of $more_meta as custom fields
foreach ($more_meta as $key => $value) { // Cycle through the $more_meta array
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'xx_save_custom_meta', 1, 2); // save the custom fields
Having experimented with this for a couple of days, I have a semi solution to the issue I was experiencing. In the original code, I was attempting to register three meta boxes, but placing two of them in multiple post types ($post_type) and the other in just a single post type (work). I have not determined the cause, but this is what was causing the meta data not to save in one of the post types.
Whilst this isn't a full answer with a detailed breakdown, it at least locates the source of the issue, and my initial thought that the issue lies within the save function is correct, I just don't know how to fix this myself. This solution works fine for me however so I won't be investing anymore time into this issue.
Hopefully this is of use to others at least as a starting point.
// Register the Project Meta Boxes
function add_custom_metaboxes() {
$post_types = array ( 'post', 'work' );
foreach( $post_types as $post_type )
{
add_meta_box('xx_introduction', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
add_meta_box('xx_more', 'Introduction', 'xx_introduction', $post_type, 'normal', 'high');
}
}

Passing values through url for checkbox fields with same name to get ?tag=tag1+tag2+tag3

I have Wordpress installed and I have this form on each category page sidebar:
<form name="tags" onChange="document.forms.tags.submit();">
<input type="checkbox" name="tag" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1<br>
<input type="checkbox" name="tag" value="tag2" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag2") { echo "checked";}?>>tag2
</form>
The goal is to create an url like /?tag=tag1+tag2 and so on.
With the above code I get the url like this: /?tag=tag1&tag=tag2.
I've searched for two weeks and tried a lot, but nothing works for me. I've tried for example
<input type="checkbox" name="tag[]" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1
but then i get ?tag%5B%5D=tag1 and Wordpress don't find any results.
The form submit each time a checkbox is checked.
If I use radio input fields, then it works great, because then there's one value each time, but I want to pass multiple values with the same name and render a url like /?tag=tag1+tag2+tag3+tag4 etc.
Can anybody help me with this problem, because I don't know how to get this work for me. Thanks!
this is what you want:
// This would output '/client/?s=word&foo=bar'
echo add_query_arg( 'foo', 'bar' );
take a look on add_query_arg() and get_query_var()
Consider on this example http://codepad.org/s08Jmseg
$url_string = $_SERVER['QUERY_STRING'];
// reuturn: tag=tag1+tag2+tag3
parse_str($url_string);
// $tag holds the value tag1+tag2+tag3
$string = urldecode( $tag );
$arr = explode(" ", $string);
$my_tag = "tag2";
if( in_array( $my_tag, $arr ) ) {
// $key
echo $my_tag . ' found!';
}
else {
echo "Tag Not Found!";
}
//print_r($arr);

Wordpress user profile display name - change from selectbox to textbox

Im trying to change the user profile "Display name publicly as" from a select box to a textbox. Any ideas on how this can be done? Couldnt find anything all all.
Two way to solve it. You edit the wordpress core file, which is not a good way. Or you can add an extra field on profile box using wordpress hook.
I am giving you the first way:
Go to wp-admin/user-edit.php
Open the file. Then find
<td>
<select name="display_name" id="display_name">
<?php
$public_display = array();
$public_display['display_nickname'] = $profileuser->nickname;
$public_display['display_username'] = $profileuser->user_login;
if ( !empty($profileuser->first_name) )
$public_display['display_firstname'] = $profileuser->first_name;
if ( !empty($profileuser->last_name) )
$public_display['display_lastname'] = $profileuser->last_name;
if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) {
$public_display['display_firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name;
$public_display['display_lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name;
}
if ( !in_array( $profileuser->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere
$public_display = array( 'display_displayname' => $profileuser->display_name ) + $public_display;
$public_display = array_map( 'trim', $public_display );
$public_display = array_unique( $public_display );
foreach ( $public_display as $id => $item ) {
?>
<option id="<?php echo $id; ?>"<?php selected( $profileuser->display_name, $item ); ?>><?php echo $item; ?></option>
<?php
}
?>
</select>
</td>
Change it with
<th><label for="display_name"><?php _e('Display name publicly as'); ?> </label></th>
<td><input type="text" name="display_name" id="display_name" value="<?php echo esc_attr( $profileuser->display_name ); ?>" class="regular-text" /></td>
But still this is not a good solution.
I came across this same issue and used a bit of javascript to solve it. Wordpress allows you to set the display_name field to anything you want, so you can use the following script (in your theme's functions.php file) to convert the select to a text field.
function change_display_name_to_textfield() {
echo "><div>"; // don't remove '>'
?>
<script>
jQuery(function($) {
// replace display_name select with input
$('select#display_name').after( '<input type="text" name="display_name" id="display_name" value="' + $('#display_name').val() + '" class="regular-text">' ).remove();
})
</script>
<?php
echo "</div"; // don't add '>'
}
// hook into new user and edit user pages
add_action( "user_new_form_tag", "change_display_name_to_textfield" );
add_action( "user_edit_form_tag", "change_display_name_to_textfield" );
Note the weird use of opening/closing tags. This is because those particular hooks execute just before the end of the opening form tag. So you need to close the form tag, print your script, then print a final tag without closing it.
For admin user edit pages, I found one of the easiest way:
add_action( 'personal_options', 'rm_personal_options' );
function rm_personal_options($profileuser){
if( !is_admin() ) return; ?>
<script type="text/javascript">
jQuery(document).ready(function($){
//$("input#nickname").parent().parent().hide();
$("select#display_name").replaceWith(function(){
return '<input type="text" name="display_name" id="display_name" value="'+$(this).val()+'" class="regular-text" style="width:25em" />';
});
});
</script>
<?php
}
Simply paste it into the functions.php of the theme file :)

Resources