Skip to content

Modifying The Instance

Michiel Tramper edited this page Sep 3, 2019 · 4 revisions

WP Custom Fields revolves around the central instance that is created. This is the first step that is taken before the framework can be used. As it uses the singleton pattern, the framework is only initialized once.

 

Create an Instance

Before you can add fields, you have to create an instance of the WP_Custom_Fields framework in the following manner:

$fields = MakeitWorkPress\WP_Custom_Fields\Framework::instance();

If you are going to use the location field, you have to add your google maps api key to the instance in the following way:

$fields = MakeitWorkPress\WP_Custom_Fields\Framework::instance( array('google_maps_key' => 'your_google_maps_key') );

 

Retrieving an Instance

On any other location in the code, the instance may be retrieved in the same way as creating an instance.

$fields = MakeitWorkPress\WP_Custom_Fields\Framework::instance();

It is possible to retrieve data for fields using the get method.

$fields->get( string $type = 'all' );

A type can be passed, which can be 'options', 'meta'' or 'customizer'.

$fields->get('options');

This will retrieve the data for all registered option pages.

 

Adding Fields to the Instance

It is possible to add fields using the add method. This is also explained in the homepage of the wiki.

$fields->add( string $type, array $values );

 

Editing Fields to the Instance

It is possible to edit existing groups, sections or fields using the edit method.

$fields->edit( 
    string $type, // The options type
    array $values, // The values for replacement 
    string $id = '', // The id for the settings group  that could be edited
    string $section = '', // The id for the section that could be edited
    string $field = '',  // The id for the field that could be edited
    string $key = '' // The key of the field that could be edited
);

For example, if you want to edit the title of the field with an id of 'field_one' in the section with an id of 'section_one' and a settings group with the id of 'settings', it will look like the following:

$fields->edit( 
    'options',
    __('New Field Title', 'textdomain'), 
    'settings',
    'section_one', 
    'field_one', 
     'title' 
);

It is also possible to replace complete sections or even setting groups by only setting the ids for those parameters. The array of values should match the given level.

 

Public Properties

The static properties for icons and fonts are public. These may be accessed.

$fonts = MakeitWorkPress\WP_Custom_Fields\Framework::$fonts;
$icons = MakeitWorkPress\WP_Custom_Fields\Framework::$icons;