Last active
January 9, 2017 21:00
-
-
Save westonruter/cdcda77f50bb752c5df2f50c8e4ce1dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: JS Widgets: Standalone Form Shortcode | |
*/ | |
add_action( 'wp_enqueue_scripts', function() { | |
wp_enqueue_style( 'js-widget-form' ); | |
} ); | |
add_shortcode( 'js-widget-standalone-form', function( $atts ) { | |
global $wp_widget_factory; | |
if ( empty( $atts['id_base'] ) ) { | |
return; | |
} | |
$widget = null; | |
foreach ( $wp_widget_factory->widgets as $widget_instance ) { | |
if ( $atts['id_base'] === $widget_instance->id_base ) { | |
$widget = $widget_instance; | |
break; | |
} | |
} | |
if ( ! $widget || ! ( $widget instanceof WP_JS_Widget ) ) { | |
return; | |
} | |
ob_start(); | |
$widget->enqueue_control_scripts(); | |
$widget->render_form_template_scripts(); | |
$id = (string) rand(); | |
?> | |
<div id="<?php echo esc_attr( $id ) ?>"></div> | |
<script> | |
jQuery( function( $ ) { | |
var container = $( <?php echo wp_json_encode( '#' . $id ) ?> ); | |
var idBase = <?php echo wp_json_encode( $widget->id_base ) ?>; | |
var form = new wp.widgets.formConstructor[ idBase ]( { | |
model: new wp.customize.Value( { title: 'Hello world', text: 'Foood', filter: true } ), | |
container: container | |
} ); | |
form.render(); | |
} ); | |
</script> | |
<?php | |
return ob_get_clean(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment