Changeset 2097633
- Timestamp:
- 05/30/2019 03:47:55 AM (7 years ago)
- Location:
- color-your-bar/trunk
- Files:
-
- 2 edited
-
color-your-bar.php (modified) (7 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
color-your-bar/trunk/color-your-bar.php
r2039374 r2097633 6 6 * Author: Darshan Saroya 7 7 * Author URI: http://darshansaroya.com 8 * Version: 1. 018 * Version: 1.2 9 9 * Text Domain: color-your-bar 10 10 * Domain Path: languages … … 24 24 * @package Color Your Bar 25 25 * @author Darshan Saroya 26 * @version 1. 026 * @version 1.2 27 27 */ 28 29 register_activation_hook( __FILE__, 'cyb_active_func' ); 30 31 function cyb_active_func(){ 32 add_option( 'cyb-switch', 0 ); 33 add_option( 'cyb-post-type', array('post', 'page') ); 34 } 28 35 29 36 add_action( 'admin_enqueue_scripts', 'cyb_assets' ); … … 36 43 37 44 function cyb_add_head_tag(){ 45 global $post; 46 $cyb_post = get_post_type($post->ID); 47 $cyb_post_type = get_option( 'cyb-post-type'); 38 48 $enable_cyb= get_option('cyb-switch'); 39 $cyb_color= get_option('cyb-color'); 49 $cyb_color= get_option('cyb-color'); 50 if(null !== $cyb_post_type && is_array($cyb_post_type)){ 51 foreach ($cyb_post_type as $post_type ) { 52 if($cyb_post == $post_type){ 53 $color = get_post_meta( $post->ID, 'cyb-color', true ); 54 if(null !== $color){ 55 $cyb_color = $color; 56 } 57 } 58 } 59 } 40 60 if($enable_cyb!='0'){ ?> 41 61 <meta name="theme-color" content="<?php echo esc_html( $cyb_color ); ?>"> … … 102 122 register_setting("cyb_setting_section", 'cyb-switch', $cyb_switch_args); 103 123 124 add_settings_field('cyb-post-type', esc_html__( 'Show Meta Box on Post Type', 'color-your-bar' ), "cyb_post_type_element", "color-your-bar", "cyb_setting_section"); 125 126 $cyb_post_type_args = array( 127 'type' => 'array', 128 'sanitize_callback' => 'cyb_sanitize_array', 129 'default' => 0, 130 ); 131 register_setting("cyb_setting_section", 'cyb-post-type', $cyb_post_type_args); 132 104 133 add_settings_field('cyb-color', esc_html__( 'Choose Color', 'color-your-bar' ), "cyb_color_field_element", "color-your-bar", "cyb_setting_section"); 105 134 … … 123 152 } 124 153 154 function cyb_post_type_callback(){ 155 esc_html_e( 'Settings to change Google Chrome(Mobile) address bar color', 'color-your-bar' ); 156 } 157 125 158 function cyb_color_switch_element() 126 159 { … … 132 165 } 133 166 167 function cyb_post_type_element() 168 { 169 $val= get_post_types ( array('public' => true,)); 170 //id and name of form element should be same as the setting name. 171 $post_selected = get_option('cyb-post-type'); 172 foreach ($val as $post_type) { 173 ?> 174 <input type="checkbox" name='cyb-post-type[]' value="<?php echo esc_attr($post_type); ?>" <?php 175 if(is_array($post_selected)){ 176 foreach($post_selected as $type){ 177 if($type == $post_type){ 178 echo esc_attr( 'checked' ); 179 } 180 } 181 } ?>> <?php echo esc_html($post_type); ?><br> 182 <?php } 183 } 184 134 185 function cyb_sanitize_checkbox($input){ 135 186 if ( $input != 1 ) { … … 140 191 } 141 192 193 function cyb_sanitize_array($input){ 194 if ( !is_array( $input ) ) { 195 return ''; 196 } else { 197 return $input; 198 } 199 } 200 142 201 add_action("admin_init", "cyb_setting_display"); 202 203 function cyb_add_color_metaboxes() { 204 $cyb_post_type = get_option( 'cyb-post-type' ); 205 foreach ($cyb_post_type as $post_type ) { 206 add_meta_box( 207 'cyb_color_meta', 208 __('Chrome Bar Color', 'color-your-bar'), 209 'cyb_color_meta', 210 $post_type, 211 'side', 212 'default' 213 ); 214 } 215 } 216 217 if( get_option( 'cyb-switch') != 0){ 218 add_action( 'add_meta_boxes', 'cyb_add_color_metaboxes' ); 219 } 220 221 function cyb_color_meta() { 222 global $post; 223 // Nonce field to validate form request came from current site 224 wp_nonce_field( basename( __FILE__ ), 'cyb-nonce' ); 225 // Get the cyb_color data if it's already been entered 226 $cyb_color = get_option('cyb-color'); 227 if( null !== get_post_meta( $post->ID, 'cyb-color', true ) && get_post_meta( $post->ID, 'cyb-color', true )!=''){ 228 $cyb_color = get_post_meta( $post->ID, 'cyb-color', true ); 229 } 230 // Output the field ?> 231 <input type="text" name='cyb-color' id='cyb-color' value="<?php echo esc_html($cyb_color); ?>" /> 232 <?php } 233 234 /** 235 * Save the metabox data 236 */ 237 function cyb_save_meta( $post_id, $post ) { 238 // Return if the user doesn't have edit permissions. 239 if ( ! current_user_can( 'edit_post', $post_id ) ) { 240 return $post_id; 241 } 242 // Verify this came from the our screen and with proper authorization, 243 // because save_post can be triggered at other times. 244 if ( ! isset( $_POST['cyb-color'] ) || ! wp_verify_nonce( $_POST['cyb-nonce'], basename(__FILE__) ) ) { 245 return $post_id; 246 } 247 // Now that we're authenticated, time to save the data. 248 // This sanitizes the data from the field and saves it into an array $cyb_meta. 249 $cyb_meta['cyb-color'] = esc_textarea( $_POST['cyb-color'] ); 250 // Cycle through the $cyb_meta array. 251 // Note, in this example we just have one item, but this is helpful if you have multiple. 252 foreach ( $cyb_meta as $key => $value ) : 253 // Don't store custom data twice 254 if ( 'revision' === $post->post_type ) { 255 return; 256 } 257 if ( get_post_meta( $post_id, $key, false ) ) { 258 // If the custom field already has a value, update it. 259 update_post_meta( $post_id, $key, $value ); 260 } else { 261 // If the custom field doesn't have a value, add it. 262 add_post_meta( $post_id, $key, $value); 263 } 264 if ( ! $value ) { 265 // Delete the meta key if there's no value 266 delete_post_meta( $post_id, $key ); 267 } 268 endforeach; 269 } 270 add_action( 'save_post', 'cyb_save_meta', 1, 2 ); 271 143 272 ?> -
color-your-bar/trunk/readme.txt
r2039374 r2097633 4 4 Donate link: http://www.paypal.me/darshansaroya 5 5 Requires at least: 3.5 6 Tested up to: 5. 16 Tested up to: 5.2.1 7 7 License: GPL2 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 34 34 35 35 == Changelog == 36 = 1. 0=37 Initial Release.36 = 1.2 = 37 Meta Box Added: Now you can color every single page/post chrome bar. 38 38 39 39 = 1.01 = 40 40 Readme Updated 41 41 42 = 1.0 = 43 Initial Release. 44 42 45 == Upgrade Notice == 43 46 1.0 Initial Release. 44 47 1.01 Readme Updated 48 1.2 Meta Box Added
Note: See TracChangeset
for help on using the changeset viewer.