• Resolved baddon250

    (@baddon250)


    I’m working with the profile information form, and I’m trying to add a link to the error message. I tried pretty hard and havn’t been able to do it, and I havn’t found anything online relating to this.

    I’m trying to make the add_error function output an error message with a link like this.

    UM()->form()->add_error( $key, __( “Toll-Free # Not Added, Click Here to add to your profile.”, ‘ultimate-member’ ) );

    Any Ideas?

Viewing 15 replies - 16 through 30 (of 33 total)
  • @baddon250

    Yes UM is calling esc_attr for all error texts.

    Look at the function field_error in .../ultimate-member/includes/core/class-fields.php lines 479/502

    Maybe you should redirect the user to this page instead of displaying the link?

    • This reply was modified 3 years, 5 months ago by missveronica.
    Thread Starter baddon250

    (@baddon250)

    Is there any way you can make a change to that function to give it the option to bypass that esc_attr for custom options like this. I’m sure im not the first and I definitely won’t be the last. Forwarding is not an option because it would feel forceful and unexpected to the user.

    I would do it on my end, but then it would be rewritten the next update.

    Mayble replace esc_attr with wp_kses_post to allow for some basic html usage when the option is selected.

    • This reply was modified 3 years, 5 months ago by baddon250.

    @baddon250

    You can try to add this filter to your action code snippet:
    .../ultimate-member/includes/core/class-fields.php in line 3906:

    $output = apply_filters( "um_{$key}_form_edit_field", $output, $this->set_mode );

    Where $key is your field key and $output is the current HTML.
    Replace the link in your action code snippet with a placeholder like {add_on_not_enabled} which you replace with your link in this filter code snippet.

    Thread Starter baddon250

    (@baddon250)

    Do you think you could alter those 2 functions to be replaced with these 2 functions?

    		/**
    		 * Print field error
    		 *
    		 * @param string $text
    		 * @param bool   $force_show
    		 *
    		 * @return string
    		 */
    		function field_error( $text, $force_show = false, $allow_html = false ) {
    
    			if ( empty( $text ) ) {
    				return '';
    			}
    			
    			if($allow_html){
    				$data = wp_kses_post($text);
    			} else{
    				$data = esc_attr($text);
    			}
    
    			if ( $force_show ) {
    				$output = '<div class="um-field-error"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>';
    				return $output;
    			}
    
    
    			if ( isset( $this->set_id ) && UM()->form()->processing == $this->set_id ) {
    				$output = '<div class="um-field-error"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>';
    			} else {
    				$output = '';
    			}
    
    			if ( ! UM()->form()->processing ) {
    				$output = '<div class="um-field-error"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>';
    			}
    
    			return $output;
    		}
    
    
    		/**
    		 * Print field notice
    		 *
    		 * @param string $text
    		 * @param bool   $force_show
    		 *
    		 * @return string
    		 */
    		function field_notice( $text, $force_show = false, $allow_html = false ) {	
    			
    			if ( empty( $text ) ) {
    				return '';
    			}
    			
    			if($allow_html){
    				$data = wp_kses_post($text);
    			} else{
    				$data = esc_attr($text);
    			}
    
    			if ( $force_show ) {
    				
    				$output = '<div class="um-field-notice"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>';
    				return $output;
    			}
    
    
    			if ( isset( $this->set_id ) && UM()->form()->processing == $this->set_id ) {
    				$output = '<div class="um-field-notice"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>';
    			} else {
    				$output = '';
    			}
    
    			if ( ! UM()->form()->processing ) {
    				$output = '<div class="um-field-notice"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>';
    			}
    
    			return $output;
    		}

    Thread Starter baddon250

    (@baddon250)

    You’re probably going to end up doing something like this anyways, so maybe its a good time, because theres a lot of companies that run a SAAS busines model like we do, and one of our products resides on displaying certain fields on profiles.

    @baddon250

    No solution for UM is like my post above with a filter after the field_error function being called.

    Thread Starter baddon250

    (@baddon250)

    What do I do with $this->set_mode.

    Keep in mind I will have multiple different links with the associated fields below.

    One for Website.

    One for Phone Number

    One for Toll-Free Phone Number

    One Sales Icons

    One for Gallery Sorting

    Thread Starter baddon250

    (@baddon250)

    If I could run something inside the given functions I have, that would be best.

    Thread Starter baddon250

    (@baddon250)

    Here’s the function I’m working with. With $output being the error with link.

    function um_custom_validate_website( $key, $array, $args ) {
    	//$val = $args[$key];
    	$keys = array('page_url');
    	//website link 37081
    	
    	if(!isset($args[$key]) || empty($args[$key])){
    	} else{
    		if(is_active_subscription(37081)){//37082 = toll-free-number	
    		} else{
    			$link = get_site_url(). '/product/toll-free-phone-number';
    
    			//my_submit_form_error('Add-On Not Enabled, <a href="'. $link .'">CLICK HERE</a> to to get the Add-On.', $key);
    			$output = 'Add-On Not Enabled, Please go <b><a href="' . $link . '">HERE</a></b> to enable.';
    			UM()->form()->add_error( $key, __( "Add-On Not Enabled,  Go to your Profile Add-Ons on your Profile Menu to add.", 'ultimate-member' ) );
    		}
    	}
    	
    }
    add_action( 'um_custom_field_validation_validate_website', 'um_custom_validate_website', 30, 3 );

    @baddon250

    $this->set_mode contains the form type like register, login, password, profile, account

    Thread Starter baddon250

    (@baddon250)

    I’m getting this error when I try to run that filter.

    PHP message: PHP Fatal error: Uncaught Error: Using $this when not in object context

    $key is set to “user_url”

    • This reply was modified 3 years, 5 months ago by baddon250.
    • This reply was modified 3 years, 5 months ago by baddon250.

    @baddon250

    Yes replace $this->set_mode with $mode in your filter function parameter list.

    Thread Starter baddon250

    (@baddon250)

    The php error went away, but it’s not showing the updated error message for the user

    • This reply was modified 3 years, 5 months ago by baddon250.
    Thread Starter baddon250

    (@baddon250)

    That code is not making the url show on the error message. Here is my code.

    
    function um_custom_validate_website( $key, $array, $args ) {
    	//$val = $args[$key];
    	$keys = array('page_url');
    	//website link 37081
    	
    	if(!isset($args[$key]) || empty($args[$key])){
    	} else{
    		if(is_active_subscription(37081)){//37082 = toll-free-number	
    		} else{
    			$link = get_site_url(). '/product/toll-free-phone-number';
    
    			//my_submit_form_error('Add-On Not Enabled, <a href="'. $link .'">CLICK HERE</a> to to get the Add-On.', $key);
    			$output = 'Add-On Not Enabled, Please go <b><a href="' . $link . '">HERE</a></b> to enable.';
    			UM()->form()->add_error( $key, __( "Add-On Not Enabled,  Go to your Profile Add-Ons on your Profile Menu to add.", 'ultimate-member' ) );
    		}
    	}
    	
    }
    $link = get_site_url(). '/product/toll-free-phone-number';
    $output = 'Add-On Not Enabled, Please go <b><a href="' . $link . '">HERE</a></b> to enable.';
    $key = "user_url";
    add_action( 'um_custom_field_validation_validate_website', 'um_custom_validate_website', 30, 3 );
    apply_filters( "um_{$key}_form_edit_field", $output, $mode );
    

    @baddon250

    You have not followed the code example in the documentation for the filter function:

    /**
     * UM hook
     *
     * @type filter
     * @title um_{$key}_form_edit_field
     * @description Change field HTML on edit mode by field $key
     * @input_vars
     * [{"var":"$output","type":"string","desc":"Field HTML"},
     * {"var":"$mode","type":"string","desc":"Fields Mode"}]
     * @change_log
     * ["Since: 2.0"]
     * @usage add_filter( 'um_{$key}_form_edit_field', 'function_name', 10, 2 );
     * @example
     * <?php
     * add_filter( 'um_{$key}_form_edit_field', 'my_form_edit_field', 10, 2 );
     * function my_form_edit_field( $output, $mode ) {
     *     // your code here
     *     return $output;
     * }
     * ?>
     */
Viewing 15 replies - 16 through 30 (of 33 total)

The topic ‘Help with Adding Error Message on Form’ is closed to new replies.