getolympus/olympus-dionysos-field-link

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

Installs: 215

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Language:JavaScript

Type:olympus-field

v0.0.22 2020-05-03 00:43 UTC

README

Dionysos Link Field

Olympus Component CodeFactor Grade Packagist Version MIT

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

composer require getolympus/olympus-dionysos-field-link

Table of contents

Field initializationVariables definitionTexts definitionRetrive dataRelease HistoryContributing

Field initialization

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

return \GetOlympus\Dionysos\Field\Link::build('my_link_field_id', [
    'title'       => 'Never gonna give you up!',
    'default'     => [
        [
            'url'    => 'https://www.youtube.com/watch?v=oVTPg9iicy4',
            'label'  => 'Never gonna get you down!',
            'target' => '_blank',
        ],
    ],
    'description' => 'You\'ve been Rick rolled!',
    'multiple'    => false,

    /**
     * Texts definition
     * @see the `Texts definition` section below
     */
    't_addblock_title'        => 'Click on the edit button',
    't_addblock_description'  => 'Click on the "+" button to add your link.',
    't_addblocks_description' => 'Click on the "+" button to add a link item.',
    't_addblock_label'        => 'Add',
    't_editblock_label'       => 'Edit',
    't_removeblock_label'     => 'Remove',
]);

Variables definition

Notes:

  • Set multiple to true to enable the "Add link" button

Texts definition

Retrive data

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

{
  "1": {
    "url": "https://www.google.com",
    "label": "Google.com",
    "target": "_self"
  },
  "2": {
    "url": "https://www.yahoo.com",
    "label": "Yahoo.com",
    "target": "_blank"
  }
}

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

// Get links from Database
$links = get_option('my_link_field_id', []);

// Check if links are empty
if (!empty($links)) {
    // Build HTML list
    echo '<ul>';

    foreach ($links as $link) {
        // Build HTML items
        echo '<li>';
        echo '<a href="'.$link['url'].'" target="'.$link['target'].'" title="'.esc_html($link['label']).'">';
        echo $link['label'];
        echo '</a>';
        echo '</li>';
    }

    echo '</ul>';
}

Release history

Contributing

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