Settings page: Adding a demo shortcode - dotherightthing/wpdtrt-plugin-boilerplate GitHub Wiki

Summary

DTRT WordPress Plugin Boilerplate supports display of a demo shortcode on the plugin settings page.

Status

  • Stable @ 1.3.2
  • Needs further work, see Issues

Usage

One demo shortcode may be displayed on the plugin settings page. This gives plugin users a preview of a real shortcode and displays the shortcode string required to achieve it.

Authoring a demo shortcode

demo_shortcode_params are set in the root plugin page, wpdtrt-plugin-boilerplatename.php.

Example from wpdtrt-map:

    $wpdtrt_map_plugin = new WPDTRT_Map_Plugin(
      array(
        ...
        'demo_shortcode_params' => array(
          'name' => 'wpdtrt_map_shortcode',
          'unique_id' => 1,
          'enlargement_link_text' => 'View larger version',
          'number' => 1,
          'mock_acf_map' => array(
            'address' => __('Seatoun School & Community Emergency Hub Burnham Street, Seatoun, Wellington, New Zealand', 'wpdtrt-map'),
            'lat' => '-41.3264776',
            'lng' => '174.83712689999993'
          )
        )
        ...
     )
    );
  }

Options

name required

(e.g. wpdtrt_map_shortcode)

The real shortcode name, registered within new DoTheRightThing\WPPlugin\Shortcode(...)

shortcode_parameter optional

(e.g. unique_id, enlargement_link_text, etc)

One or more custom shortcode options, added to the selected_instance_options array within new DoTheRightThing\WPPlugin\Shortcode(...)

number required

(e.g. 1)

Legacy option used internally, this will be removed at some point

mock_* optional

(e.g. mock_acf_map)

Mock data to replace data that is only available on posts/pages

Using mock data

Example from wpdtrt-map:

    public function get_acf_map() {

        // the map location is 'picked' using the ACF Map field
        $acf_map = get_field('wpdtrt_map_acf_google_map_location');

        // it can also be mocked using the demo_shortcode_params
        $demo_shortcode_options = $this->get_demo_shortcode_params();

        // real map embed on any page
        if ( ! $acf_map ) {
            // shortcode demo on options page
            if ( is_admin() && array_key_exists('mock_acf_map', $demo_shortcode_options) ) {
                $mock_acf_map = $demo_shortcode_options['mock_acf_map'];
                $address = $mock_acf_map['address'];
                $coordinates = $mock_acf_map['lat'] . ',' . $mock_acf_map['lng'];

                if ( isset( $address ) && isset( $coordinates ) ) {
                    $acf_map = $mock_acf_map;
                }
            }
        }

        return $acf_map;
    }