getolympus/olympus-dionysos-field-upload

Upload field, this component is a part of the Olympus Dionysos fields.

Installs: 194

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Language:JavaScript

Type:olympus-field

v0.0.16 2020-03-22 14:58 UTC

README

This component is a part of the Olympus Dionysos fields for WordPress.
It uses the default wpMedia WordPress javascript bundle to manage field.

composer require getolympus/olympus-dionysos-field-upload

Olympus Component CodeFactor Grade Packagist Version MIT

Field initialization

Use the following lines to add a upload field in your WordPress admin pages or custom post type meta fields:

return \GetOlympus\Dionysos\Field\Upload::build('my_upload_field_id', [
    'title'       => 'What\'s your flavor?',
    'can_upload'  => false,
    'default'     => [],
    'description' => 'Tell me what\'s your flavour!',
    'multiple'    => false,
    'type'        => 'image',
    'size'        => 'thumbnail',

    /**
     * Texts definition
     * @see the `Texts definition` section below
     */
    't_addblock_description'  => 'Click on the "+" button to add a media.',
    't_addblocks_description' => 'Click on the "+" button to add medias.',
    't_cannot_upload'         => 'It seems you are not able to upload files.',
    't_name_label'            => 'Edit the legend',
    't_addblock_label'        => 'Add',
    't_editblock_label'       => 'Edit',
    't_removeblock_label'     => 'Remove',
]);

Variables definition

Notes:

  • can_upload value is defined thanks to current_user_can('upload_files') (see WordPress reference)
  • Set multiple to true to enable the "Add medias" button

Texts definition

Retrive data

Retrieve your value from Database with a simple get_option('my_upload_field_id', []) (see WordPress reference). Below, a json_encode() example to understand how data are stored in Database:

{
  "mediaId": {
    "name": "mediaName.mediaExt",
    "id": "mediaId",
    "height": "mediaHeight",
    "url": "mediaUrl",
    "width": "mediaWidth"
  }
}

And below, a simple example to show how to iterate on the data array in PHP:

// Get upload from Database
$upload = get_option('my_upload_field_id', []);

if (!empty($upload)) {
    foreach ($upload as $img) {
        echo '<figure id="'.$img['id'].'">';
        echo '<img src="'.$img['url'].'" width="'.$img['width'].'" height="'.$img['height'].'" alt="'.esc_html($img['name']).'" />';
        echo '<figcaption>'.$img['name'].'</figcaption>';
        echo '</figure>';
    }
}

Release History

0.0.16

  • Remove useless admin scripts

0.0.15

  • Display now compatible with new Zeus-Core version

0.0.14

  • New Olympus components compatibility
  • Change repository to be a part of Dionysos fields

Contributing

  1. Fork it (https://github.com/GetOlympus/olympus-dionysos-field-upload/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Built with ♥ by Achraf Chouk ~ (c) since a long time.