Skip to content

Create Add validation for a custom URL user field at checkout #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions add-validation-for-user-url-checkout-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* This recipe adds validation for a URL User Field at Membership Checkout.
*
* title: Add Validation For User URL Field At Checkout
* layout: snippet
* collection: checkout
* category: user fields
* link: tbd
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_check_valid_urls( $pmpro_continue_registration ) {
if ( ! $pmpro_continue_registration ) {
return $pmpro_continue_registration;
}

$url_fields = array(
'user_url' => 'Website', // replace user_url with the name of your field and Website with your field label
);

$error_messages = array();

foreach ( $url_fields as $field_name => $label ) {
if ( isset( $_REQUEST[ $field_name ] ) ) {
$url = sanitize_text_field( $_REQUEST[ $field_name ] );

if ( ! empty( $url ) && ! preg_match( '/^https?:\/\//i', $url ) ) {
$pmpro_continue_registration = false;
$error_messages[] = 'Please enter a full URL for "' . esc_html( $label ) . '" starting with http:// or https://'; // Set your preferred starting sentence for the error displayed
}
}
}

// Show all error messages
if ( ! empty( $error_messages ) ) {
pmpro_setMessage( implode( '<br>', $error_messages ), 'pmpro_error' );
}

return $pmpro_continue_registration;
}

add_filter( 'pmpro_registration_checks', 'my_pmpro_check_valid_urls' );