וורדפרס ואתרי אינטרנט

owl-158414_960_720
דורג 5 מתוך 5

באתר תמצאו מדריכים ומאמרים ייחודים בעברית, המדריכים לרוב מנסיון מעשי ומיוחסים לפרוייקטים שאני עובד עליהם באותו הזמן. האתר הוא יותר לשימוש פרטי ולתיעוד דברים, בכדי להשתמש מאוחר יותר, אבל החלטתי לפרסם ניתן לשאול כל שאלה וגם לתרום מדריכים באתר דרך תגובה או דרך צור קשר

כאשר רוצים לעשות ולידציה בטפסים של אלמנטור למשל להגביל את מספר הטלפון ל 9 ספרות ע"י regx אפשר להשתמש בקוד הבא.

https://github.com/elementor/elementor/issues/7580
add_action('elementor_pro/forms/validation/tel', function( $field, $record, $ajax_handler ) {

         // Match this format XXX-XXX-XXXX, 123-456-7890

         if ( preg_match( '/[0-9]{3}-[0-9]{3}-[0-9]{4}/', $field['value'] ) !== 1 ) {
                 $ajax_handler->add_error( $field['id'], 'Please make sure the phone number is in XXX-XXX-XXXX format, eg: 123-456-7890' );
         }
}, 10, 3 );

קוד נוסף למספר טלפון שמאפשר עוד ורסיות צריך לעשות התאמה לטלפונים בארץ…

מספר אפשרויות של פטרנים למספרים ישראלים 
אני מעדיף את השני 
//רק מספרים סלולאריים
/^05\d([-]{0,1})\d{7}$/

//כל המספרים
/^0\d([\d]{0,1})([-]{0,1})\d{7}$/

עוד מספרים כולל 972

^\+?(972|0)(\-)?0?(([23489]{1}\d{7})|[5]{1}\d{8})$
add_action( 'elementor_pro/forms/validation/tel', function( $field, $record, $ajax_handler ) {

    // Match this format XXX-XXX-XXXX, 123-456-7890

    if ( preg_match( '/(?:[0-9]-?){3}(?:[0-9]-?){3}(?:[0-9]-?){4}/', $field['value'] ) !== 1 ) {
            $ajax_handler->add_error( $field['id'], 'Please make sure the phone number is in XXX-XXX-XXXX or XXX-XXXXXXX format, eg: 123-4567890' );
    }
}, 10, 3 );

הרחבה של אלמנטור הוספת שדות.

ניתן להוסיף שדות מותאמים אישית ע"י הקוד הבא ואז לקבוע מה השדה יהיה.

<?php
    namespace ElementorPro\Modules\Forms\Fields;
    use ElementorPro\Base\Module_Base;
    use ElementorPro\Modules\Forms\Fields;
    use Elementor\Controls_Manager;
    use ElementorPro\Plugin;
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    
    class Wi_CustomFormField extends Field_Base {
        
        public function get_type() {
            return 'custom_field';
        }
        
        public function get_name() {
            return __( 'Your custom field', 'elementor-pro' );
        }
        
        public function render( $item, $item_index, $form ) {
          $form->add_render_attribute( 'input' . $item_index, 'class', 'elementor-field-textual' );

          $form->add_render_attribute( 'input' . $item_index, 'pattern', '[0-9()#&+*-=.]+' );
          $form->add_render_attribute( 'input' . $item_index, 'title', __( 'Only numbers and phone characters (#, -, *, etc) are accepted.', 'elementor-pro' ) );
          echo '<input ' . $form->get_render_attribute_string( 'input' . $item_index ) . '>';
        }
    
        public function validation( $field, Classes\Form_Record $record, Classes\Ajax_Handler $ajax_handler ) {
            if ( empty( $field['value'] ) ) {
                return;
            }
            /**
             * Your custom validation
             */
            if ( preg_match( '/^[0-9()#&+*-=.]+$/', $field['value'] ) !== 1 ) {
                $ajax_handler->add_error( $field['id'], __( 'Only numbers and phone characters (#, -, *, etc) are accepted.', 'elementor-pro' ) );
            }
        }
    }
    
    \ElementorPro\Modules\Forms\Module::instance()->add_form_field_type( 'custom_field', new Wi_CustomFormField() );
    
?>

קוד למספר טלפון ישראלי תקין ~~~~

add_action('elementor_pro/forms/validation/tel', function( $field, $record, $ajax_handler ) {
if ( preg_match( '/^0\d([\d]{0,1})([-]{0,1})\d{7}$/', $field['value'] ) !== 1 ) {
     $ajax_handler->add_error( $field['id'], 'מספר טלפון לא חוקי' );
 }
}, 10, 3 );

תפריט נגישות