Skip to content

Instantly share code, notes, and snippets.

@dgwyer
Created September 20, 2017 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgwyer/0bb2022be0d733cf3bfc4e094ea815f7 to your computer and use it in GitHub Desktop.
Save dgwyer/0bb2022be0d733cf3bfc4e094ea815f7 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Inline JS Test
Version: 0.1
Author: David Gwyer
*/
// ijst_ prefix is derived from [i]nline [js] [t]est
// Enqueue scripts
function ijst_enqueue_scripts() {
$options = get_option( 'ijst_options' );
$js = get_post_meta( '5943', '_ijst-js', true );
wp_enqueue_script( 'ijst-test', plugins_url('test.js', __FILE__), array(), '', true );
$inline_js1 = $options['textarea'];
$inline_js2 = $js;
wp_add_inline_script( 'ijst-test', $inline_js1 );
wp_add_inline_script( 'ijst-test', $inline_js2 );
}
add_action( 'wp_enqueue_scripts', 'ijst_enqueue_scripts' );
// Plugin options page
function ijst_init() {
register_setting( 'ijst_plugin_options', 'ijst_options' );
}
add_action( 'admin_init', 'ijst_init' );
function ijst_add_options_page() {
$page = add_options_page( 'Inline JS Test', 'Inline JS Test', 'manage_options', __FILE__, 'ijst_render_form' );
}
add_action( 'admin_menu', 'ijst_add_options_page' );
function ijst_render_form() {
?>
<div class="wrap">
<h2 style="font-size: 23px;">Inline JS Test</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'ijst_plugin_options' );
$options = get_option( 'ijst_options' );
?>
<table>
<tr>
<td><textarea name="ijst_options[textarea]" rows="7" cols="50" type='textarea'><?php echo $options['textarea']; ?></textarea></td>
</tr>
</table>
<p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ) ?>"></p>
</form>
</div>
<?php
}
// Post meta box
function ijst_meta_box_init() {
add_meta_box( 'inline-js-test', 'Inline JS Test', 'ijst_render_meta_box', 'post', 'normal', 'high' );
add_action( 'save_post', 'ijst_save_meta_box_data' );
}
add_action( 'admin_init', 'ijst_meta_box_init' );
function ijst_save_meta_box_data( $post_id ) {
if ( isset( $_POST['ijst-js'] ) ) { update_post_meta( $post_id, '_ijst-js', esc_attr( $_POST['ijst-js'] ) ); }
}
function ijst_render_meta_box( $post, $args ) {
$js = get_post_meta( $post->ID, '_ijst-js', true );
?><table><tr><td><textarea id="ijst-js" name="ijst-js"><?php echo esc_attr( $js ); ?></textarea></td></tr></table><?php
}
console.log("test.js loaded");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment