Skip to content

Modifying The Instance

Michiel Tramper edited this page Mar 28, 2018 · 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, 
    array $values, 
    string $id = '', 
    string $section = '', 
    string $field = '', 
    string $key = '' 
);

The string type reflects the type of frame that needs to be edited. The values are the values that are going to replace the existing values. 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 may be accessed and altered.

$fonts = MakeitWorkPress\WP_Custom_Fields\Framework::$fonts;
$icons = MakeitWorkPress\WP_Custom_Fields\Framework::$icons;
Clone this wiki locally