Advertisement
moften

Custom form in Restrict content pro wordpress plugin

Apr 13th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1.  
  2.  
  3. Custom form in Restrict content pro wordpress plugin
  4.  
  5. // put this code on functions.php theme file
  6. // replace all variables (example: $telefono for your custom variable: $phone)
  7. // save and enjoy
  8.  
  9.  
  10.  
  11.  
  12.  
  13. /**
  14.  * Adds the custom fields to the registration form and profile editor
  15.  *
  16.  */
  17. function pw_rcp_add_user_fields() {
  18.    
  19.     $telefono = get_user_meta( get_current_user_id(), 'rcp_telefono', true );
  20.     $actividad   = get_user_meta( get_current_user_id(), 'rcp_actividad', true );
  21.     $dependencia   = get_user_meta( get_current_user_id(), 'rcp_dependencia', true );
  22.  
  23.     ?>
  24.     <p>
  25.         <label for="rcp_telefono"><?php _e( 'Teléfono', 'rcp' ); ?></label>
  26.         <input name="rcp_telefono" id="rcp_telefono" type="tel" value="<?php echo esc_attr( $telefono ); ?>"/>
  27.     </p>
  28.     <p>
  29.         <label for="rcp_actividad"><?php _e( 'Tu Actividad', 'rcp' ); ?></label>
  30.         <input name="rcp_actividad" id="rcp_actividad" type="text" value="<?php echo esc_attr( $actividad ); ?>"/>
  31.     </p>
  32.     <p>
  33.         <label for="rcp_dependendia"><?php _e( 'Dependencia', 'rcp' ); ?></label>
  34.         <input name="rcp_Dependencia" id="rcp_dependencia" type="text" value="<?php echo esc_attr( $dependencia ); ?>"/>
  35.     </p>
  36.     <?php
  37. }
  38. add_action( 'rcp_after_password_registration_field', 'pw_rcp_add_user_fields' );
  39. add_action( 'rcp_profile_editor_after', 'pw_rcp_add_user_fields' );
  40.  
  41.  
  42. /**
  43.  * Adds the custom fields to the member edit screen
  44.  *
  45.  */
  46. function pw_rcp_add_member_edit_fields( $user_id = 0 ) {
  47.  
  48.    
  49.     $telefono = get_user_meta( $user_id, 'rcp_telefono', true );
  50.     $actividad   = get_user_meta( $user_id, 'rcp_actividad', true );
  51.     $dependencia = get_user_meta( $user_id), 'rcp_dependencia', true);
  52.  
  53.     ?>
  54.  
  55.     <tr valign="top">
  56.         <th scope="row" valign="top">
  57.             <label for="rcp_telefono"><?php _e( 'Telefono ', 'rcp' ); ?></label>
  58.         </th>
  59.         <td>
  60.             <input name="rcp_telefono" id="rcp_telefono" type="text" value="<?php echo esc_attr( $telefono ); ?>"/>
  61.             <p class="description"><?php _e( 'Teléfono ', 'rcp' ); ?></p>
  62.         </td>
  63.     </tr>
  64.  
  65.  
  66.     <tr valign="top">
  67.         <th scope="row" valign="top">
  68.             <label for="rcp_actividad"><?php _e( 'Actividad', 'rcp' ); ?></label>
  69.         </th>
  70.         <td>
  71.             <input name="rcp_actividad" id="rcp_actividad" type="text" value="<?php echo esc_attr( $actividad ); ?>"/>
  72.             <p class="description"><?php _e( 'Actividad', 'rcp' ); ?></p>
  73.         </td>
  74.     </tr>
  75.  
  76.  
  77.     <tr valign="top">
  78.         <th scope="row" valign="top">
  79.             <label for="rcp_dependencia"><?php _e( 'Dependencia', 'rcp' ); ?></label>
  80.         </th>
  81.         <td>
  82.             <input name="rcp_dependencia" id="rcp_dependencia" type="text" value="<?php echo esc_attr( $dependencia ); ?>"/>
  83.             <p class="description"><?php _e( 'Dependencia', 'rcp' ); ?></p>
  84.         </td>
  85.     </tr>
  86.  
  87.     <?php
  88. }
  89. add_action( 'rcp_edit_member_after', 'pw_rcp_add_member_edit_fields' );
  90.  
  91.  
  92.  
  93. /**
  94.  * Determines if there are problems with the registration data submitted
  95.  *
  96.  */
  97. function pw_rcp_validate_user_fields_on_register( $posted ) {
  98.  
  99.     if ( is_user_logged_in() ) {
  100.        return;
  101.         }
  102.  
  103.     if( empty( $posted['rcp_telefono'] ) ) {
  104.         rcp_errors()->add( 'invalid_telefono', __( 'Escriba su teléfono ', 'rcp' ), 'register' );
  105.     }
  106.  
  107.     if( empty( $posted['rcp_actividad'] ) ) {
  108.         rcp_errors()->add( 'invalid_actividad', __( 'Escriba su actividad', 'rcp' ), 'register' );
  109.     }
  110.  
  111.     if( empty( $posted['rcp_dependencia'] ) ) {
  112.         rcp_errors()->add( 'invalid_dependencia', __( 'Escriba su dependencia', 'rcp' ), 'register' );
  113.     }
  114.  
  115. }
  116. add_action( 'rcp_form_errors', 'pw_rcp_validate_user_fields_on_register', 10 );
  117.  
  118.  
  119.  
  120.  
  121.  
  122. /**
  123.  * Stores the information submitted during registration
  124.  *
  125.  */
  126. function pw_rcp_save_user_fields_on_register( $posted, $user_id ) {
  127.  
  128.     if( ! empty( $posted['rcp_telefono'] ) ) {
  129.         update_user_meta( $user_id, 'rcp_telefono', sanitize_text_field( $posted['rcp_telefono'] ) );
  130.     }
  131.  
  132.     if( ! empty( $posted['rcp_actividad'] ) ) {
  133.         update_user_meta( $user_id, 'rcp_actividad', sanitize_text_field( $posted['rcp_actividad'] ) );
  134.     }
  135.  
  136.  
  137.     if( ! empty( $posted['rcp_dependencia'] ) ) {
  138.         update_user_meta( $user_id, 'rcp_dependencia', sanitize_text_field( $posted['rcp_dependencia'] ) );
  139.     }
  140.  
  141. }
  142. add_action( 'rcp_form_processing', 'pw_rcp_save_user_fields_on_register', 10, 2 );
  143.  
  144.  
  145. /**
  146.  * Stores the information submitted profile update
  147.  *
  148.  */
  149. function pw_rcp_save_user_fields_on_profile_save( $user_id ) {
  150.  
  151.     if( ! empty( $_POST['rcp_telefono'] ) ) {
  152.         update_user_meta( $user_id, 'rcp_telefono', sanitize_text_field( $_POST['rcp_telefono'] ) );
  153.     }
  154.  
  155.     if( ! empty( $_POST['rcp_actividad'] ) ) {
  156.         update_user_meta( $user_id, 'rcp_actividad', sanitize_text_field( $_POST['rcp_actividad'] ) );
  157.     }
  158.  
  159.     if( ! empty( $_POST['rcp_dependencia'] ) ) {
  160.         update_user_meta( $user_id, 'rcp_dependencia', sanitize_text_field( $_POST['rcp_dependencia'] ) );
  161.     }
  162.  
  163. }
  164. add_action( 'rcp_user_profile_updated', 'pw_rcp_save_user_fields_on_profile_save', 10 );
  165. add_action( 'rcp_edit_member', 'pw_rcp_save_user_fields_on_profile_save', 10 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement