getolympus/olympus-dionysos-field-background

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

Installs: 129

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Language:JavaScript

Type:olympus-field

v0.0.3 2020-04-04 21:10 UTC

This package is auto-updated.

Last update: 2024-04-05 06:45:29 UTC


README

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

composer require getolympus/olympus-dionysos-field-background

Olympus Component CodeFactor Grade Packagist Version MIT

field-background-64.png

Field initialization

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

return \GetOlympus\Dionysos\Field\Background::build('my_background_field_id', [
    'title'       => 'Ooh, got ourselves an epic now...',
    'can_upload'  => false,
    'default'     => [
        'background-attachment' => 'initial',
        'background-color'      => 'transparent',
        'background-image'      => 'https://vignette.wikia.nocookie.net/despicableme/images/1/1d/Kevin_minions.png/revision/latest/scale-to-width-down/350?cb=20170703052012',
        'background-position'   => 'left top',
        'background-repeat'     => 'no-repeat',
        'background-size'       => 'cover',
        'height'                => '200px',
        'width'                 => '100px',
    ],
    'description' => 'Oh crap, I got knocked!',
    'size'        => 'thumbnail',

    /**
     * Settings definition
     * @see the `Settings definition` section below
     */
    'settings' => [],
]);

Variables definition

Variable Type Default value if not set Accepted values
title String 'Background' empty
can_upload Boolean false true or false
default Array empty empty
description String empty empty
settings Array [] see Settings definition
size String 'thumbnail' image sizes from add_image_size() WordPress function

Notes:

  • can_upload value is defined thanks to current_user_can('upload_files') (see WordPress reference)

Settings definition

The settings variable is an array of options defined below:

Variable Type Default value if not set Accepted values
upload Boolean true true or false
color Array [] see WordPress reference
upload Array [] see WordPress reference

Retrive data

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

{
  "background-attachment": "initial",
  "background-color": "transparent",
  "background-image": "https://vignette.wikia.nocookie.net/despicableme/images/1/1d/Kevin_minions.png/revision/latest/scale-to-width-down/350?cb=20170703052012",
  "background-position": "left top",
  "background-repeat": "no-repeat",
  "background-size": "cover",
  "height": "200px",
  "width": "100px"
}

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

// Get background from Database
$background = get_option('my_background_field_id', []);

// Check if background is empty and display it
if (!empty($background)) {
    $style = '';

    foreach ($background as $attr => $value) {
        $style .= $attr.':'.$value.';';
    }

    echo '<div style="'.$style.'">My content</div>';
}

Release History

0.0.3

  • Add height and width

0.0.2

  • Add display
  • Add JS integration

0.0.1

  • Initial commit

Contributing

  1. Fork it (https://github.com/GetOlympus/olympus-dionysos-field-background/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.