This component is a part of the Olympus Dionysos fields for WordPress.
It useswpColor
andwpMedia
WordPress javascript bundle to manage field.
composer require getolympus/olympus-dionysos-field-background
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' => [],
]);
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 tocurrent_user_can('upload_files')
(see WordPress reference)
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 |
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>';
}
0.0.3
- Add height and width
0.0.2
- Add display
- Add JS integration
0.0.1
- Initial commit
- Fork it (https://github.com/GetOlympus/olympus-dionysos-field-background/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
Built with ♥ by Achraf Chouk ~ (c) since a long time.