Support

Account

Forum Replies Created

  • Hi John,

    Yes, the plugin is already initialized. Even when I put that ACF filter within functions.php, it still does not do the validation if I put in non-numeric characters.

  • I tried two things:

    1. I put it at the end of my main WordPress functions.php file.
    2. I put it in the __construct function of the plugin file I am working with (the main plugin.php) file. For context, it looks like this. Note: I’ve replaced the actual plugin name with a generic “plugin” name.

    File: plugin.php
    Code:

    
    <?php
    /**
     * @package Plugin
     * @version 1.0.0
     */
    /*
    Plugin Name: Plugin
    Plugin URI: https://plugin.com/
    Description: Plugin Description
    Author:  Me
    Version: 1.0.0
    Author URI:  https://plugin.com/
    */
    
    class plugin
    {
    	public static $version = '';
    
        /**
         * pluginconstructor.
         *
         * The main plugin actions registered for WordPress
         */
        public function __construct()
        {
    
    add_filter(‘acf/validate_value/name=sell_price’, ‘allow_only_floats’, 20, 4);
    add_filter(‘acf/validate_value/name=buy_price’, ‘allow_only_floats’, 20, 4);
    
    function allow_only_floats($valid, $value, $field, $input) {
    if (!$valid) {
    return $valid;
    }
    if (preg_replace(‘/[^0-9.]/’, $value)) {
    return ‘Only numbers are accepted’;
    }
    return $valid;
    }
    
  • I did that but for some reason it made the field disappear. What I really want to do is to strip non-numeric characters before it is submitted.

    I looked at this forum thread that you previously answered before John: https://support.advancedcustomfields.com/forums/topic/limit-field-value-to-letters-only-with-no-numbers/ and it did not work. This is what I did:

    1. I placed this code into the __construct function of the main plugin php file (the php file with the same name as the plugin). This is the code:

    add_filter(‘acf/validate_value/name=sell_price’, ‘allow_only_floats’, 20, 4);
    add_filter(‘acf/validate_value/name=buy_price’, ‘allow_only_floats’, 20, 4);

    function allow_only_floats($valid, $value, $field, $input) {
    if (!$valid) {
    return $valid;
    }
    if (preg_replace(‘/[^0-9.]/’, $value)) {
    return ‘Only numbers are accepted’;
    }
    return $valid;
    }

    2. When I put in non-numeric characters, it was still being saved with those non-numeric characters.
    3. I’m not really sure what the “20” and “4” values mean, but I just copied it from your previous form post.

    Where could I go from here?

Viewing 3 posts - 1 through 3 (of 3 total)