Skip to content

Setup: shortcode admin

Justin Sternberg edited this page Mar 19, 2016 · 10 revisions

Your Admin file must extend WDS_Shortcode_Admin in order to use all the fancy stuff we have.

Required Methods

Since WDS_Shortcode_Admin is an abstracted class, you are required to setup the following methods in your sub-classes:

  • js_button_data()
  • fields()

js_button_data()

Below is a sample config for the js_button_data() method of your admin class.

/**
 * Sets up the button
 *
 * @return array
 */
function js_button_data() {
	return array(
		'qt_button_text' => __( 'Quick Tag Button Text', 'textdomain' ),
		'button_tooltip' => __( 'Visual Button Tooltip', 'textdomain' ), // Also title of the popup
		'icon'           => 'dashicons-slides',
		'include_close'  => true, // default false - if true, shortcode has closing tag
	);
}

Defaults
These can be overwritten in your own js_button_data() method should you see the need.

  • 'author' - Used by TinyMCE
  • 'authorurl' - Used by TinyMCE
  • 'infourl' - Used by TinyMCE
  • 'version' - Version String
  • 'l10ncancel' - Cancel button text
  • 'l10ninsert' - Insert button text

Additional Overrides
Below are some additional params you can specify should you see the need

  • 'dialogClass' - Change/add-to the popup classes
  • 'modalHeight' - Set the height of the modal - default auto
  • 'width' - Sets the width of the modal - default 500

fields()

Example fields() method using CMB2 fields.

/**
 * Adds fields to the button modal using CMB2
 *
 * @param $fields
 * @param $button_data
 *
 * @return array
 */
function fields( $fields, $button_data ) {
	$fields[] = array(
		'name' => __( 'Key Field', 'textdomain' ),
		'desc' => __( 'This is a description, catchy huh!', 'textdomain' ),
		'type' => 'text',
		'id'   => 'some_default_key',
	);

	$fields[] = array(
		'name' => __( 'Some Image', 'textdomain' ),
		'type' => 'file',
		'id'   => 'attachment_image',
	);

	return $fields;
}

Overall, there's nothing fancy going on with fields, it's how you would expect, just an array of CMB2 fields.

What about non-CMB2 installations?

If you would like to have your own callbacks, or even your own JavaScript file for a shortcode, you can overload the _additional_args() method in your sub-class which by default looks like so:

protected function _additional_args() {

	// Optional additional parameters
	$additional_args = array(
		// Can be a callback or metabox config array
		'cmb_metabox_config'   => array( $this, 'shortcode_button_cmb_config' ),
		// Set the conditions of the shortcode buttons
		'conditional_callback' => array( $this, 'conditional_callback' ),

		// Use if you are not using CMB2 to generate the form fields
		// 'form_display_callback' => '',
	);

	return $additional_args;
}

Even by overloading this method, since the class is abstracted, you will still need to at minimum set your fields() method to an empty method.

If you are having a problem with the Shortcode_Button asset (JS/CSS) URLs being incorrect, you can use the shortcode_button_assets_url filter to correct them. The correct URL should look something like this: YOURSITE.COM/wp-content/plugins/wds-shortcodes/vendor/jtsternberg/shortcode-button/js/shortcode-button.js.

Clone this wiki locally