Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds shortcode_ui_preview filter #769

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joseadrian
Copy link

Added shortcode_ui_preview filter to give the option to modify the preview in the WYSIWYG.

Some people want to have the option not to preview the content parsed in the editor because they might break the content, or use the dashicons instead.

The filter added will allow them to do that:

// $options = Shortcode UI Options
add_filter('shortcode_ui_preview', function($return, $shortcode_tag, $shortcode, $options) { 
    if($shortcode_tag == 'special_shortcode_tag') {
         return '<img src="/path/to/image">';
    }

    return sprintf( '<span class="dashicons %s"></span> %s', $options['listItemImage'], $options['label']);
}, 10, 4);

@joseadrian joseadrian force-pushed the custom-preview branch 2 times, most recently from f2e312c to 477f414 Compare August 29, 2017 18:54
Added `shortcode_ui_preview` filter to give the option to modify the preview in the WYSIWYG.
@davisshaver
Copy link
Member

davisshaver commented Sep 6, 2017

Hi @joseadrian, thanks for contributing. The filter seems reasonable to me. I'm a little suspicious of the new regex in there though. Would you be willing to move that into a utility function and add some test coverage to make sure it handles different shortcode configurations as you expect? Here's the chunk of code I'm looking at.

+		preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $shortcode, $matches );
+		$shortcode_tag = isset( $matches[1][0] ) ? $matches[1][0] : '';

@goldenapples
Copy link
Contributor

I was actually wondering if there wasn't a simpler way of doing this. We've talked about a few options that might work:

  1. Adding an argument to the shortcode_ui_register_for_shortcode args, like 'preview' => false, and, if that option is set, just showing the text of the shortcode in an mce view, rather than rendering the preview.
  2. Using the SHORTCODE_UI_DOING_PREVIEW constant to return an alternate rendition of a shortcode when rendering a shortcode preview for the editor. (This is already possible, by filtering pre_do_shortcode_tag or do_shortcode_tag and checking for the value of the SHORTCODE_UI_DOING_PREVIEW constant. But we could probably expose a utility method to make it easier.)

Would either, or a combination of these approaches fit your use case as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants