New Filter – Uppercase first letter and make Title case

Home Forums Quform WordPress New Filter – Uppercase first letter and make Title case

This topic is: resolved
  • This topic has 5 replies, 2 voices, and was last updated 7 years ago by katw.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #25383
    katw
    Participant

    Hi Ally,

    I would like to create two custom field filters.

    1. change first letter in the field to UPPERCASE (for NAME FIELDS for example)
    2. change first letter of each discrete word to UPPERCASE (for address and title fields)

    Filter 2 (Title-case) use-case example: street address field with field data “unit 6/77 stone st” becomes “Unit 6/77 Stone St” and “1234 pacific hwy” becomes “1234 Pacific Hwy” etc

    For filter 1 (Uppercase first word) use-case: first name field “sue-anne” becomes “Sue-anne”

    I looked but couldn’t see an example to follow.

    Any suggestions?

    #25386
    katw
    Participant

    Don’t worry, I will do this client side.

    BTW might be worth adding these filters to future QuForm updates.

    And publishing a “how to make your own filter” as you have done for custom validators.

    Just an idea.

    #25403
    katw
    Participant

    I tried to create a new PHP filter for uppercase first letter.

    I modelled the code on your TRIM example and uploaded the new filter to the QUFORM > FILTERS directory.

    Unfortunately it didn’t work.

    Am guessing an Initialisation routine knows about the filters installed and didn’t like my addition. Or I have missed doing an important step.

    You probably don’t want to help with this solution as I am modifying your install files (which makes it problematic for me at time of upgrading).

    BUT if you did want to help here is the code I added to a file called UpperFirstLetter.PHP

    <?php
    
    /**
     * @copyright Copyright (c)2018 ThemeCatcher
     */
    class Quform_Filter_UpperFirstLetter extends Quform_Filter_Abstract
    {
        /**
         * Makes first letter of value uppercase
         *
         * If the value provided is not a string, the value will remain unfiltered
         *
         * @param   string  $value  The value to filter
         * @return  string          The filtered value
         */
        public function filter($value)
        {
            if ( ! is_string($value)) {
                return $value;
            }
    
            return ucfirst($value);
        }
    
        public static function getDefaultConfig()
        {
            $config = apply_filters('quform_default_config_filter_UpperFirstLetter', array());
    
            $config['type'] = 'UpperFirstLetter';
    
            return $config;
        }
    }
    #25412
    katw
    Participant

    Found I should also add my new filter definition to function getFilters()

    #25450
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    • This reply was modified 7 years ago by Ally.
    #25491
    katw
    Participant

    Fantastic, I will follow that same format to build a title case filter too.

    Thanks again.

    CLOSE TICKET

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy