Wordpress CF7 generate random unique string for each submision - wordpress

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 ) . '" />';
}

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>';
}
}

No access to the created input

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.

How to display currency in Indian numbering format in wordpress?

One of my client have real estate website to sell properties. It was built in wordpress using houzez theme. That website have default numbering system where the separator shows at every three characters eg: 10 lacks : 1,000,000 , but in indian currency format it should be like : 10,00,000 .
The Indian currency numbering system is looks like :
1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000
How do i change that numbering system in my wordpress website?
is there any plugin available?
Well, you can use the function numberToCurrencymade by hitswa
Then this function can be used in different ways:
Directly to the field: <?php echo numberToCurrency( get_post_meta( get_the_id(), 'price', true ) ); ?>
Apply a filter to the field assuming that your code to display the field is like this:
$unformatted_price = get_post_meta(get_the_id(), 'price', true);<br>
echo apply_filters('price_meta_content', $unformatted_price );
And on your functions.php <?php add_filter( 'price_meta_content', 'numberToCurrency' ) ; ?>
Another solution would be to add a filter to the_content
add_filter('the_content','strnumtocurrency', 99);
function strnumtocurrency($content){
$pattern = "/(\d+.\d+)/";
return preg_replace( $pattern, numberToCurrency('$1'), $content );
}
Here is the numberToCurrency code
<?php
function numberToCurrency($number)
{
if(setlocale(LC_MONETARY, 'en_IN'))
return money_format('%.0n', $number);
else {
$explrestunits = "" ;
$number = explode('.', $number);
$num = $number[0];
if(strlen($num)>3){
$lastthree = substr($num, strlen($num)-3, strlen($num));
$restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
$restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
$expunit = str_split($restunits, 2);
for($i=0; $i<sizeof($expunit); $i++){
// creates each of the 2's group and adds a comma to the end
if($i==0)
{
$explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
}else{
$explrestunits .= $expunit[$i].",";
}
}
$thecash = $explrestunits.$lastthree;
} else {
$thecash = $num;
}
if(!empty($number[1])) {
if(strlen($number[1]) == 1) {
return '₹ ' .$thecash . '.' . $number[1] . '0';
} else if(strlen($number[1]) == 2){
return '₹ ' .$thecash . '.' . $number[1];
} else {
return 'cannot handle decimal values more than two digits...';
}
} else {
return '₹ ' .$thecash.'.00';
}
}
}
?>

How to display WordPress RSS feed your website?

Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.
I want to show only 3 post on my website.
I want to automatically check for any new post everytime when i reload my website, so that the recent three gets displayed only.
I want to show the complete title of my wordpress blogpost but specific letters of description.
Also the description should end up with a word not some piece of non-dictionary word ending with "..."
How this can be done, i have heard that it can be done through RSS.
Can somebody help me?
To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the "...".
First we will make a script to trim the description and to put "..." in last:-
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
}
else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
}
else {
$output = $text;
}
return $output;
}
Now we will define the variables in which we store the values:-
$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)
for ($i=0; $i<3; $i++)
{
$item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}
?>
Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-
FIRST RECENT POST:
<?php echo $item_title[0]; ?>
<?php echo substrwords($item_description[0],70); ?>
SECOND RECENT POST:
<?php echo $item_title[1]; ?>
<?php echo substrwords($item_description[1],70); ?>
THIRD RECENT POST:
<?php echo $item_title[2]; ?>
<?php echo substrwords($item_description[2],70); ?>
Hope this can solve your problem. By the way Nice question.
Click here for the original documentation on displaying RSS feeds with PHP.
Django Anonymous's substrwords function is being used to trim the description and to insert the ... at the end of the description if the it passes the $maxchar value.
Full Code:
blog.php
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 100);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can easily put this in a separate PHP file (blog.php) and call it inside your actual page.
Example:
social.php
<h3>Latest blog post:</h3>
<?php require 'blog.php' ?>
Also, this code is plug-n-play friendly.
Why not use the Wordpress REST API to retrieve posts -
API URL is : https://public-api.wordpress.com/rest/v1/sites/$site/posts/
where $site is the site id of your wordpress blog
or else simply use this plugin -
http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html

how to remove read more when full text is on in wordpress?

the code looks like this
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
there is an option in wp-admin>settings>reading>For each article in a feed, show
if this is set to full text
the excerpt() must return full length article instead of specified length.
how to do this?
Good question! The answer is simple: write your own function!
Open up functions.php in your favorite editor and mash random buttons on your keyboard until you get something like this:
function my_awesome_excerpt ($post_id = false, $full = false, $length = 22) {
if (!$post_id) global $post;
else $post = get_post($post_id);
$text = $post->post_content;
if ($full) return $text;
else {
$text_array = explode(' ', $text);
$return_string = array();
for ($i = 0; $i <= $length; $i++)
array_push($return_string, $text_array[$i]);
$new_awesome_string = '<p>';
$new_awesome_string .= implode(' ', $return_string);
$new_awesome_string .= '</p><p class="readmore">';
$new_awesome_string .= '<a href="' . get_permalink($post_id) . '">';
$new_awesome_string .= 'Read More' . '</a></p>';
return $new_awesome_string;
}
}
Now, you're ready for the cool part. Once you're in your loop, you can write out some magic like this:
echo my_awesome_excerpt();
and it will automagically spit out an excerpt. It's using the global post variable and everything! You can even use it outside of the loop:
echo my_awesome_excerpt($cpt->ID, 22);
and set your own special length!
Or maybe you just know in your heart that it's not worth it, you just want to show the whole thing. How's that look?
Inside the loop, you're going to have to give it a post ID, sorry about that.
echo my_awesome_script($post->ID, false);
I hope this helps. Have a great day!

Resources