-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-js-meta-box.php
165 lines (125 loc) · 4.6 KB
/
custom-js-meta-box.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
Plugin Name: Custom JavaScript Meta Box
Plugin URI:
Description: Abiliy to place custom JavaScript on individual pages and posts. Once enabled a custom text box will apear on page and post write panels and the custom JavaScript will be written at the bottom of the HTML document.
Author: CTLT Dev
Version: 1.0.3
Author URI:
*/
Class Custom_JavaScript_Meta_Box {
static $instance;
private static $plugin_ver = '1.0.4';
function __construct() {
self::$instance = $this;
add_action( 'init', array( $this, 'init' ) );
}
/**
* init function.
*
* @access public
* @return void
*/
function init(){
// filters
add_action( 'wp_footer',array( $this,'display_js'), 999 );
// admin side
/* Use the admin_menu action to define the custom boxes */
add_action( 'admin_menu', array( $this, 'init_meta_box' ) );
/* Use the save_post action to do something with the data entered */
add_action('save_post', array( $this, 'save_meta_data' ) );
add_action( 'admin_print_styles-post-new.php', array( $this,'script_and_style') );
add_action( 'admin_print_styles-post.php',array( $this,'script_and_style') );
}
/**
* display_js function.
*
* @access public
* @return void
*/
function display_js() {
global $post;
if( is_single() || is_page() ):
$custom_field = trim( get_post_meta( $post->ID, '_custom_js' , true ) );
if( !empty( $custom_field ) ):
echo '<!-- JavaScript FROM META BOX -->
<script type="text/javascript">
//<![CDATA[
';
echo $custom_field;
echo '
//]]>
</script>
';
endif;
endif;
}
// ADMIN SIDE
/* Adds a custom section to the "advanced" Post and Page edit screens */
function script_and_style(){
global $post;
if( !in_array($post->post_type, array('post','page') ) )
return;
// add javascript
wp_enqueue_script( 'codemirror', plugins_url( 'custom-js-meta-box/js/codemirror.js' ), array( 'jquery' ), self::$plugin_ver, true );
wp_enqueue_script( 'codemirror-script-js', plugins_url( 'custom-js-meta-box/js/javascript.js' ), array( 'codemirror' ), self::$plugin_ver, true );
// add just the styles
wp_enqueue_style( 'js-metabox-codemirror-style', plugins_url( 'custom-js-meta-box/css/codemirror.css' ), self::$plugin_ver, true );
}
/**
* init_meta_box function.
*
* @access public
* @return void
*/
function init_meta_box() {
// on posts
add_meta_box( 'custom_js_meta_box', __( 'Custom JavaScript', 'custom-js-meta-box' ), array( $this, 'display_meta_box' ), 'post', 'advanced','low' );
// on pages
add_meta_box( 'custom_js_meta_box', __( 'Custom JavaScript', 'custom-js-meta-box' ), array( $this, 'display_meta_box' ), 'page', 'advanced','low' );
}
/**
* meta_box_display function.
*
* @access public
* @return void
*/
function display_meta_box( $post ) {
$post_id = $post->ID;
if ( 'attachment' === $post->post_type ) {
// Other plugins, such as the portoflio slideshow plugin override the global $post, which causes problems
$post_id = absint( $_GET['post'] );
$post = get_post( $post_id );
}
$custom_js = get_post_meta( $post_id, '_custom_js', true );
// Use nonce for verification
echo '<input type="hidden" name="custom_js_mate_box_noncename" id="custom_js_mate_box_noncename" value="' .wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
echo __( 'The JavaScript will appear only on this ','custom-js-meta-box' ) . $post->post_type . __( ' and will be included at the END of the HTML','custom-js-meta-box' );
// The actual fields for data entry
?>
<pre><code> <script type="text/javascript"></code></pre>
<textarea name="custom_js_meta_box" id="custom-js-meta-box"><?php echo esc_textarea( $custom_js ); ?></textarea>
<pre><code> </script></code></pre>
<?php
}
/**
* save_meta_data function.
*
* @access public
* @param mixed $post_id
* @return void
*/
function save_meta_data( $post_id ) {
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if (isset($_POST['custom_js_mate_box_noncename']) && !wp_verify_nonce( $_POST['custom_js_mate_box_noncename'], plugin_basename(__FILE__) ))
return $post_id;
// only update the data if it is a string
if(isset($_POST['custom_js_meta_box']) && is_string( $_POST['custom_js_meta_box'] ) )
add_post_meta( $post_id, '_custom_js', $_POST['custom_js_meta_box'], true) or update_post_meta( $post_id, '_custom_js', $_POST['custom_js_meta_box'] );
return $post_id;
}
}
new Custom_JavaScript_Meta_Box;