Skip Navigation

[Resolved] Creating my own custom field types

This support ticket is created 4 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by edmundl 4 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#1707569

Hi,

I want to create a totally new custom field type using PHP.
After reading this thread : https://toolset.com/forums/topic/creating-my-own-custom-field-types/
I know you don't provide custom codes support.
But I really want to know how to achieve my goal.
My goal basically same with the thread that I posted above but changing post reference to user reference. To explain more is, I want to build a pull down menu to select from a list of users with specific roles.

Thanks

#1707651
book-user-custom-field.png

Hi,

Welcome to Toolset support and I'd be happy to assist.

I'm afraid, there is no built-in feature, that can be used to define a new type of custom field, but you can use the "wpt_field_options" filter to dynamically add options to a "select" type custom field.
( ref: https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options )

For example, suppose that you have a "Books" post type and you need a "Book User" custom field for it, which should consist of dropdown options for all the "subscriber" role users.

In the custom field group for this Books post, you'll add a new "select" type custom field "Book User", which will only have a default option with an empty value.
( as shown in the attached screenshot )

Next, you can add a custom function linked to "wpt_field_options" filter, which can dynamically insert more options for all the "subscriber" role users, into this "Book User" field, using the "get_users" function.
( ref: https://developer.wordpress.org/reference/functions/get_users/ )

Example:


add_filter( 'wpt_field_options', 'add_user_options_func', 10, 3);
function add_user_options_func( $options, $title, $type )
{
    switch( $title )
    {
    case 'Book User':
        $args = array(
            'orderby' => 'display_name',
            'fields' => array( 'ID', 'display_name'),
            'role' => 'subscriber'
        );
        foreach( get_users($args) as $user ) {
            $options[] = array(
                '#value' => $user->ID,
                '#title' => $user->display_name,
            );
        }
        break;
    }
    return $options;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

I hope this helps and the custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1708353

Hi Waqar,

Thank you very much!
That is exactly what I want,
Just follow your answer step by step and my problem is resolved.

Best