Issues in validating custom code in Contact Form 7 - php

I'm trying to implement a text field validation for field name VoucherNumber that requires the code to be in a certain pattern which is 'WWV-'followed by 4 numbers.
I was successfully able to implement this on google docs using the following expression ^[W]WV-[0-9][0-9][0-9][0-9].
I researched through various answers and attempted to add this code in functions.php but it didn't work. It would just show that the form is being sent (spinning wheel) but it would not be sent even after 5 minutes.
add_filter( 'wpcf7_validate_text*', 'validate_voucher_number', 20, 2 );
function validate_voucher_number( $result, $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}

Just make sure that your field name is VoucherNumber and try this code :
add_filter( 'wpcf7_validate_text', 'vouchervalidation', 20, 2 );
function vouchervalidation( $result, $tag ) {
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}

Related

how to apply following snippet to mobile only

the code:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}
I know I should add wp_is_mobile() somewhere but don't know where should I add it exactly.
If you want to use wp_is_mobile() then it just returns a boolean, so you could use it anywhere that wraps the output:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( wp_is_mobile() ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}
return $title;
}
But remember that if you use page caching a mobile user may be the one to generate the cache, and thus your cache will include the change and display everywhere. Given that this specific context is WooCommerce and therefore that maybe you're not caching the product pages if you need them to be dynamic somehow, this may work anyway, but #markus-ao's comment above would be a better solution if caching is an issue.

WooCommerce change order status using elementor forms

I am trying to build a form where when a user enters their order ID and unique serial code in the elementor form their order status is changed to completed. Heres the code i have written but i keep getting an error. The form has 2 fields 'a' & 'b'. A= order id and B= serial code.
add_action( 'elementor_pro/forms/validation', function ( $record, $ajax_handler ) {
$fields = $record->get_field( [
'id' => 'b'
], ['cid' => 'a'
]);
if ( empty( $fields ) ) {
return;
}
$field = current( $fields );
if ( 1 !== strlen( $field['value'] ) < 13 ) {
$ajax_handler->add_error( $field['id'], 'Invalid Serial Code, Please enter the 13 Digit Code mentioned on your delivery letter' );
} else {
$output['result'] = get_post_meta( $field['cid'], $key = 'shp_tkn', $single = true );
}
if ($output == $field['id']){
$order = $field['cid'] ;
$order->update_status('completed');
} else {
$ajax_handler->add_error( $field['id'], 'Invalid Serial Code, Please try again' );
}
}, 10, 2 );
If anyone could tell me where I'm going wrong.

Processing form data without nonce verification Error on my Function - Wordpress

Here is my function:
public function save_meta( $term_id = 0, $taxonomy = '' ) {
$meta = ! empty( $_POST['banner'] ) ? $_POST['banner'] : '';
if ( empty( $meta ) ) {
delete_term_meta( $term_id, 'banner' );
} else {
update_term_meta( $term_id, 'banner', $meta );
}
}
And When Travis review the code it tells me that
Processing form data without nonce verification.
| | (WordPress.CSRF.NonceVerification.NoNonceVerification)
I tried the following but is not working:
public function save_meta( $term_id = 0, $taxonomy = '' ) {
$meta = ! empty( $_POST['banner'] ) && wp_verify_nonce( sanitize_key( $_POST['banner'] ) ? $_POST['banner'] : '';
if ( empty( $meta ) ) {
delete_term_meta( $term_id, 'banner' );
} else {
update_term_meta( $term_id, 'banner', $meta );
}
}
What is wrong with my code?
The nonce is a hash of the user id, the session token, the current time and a tag generated by the function wp_create_nonce(). This hash is used to validate that the request is not conterfeit. In your case a suitable tag would be 'update-banner_' . $term_id. Your HTTP request should return this nonce as a query or post parameter. For form submission this is usually done by using a hidden field in the form. WordPress provides the convenience function wp_nonce_field() to do this. Your request handler should then verify this nonce using the function wp_verify_nonce() or the convenience function check_admin_referer(). Please read the WordPress documentation for details on calling these functions.

Bulk update custom post type at once in WordPress automatically

I have created a custom post type songs which has all the songs and now I want to sort it according to alphabets, which I have done using
function alphaindex_save_alpha( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
$slug = 'songs';
$letter = '';
if ( isset( $_POST['post_type'] ) && ( $slug != $_POST['post_type'] ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
$taxonomy = 'alpha';
if ( isset( $_POST['post_type'] ) ) {
$title = strtolower( $_POST['post_title'] );
// Get the first letter of the title
$letter = substr( $title, 0, 1 );
}
//set term as first letter of post title, lower case
wp_set_post_terms( $post_id, $letter, $taxonomy );
}
add_action( 'save_post', 'alphaindex_save_alpha' );
now this creates a taxonomy alpha which maps every songs based on the first alphabet for eg. Adele is mapped to A and so on.
I created this alphabet index after I had already put 2000 songs in my word press website and now for the songs to get mapped to the taxonomy I have to individually go to every post page and update the post and only then it appears.
But, doing this is a very tedious task and want a code that can do it automatically from the functions.php file.
I would recommend using posts_where filter instead of taxonomy. You do not need to add any extra data to your database to accomplish this.
add_filter('posts_where', 'starts_with_filter');
function starts_with_filter($sql){
if ( ! $query->is_main_query() || ! isset(get_query_var('starts_with')) {
return $query;
}
global $wpdb;
$startswith = substr ( get_query_var( 'starts_with' ), 0, 1);
$sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $startswith.'%' );
return $sql;
}

Wordpress Meta Box plugin, "Fatal Error: Call to undefined method"

Huge issue with this plugin!!!!
I updated Wordpress and everything and now I have this error so I can't edit some Wordpress pages:
“Fatal error: Call to undefined method RW_Meta_Box::get_class_name() in /home/wwwrcf/public_html/wp-content/plugins/meta-box-group/class-rwmb-group-field.php on line 63”
public static function html( $meta, $field )
{
ob_start();
// Add filter to child field meta value, make sure it's added only once
if ( empty( self::$meta_queue ) )
{
add_filter( 'rwmb_field_meta', array( __CLASS__, 'child_field_meta' ), 10, 3 );
}
// Add group value to the queue
array_unshift( self::$meta_queue, $meta );
// Add clone index to make sure each child field has an unique ID.
$clone_index = '';
if ( $field['clone'] && preg_match( '|_\d+$|', $field['id'], $match ) )
{
$clone_index = $match[0];
}
foreach ( $field['fields'] as $child_field )
{
$child_field['field_name'] = self::child_field_name( $field['field_name'], $child_field['field_name'] );
$child_field['attributes']['id'] = ( isset( $child_field['attributes']['id'] ) ? $child_field['attributes']['id'] : $child_field['id'] ) . $clone_index;
****line 63 -> call_user_func( array( RW_Meta_Box::get_class_name( $child_field ), 'show' ), $child_field, RWMB_Group::$saved );
}
// Remove group value from the queue
array_shift( self::$meta_queue );
// Remove filter to child field meta value and reset class's parent field's meta
if ( empty( self::$meta_queue ) )
{
remove_filter( 'rwmb_field_meta', array( __CLASS__, 'child_field_meta' ) );
}
return ob_get_clean();
}
Need to fix this.
Thanks for your help!

Categories