Skip to content

Render shortcode previews within Gutenberg #1054

Closed

Description

@notnownikki

I thought we could use server side rendering to have a base settings object for shortcode content, and extend it for specific shortcodes.

Looking at the embed block as an example of how content can be rendered server-side, and speed up the creation of new blocks (the embed block supports all embeds that WP_oEmbed does, simply by using WP_oEmbed server side for rendering) I thought we could do something similar for shortcodes.

We'd need a new API endpoint to render shortcodes, and a base settings object would take values from the block properties and produce a shortcode as it would be entered in a post, e.g. [myshortcode foo="bar" bar="bing"]

The base settings' save would just construct the shortcode itself, in the same way that the embed block just saves the URL to be embedded.

Each concrete shortcode block would only have to add the UI for inputting the values needed for the shortcode, and this would allow us to create a large number of blocks in a short time. (We could even automate the creation of the blocks based on the shortcodes registered, and refine the UI manually.)

Activity

  1. aduth commented on Jun 7, 2017

    @aduth
    Member

    Related: #334

  2. westonruter commented on Jun 7, 2017

    @westonruter
    Member

    Would be good to loop-in Shortcake contributors here. They've done a ton of work on adding a shortcode UI to the editor, creating what I think is a great prototype for blocks itself. They'd be able to share a lot of knowledge for how they went about previewing shortcodes in the editor, as well as providing a UI for manipulating the shortcode attributes.

  3. mattheu commented on Jun 15, 2017

    @mattheu
    Contributor

    Hey, I'll share some of our experiences with Shortcake.

    I thought we could use server side rendering to have a base settings object for shortcode content, and extend it for specific shortcodes.

    We took exactly this approach. We used the WordPress core Tiny MCE views functionality and created a generic shortcode view that makes an AJAX request to fetch the shortcode preview fully rendered in PHP.

    I think this solution generally works well. It means that most Shortcodes just work out of the box and developers can customise the style in their editor stylesheet if necessary.

    What are the downsides?

    1. Simple HTML shortcodes work great, but anything with JS is more problematic. We added hooks to our render code that allowed developers to get things working by loading their JS. Sometimes this was as simple as loading the JS dependencies and initializing the functionality. But often this wasn't so easy. For example, the front end JS often makes assumptions about the page DOM structure which doesn't hold true when rendered in TinyMCE. However I feel like this was still a reasonable solution - and it was mostly possible to get things working, only it didn't 'just work' as easily as promised.
    2. Shortcodes often rely on the context of the WordPress page. e.g. they call is_singular() or get_the_ID(). Whilst it's possible to spoof this and try to get things working, its not so nice. We went as far as setting global post.. I know @westonruter has thought about this problem a bit so maybe he has some ideas :).
    3. The preview was sometimes a little slow to render. I'd love for the interface to be snappier but because it had to wait for a request to complete, we had to display loading animation as the preview whilst this happened. It might be nice to allow developers to register a client side preview for shortcodes if they want to, and using the AJAX/PHP render as a default.

    Other thoughts

    Using the core TinyMCE Views meant we actually didn't need to do all that much to get this working. That handles some smart things such as sandboxing previews that contain JS inside an iframe and resizing them. If you're implementing this from scratch, that is probably something you'll need to think about.

  4. westonruter commented on Jun 15, 2017

    @westonruter
    Member

    Shortcodes often rely on the context of the WordPress page. e.g. they call is_singular() or get_the_ID(). Whilst it's possible to spoof this and try to get things working, its not so nice. We went as far as setting global post.. I know @westonruter has thought about this problem a bit so maybe he has some ideas :).

    Yeah, this is the problem space that Selective Refresh in the Customizer addresses. It ensures the proper context for a given post by doing a request to the specific URL that the partial will be rendered on, so the $wp and $wp_query globals will be set as expected, but then at template_redirect it short-circuits and just renders the partials instead of proceeding with rendering the normal template.

  5. aduth commented on Jun 15, 2017

    @aduth
    Member

    Related: Some resizable / sandboxed iframe exploration previously at #754 (since closed).

    Originally sourced from Calypso: Resizable Iframe. The Calypso Shortcode component is an interesting example as well (manages server rendering and communication between sandboxed resizable iframe).

  6. notnownikki commented on Jun 15, 2017

    @notnownikki
    MemberAuthor
  7. notnownikki commented on Jun 15, 2017

    @notnownikki
    MemberAuthor
  8. mtias commented on Jun 22, 2017

    @mtias
    Member

    A simple first pass, and maybe all we need for now—since we want to encourage shortcode authors to port them to native blocks—is to have a "shortcode" block that gives you a textarea, maybe with [] delimiters in place that gets saved as is.

  9. westonruter commented on Jun 23, 2017

    @westonruter
    Member

    @mtias I suppose something similar should be done for making legacy widgets available as blocks? In that case, we'd have the widget form that could be displayed in the inspector pane.

  10. dgwyer commented on Jun 30, 2017

    @dgwyer
    Contributor

    Is there any info yet on converting shortcodes to native blocks? I'm looking to write some new shortcodes and want them to be fully compatible with Gutenberg from the start.

    Or, would it be better to focus on creating custom blocks instead?

  11. westonruter commented on Jul 8, 2017

    @westonruter
    Member

    @dgwyer You should probably create both a shortcode and a block for now, but have them share as much logic as possible, such as in the rendering function.

  12. jasmussen commented on Aug 18, 2017

    @jasmussen
    Contributor

    Since shortcodes work in text blocks and Classic Text, and there's also a separate shortcode block, can we close this as addressed for now?

  13. notnownikki commented on Aug 18, 2017

    @notnownikki
    MemberAuthor

    If we're happy with how the shortcode block renders, I guess we can. Although I would have liked to have seen rendering done in the way the embed blocks do, where instead of a placeholder, they show the rendered content as it would appear in the post. I get that it's tricky to do though, so I'll leave this up to someone else to decide.

  14. jasmussen commented on Aug 18, 2017

    @jasmussen
    Contributor

    I think that's a fine reason to keep this open. Adding an "Enhancement" label.

  15. 7 remaining items

  16. jasmussen commented on Jan 25, 2018

    @jasmussen
    Contributor

    I like the idea of being able to preview shortcodes. But I think we should leverage the block design guidelines instead of using the tabbed interface, which I would consider a one-off for the custom HTML block.

    The key principle is that an unselected block is a preview, and a selected block can surface extra input fields and controls. For example a button block shows a URL input field when selected.

    Couldn't it work well if the shortcode block always showed a preview when unselected, and when selected it surfaced the input field for the shortcode itself?

    ASCII mockup:

    Selected shortcode block:

    [product_category]
    __________________
    
               
     Apple Products
    

    Unselected shortcode block:

               
     Apple Products
    
  17. mtias commented on Feb 6, 2018

    @mtias
    Member

    Couldn't it work well if the shortcode block always showed a preview when unselected, and when selected it surfaced the input field for the shortcode itself?

    I'm conflicted here, because it will signal users that these are just like any other content in their page, but shortcodes are edited abstractly.

  18. niranjan-uma-shankar commented on Feb 7, 2018

    @niranjan-uma-shankar

    Thanks for your thoughts, @mtias and @jasmussen!

    I'm conflicted here, because it will signal users that these are just like any other content in their page, but shortcodes are edited abstractly.

    I echo the same thoughts Matias - shortcodes are "different" given that they can be edited abstractly - the preview approach would signal this nicely. Would be good to hear @jasmussen's take on this. I can experiment with the block design UI too, if we feel having it makes more sense. Would be good to get opinions @notnownikki @youknowriad @aduth

    @mtias @jasmussen - it would be awesome if you could review the pull request I've referenced and give it a spin :)

  19. jasmussen commented on Feb 7, 2018

    @jasmussen
    Contributor

    Matías has convinced me. It was hard to put my finger on it, but shortcodes are code, just like HTML is code, which makes it different from visual blocks. As such, perhaps your initial instinct of tabs was correct. Okay to the tab preview 👍

  20. niranjan-uma-shankar commented on Feb 15, 2018

    @niranjan-uma-shankar

    While discussing with @ehg, an idea that came up is if we can automatically convert the shortcode block to an embed block or gallery block, depending on whether the shortcode entered is [embed] or [gallery]. Similarly, auto-convert to a video or audio block, if the shortcode entered is [video] or [audio]. This would impress upon the user that they should rather be using an available block, instead of using shortcodes to achieve the same effect. Putting this out here to gather thoughts from everyone.
    If the block convert idea appeals, it would be good to have UX advice from @jasmussen and @mtias on how this could be achieved. When do we prompt the user to make the block switch? Maybe when they click the preview tab, we can find the type of shortcode and ask the user if they would like to convert the block. However, what if the user doesn't click the preview tab, how do we then prompt him to switch block?
    We could add an option in the overflow menu specially for shortcode blocks, though I'm not convinced that would be a good option either.

  21. Lewiscowles1986 commented on Feb 23, 2018

    @Lewiscowles1986
    Contributor

    Has this solved the loading of scripts and localising? #5214 semi-solves although there can be a lot of noise from the theme and as-yet there is no front-end for gutenberg (I tested by pasting into a html block). Both things this seems to have, with less manual steps and a similar UI.

  22. Lewiscowles1986 commented on Feb 23, 2018

    @Lewiscowles1986
    Contributor

    any way I can help with this?

  23. westonruter commented on Feb 23, 2018

    @westonruter
    Member

    @Lewiscowles1986 review and test #4710.

  24. Lewiscowles1986 commented on Feb 24, 2018

    @Lewiscowles1986
    Contributor

    Been referred back here by @niranjan-uma-shankar I agree with #1054 (comment) (I know they changed their opinion).

    Design users are not always technical users, clicking the block (or clicking to edit) should allow technically competent users to alter a shortcode, but the value of a shortcode is it's output. We don't value to code of them, so I'd suggest it's easier to think about most things from an output point of view, rather than how the input is distinct.

    I've been experimenting and I prefer the default to preview view, with no toolbar, even as a technical user. Even though selecting the preview itself is a little less than easy with me having to use the right control to select the block from preview mode. I checked and that is the same with or without the tabs as present in the master branch of #4710

  25. danielbachhuber commented on May 8, 2018

    @danielbachhuber
    Member

    @notnownikki @westonruter Can we file this to the Ideas project and close? I don't think shortcode preview is a Gutenberg v1 requirement — developers that want previews should implement block-equivalents. Server-Side Rendering seems like it addresses our immediate needs.

  26. strarsis commented on Mar 19, 2020

    @strarsis
    Contributor

    +1 for shortcode/widget server-side preview rendering.

    I found two plugins that offer this functionality, so it is possible and the code is there:

  27. Lewiscowles1986 commented on Mar 25, 2020

    @Lewiscowles1986
    Contributor

    Hi @strarsis CD2 Gutenberg Shortcode Block Preview is Open Source and on GitHub for contributors.

    Early on, it caused a lot of frustration for me as most people I was working with that used WordPress jetisoned it; then because the JS API for blocks kept moving and I didn't have the workflow to keep up.

    The plugin is still tested on a demo WordPress I have, so it seems to work with modern WP; but If I'm honest I'd go a different route if your clients really need the features.

  28. strarsis commented on Jul 20, 2020

    @strarsis
    Contributor

    @Lewiscowles1986: Sadly the CD2 Gutenberg Shortcode Block Preview block has been blocked/deprecated in the WordPress Plugin directory.

  29. Lewiscowles1986 commented on Jul 20, 2020

    @Lewiscowles1986
    Contributor

    I blocked it as keeping up with the API changes and theme / plugin peculiarities for a plugin with such low uptake seems foolhardy and is frustrating. Especially if you receive no errors from the console when things fail, but a redirect loop.

    5.4 update saw a number of things stop working and I kinda feel like if I'm only using your API's and things constantly seem to break, then it feels like they are being deliberately broken.

    The code is still available via SVN and GitHub should you choose to build or fork the plugin it's got two parts.

    Automated testing of the plugin was always a problem as with most of WordPress, you're effectively e2e testing. That takes a fair amount of effort.

    Update
    I got it to build and with some minor edits (using de-construction for wp.url instead of npm package) things worked just as they did before, which is odd and frustrating. I still don't recommend it's use, but I published a new release on GitHub tested on latest 5.4 with 2020 theme. YMMV and I do think it is right this has closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions