Profile

Introduction

Profiles allow you to display custom fields in your user's profiles.

Creating Profile

By default, profiles are stored inside app\Profiles directory.

Via console command

Scaffold profile with wp assely:make profile command.

wp assely:make profile SocialProfiles
Specifying slug

This will create a profile with socialprofiles slug, but you can specify it with --slug option.

wp assely:make profile SocialProfiles --slug="social-profiles"
Specifying owners

You can also define to which singularity this profile belongs to. Enter it's classname to the --belongsto option.

wp assely:make profile SocialProfiles --belongsto="App\Users"

Manually created class file

Make a file with the class that extends base Assely\Profile\Profile class.

The slug property is required. This value determines under which name your metadata will be registered and accessible.

// app/Profiles/SocialProfiles.php

namespace App\Profiles;

use Assely\Profile\Profile;

class SocialProfiles extends Profile
{
    /**
     * Profile slug.
     *
     * @var string
     */
    public $slug = 'social-profiles';

    /**
     * Describe profile relationships.
     *
     * @return self
     */
    public function relation()
    {
        return $this->belongsTo(['App\Users']);
    }

    /**
     * Profile arguments.
     *
     * @return array
     */
    public function arguments()
    {
        return [
            //
        ];
    }

    /**
     * Register profile custom fields.
     *
     * @return \Assely\Field\Field[]
     */
    public function fields()
    {
        return [
            //
        ];
    }
}

Configuring Profile

Registering Profile

All profiles are registered in the config/app.php configuration file. This file contains a profiles array where you can add your newly created user's profiles.

'profiles' => [
    // ...other profiles

    App\Profiles\SocialProfiles::class,
],

Publishing Profiles for Users

By default, profile fields are private and visible only for administrators, but if you want to allow users to fill the fields themselves you need to change profile visibility to the public.

All you have to do is use Assely\Profile\MakePublic trait inside your created profile. That is it.

use Assely\Profile\MakePublic;

class SocialProfiles extends Profile
{
    use MakePublic;

    // rest of the profile class...
}

The Arguments Method

The arguments method must always return an array of profile registration parameters.

public function arguments()
{
    return [
        'title' => ['Social Profile', 'Social Profiles'],
        'description' => 'Links to the user social profiles.'
    ];
}
Parameter name Default value Description
title [] Array of titles, where the first element is a singular variant, second a plural.
description '' Description text displayed above all fields.
preserve 'single' How profile preserves his metadata.

The Relation Method

Profiles need to belongs somewhere. The relation method allows you to describe where profile should appear. Use belongsTo method with an array of profile owners as the parameter.

public function relation()
{
    return $this->belongsTo(['App\Users']);
}

The Fields Method

The fields method allows for listing custom fields which will be managed by profile.

use Field;

public function fields()
{
    return [
        Field::text('twitter_url')
    ];
}