Open nextgenthemes opened 7 years ago
I'm sorry, what are you referring to removing? You'd like a way of keeping the Add Post Element UI, but not showing the preview of the shortcode in the editor?
Exactly that yes. If someone has problems with the preview and thought I can offer a workaround by disabling it with a option.
Hmm. Might be worth offering a way of doing that.
When I've wanted to do that, I've always just shortcircuited the shortcode callback when in the admin.
function shortcode_callback( $attrs, $content = '' ) {
// Just return the shortcode text if in the editor
if ( is_admin() ) {
return '[shortcode ' .
array_reduce(
array_keys( $attr ),
function( $attr_string, $attr_key ) use ( $attrs ) {
return $attr_string . $attr_key . '"' . $attrs[ $attr_key ] . '" ';
}
)
. ']'
. ( !empty( $content ) ? $content . '[/shortcode]' : '' );
}
// otherwise do whatever the shortcode normally does...
}
A programmatic way of doing that, say with an option in the arguments passed to shortcode_ui_register_for_shortcode
, might be nice. How would you picture it working?
Passing an option to the arguments in shortcode_ui_register_for_shortcode
seems like a good way of doing this. It could be something like 'preview' => false
to disable the preview in the editor.
It could be filtered it in the handle_ajax_bulk_do_shortcode
function here https://github.com/wp-shortcake/shortcake/blob/master/inc/class-shortcode-ui.php#L412 but preferably the ajax call shouldn't be made at all if the preview is set to false.
Backbone/js is not my strong suit so I'm not sure exactly where to hook in but maybe in or before the shortcodeViewConstructor
https://github.com/wp-shortcake/shortcake/blob/master/js/src/utils/shortcode-view-constructor.js#L29
I could make a pull request, which implements this in the handle_ajax_bulk_do_shortcode
function, if you want me to, but then again, it seems like the less than optimal solution.
Don't do it for me. I opened this because I had a user who had problems with the preview. That was b4 the big update I think. Never heard back or another problem. This is low priority for me now until I get another support request from someone who has issues with the Shortcode UI. But I think I would personally use it because I don't need the preview and actually would prefer just to see the shortcode even in visual editor, but I am a dev not a user so I think they like it.
I originally opened this issue because I thought there is something like
remove_filter('something', 'scui_preview');
But if it would that would disable previews for the entire plugin and not playing nice with others so a option during registration for individual shortcodes sounds good. That code example looks hacky to me.
I think this is a good idea and would like it done. I have several use cases for it and generally only want the "Add Post Element" UI, which is really nice. The preview often creates more bloat in the editor as we typically do not include the css/js needed to style the shortcode output.
I'd consider this a blessed enhancement, and I agree with @rasmustaarnby above about the 'preview' => false
option making sense in the shortcode registration.
Trying to cram this logic into the handle_ajax_bulk_do_shortcode
callback does seem a bit hacky, though, since the shortcode UI attributes shouldn't need to be registered for the Ajax callback. Adding a bit of logic into shortcodeViewConstructor.prototype.initialize
so that if preview
is false on the shortcode model, we don't bother fetching the preview, makes the most sense to me.
+1 to that ;D
+1. I've tried to modify the call to 'handle_ajax_bulk_do_shortcode' but so far it just shows the placeholder icon showing that it's trying to render the shortcode. Would just be nice to disable it altogether, but I can't figure it out myself.
Better yet, is there a way to register a placeholder graphic for each shortcode? Just like the core wp image gallery shortcode used to do in the visual editor (i.e. show a gray box with a gallery icon on it). This way user can still benefit from clicking on object and editing it within UI shortcake modal window.
a way to register a placeholder graphic for each shortcode
Is there any reason we couldn't just use the shortcode icon for this? It's already possible to do a custom preview by checking is_admin in the shortcode and returning something different for the editor. For simply disabling the previews it'd be nice to just expose a simple flag argument like 'editor_preview
=> false`, in which case the editor view would render a gray box containing the shortcode icon (and possibly the text of the shortcode itself, for reference)?
I imagine I just remove some filters/actions.