Advertisement
firoze

custom css add in wordpress

Feb 16th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. // this is for only custom css for wordpress // call this code into your functions.php
  2.  
  3. <?php
  4. /* custom css for wordpress*/
  5.  
  6. if (!defined('ABSPATH')) die ('No direct access allowed');
  7.  
  8. if(!class_exists('Wpacc'))
  9. {
  10. class Wpacc
  11. {
  12. private $options;
  13.  
  14. public function __construct() {
  15. add_action('admin_menu', array($this, 'add_menu'));
  16. add_action( 'admin_init', array( $this, 'init_settings' ) );
  17. add_action( 'add_meta_boxes', array($this, 'add_meta_box' ) );
  18. add_action( 'save_post', array( $this, 'single_save' ) );
  19. add_action('init', array($this, 'init'));
  20. add_filter('query_vars', array($this, 'add_wp_var'));
  21. add_action( 'wp_enqueue_scripts', array($this, 'add_custom_css'), 999 );
  22. add_action('wp_head', array($this, 'single_custom_css'));
  23. }
  24.  
  25. public function init() {
  26. load_plugin_textdomain( 'wp-add-custom-css', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
  27. }
  28.  
  29. public static function uninstall() {
  30. self::delete_options();
  31. self::delete_custom_meta();
  32. }
  33.  
  34. public function add_meta_box( $post_type ) {
  35. $post_types = array('post', 'page');
  36. if ( in_array( $post_type, $post_types )) {
  37. add_meta_box('wp_add_custom_css', __( 'Custom CSS', 'wp-add-custom-css' ), array( $this, 'render_meta_box_content' ), $post_type, 'advanced', 'high');
  38. }
  39. }
  40.  
  41. public function single_save( $post_id ) {
  42. if ( ! isset( $_POST['wp_add_custom_css_box_nonce'] ) || ! wp_verify_nonce( $_POST['wp_add_custom_css_box_nonce'], 'single_add_custom_css_box' ) ) {
  43. return;
  44. }
  45. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  46. return;
  47. }
  48. if ( 'page' == $_POST['post_type'] ) {
  49. if ( ! current_user_can( 'edit_page', $post_id ) )
  50. return;
  51. } else {
  52. if ( ! current_user_can( 'edit_post', $post_id ) )
  53. return;
  54. }
  55.  
  56. $single_custom_css = sanitize_text_field( $_POST['single_custom_css'] );
  57. update_post_meta( $post_id, '_single_add_custom_css', $single_custom_css );
  58. }
  59.  
  60. public function render_meta_box_content( $post ) {
  61. wp_nonce_field( 'single_add_custom_css_box', 'wp_add_custom_css_box_nonce' );
  62. $single_custom_css = get_post_meta( $post->ID, '_single_add_custom_css', true );
  63. echo '<p>'. __( 'Add custom CSS rules for this ' . $post->post_type, 'wp-add-custom-css' ) . '</p> ';
  64. echo '<textarea id="single_custom_css" name="single_custom_css" style="min-height:200px;min-width:700px">' . esc_attr( $single_custom_css ) . '</textarea>';
  65. }
  66.  
  67. public function add_menu() {
  68. global $wpacc_settings_page;
  69. $wpacc_settings_page = add_menu_page(' Add Custom CSS', 'Add Custom CSS', 'manage_options', 'wp-add-custom-css_settings', array($this, 'create_settings_page'), plugin_dir_url( __FILE__ ) . '/icon.png');
  70. }
  71.  
  72. public function create_settings_page() {
  73. $this->options = get_option( 'wpacc_settings' );
  74. ?>
  75. <div class="wrap">
  76. <h2><?php echo __(' Add Custom CSS', 'wp-add-custom-css'); ?></h2>
  77. <form id="worpress_custom_css_form" method="post" action="options.php">
  78. <?php settings_fields( 'wpacc_group' ); ?>
  79. <?php do_settings_sections( 'wp-add-custom-css_settings' ); ?>
  80. <?php submit_button( __('Save', 'wp-add-custom-css') ); ?>
  81. </form>
  82. <h3><?php echo __(''); ?></h3>
  83. <ul>
  84. <li>
  85. Add Your Custom Css And Save
  86. </li>
  87. </ul>
  88. </div>
  89. <?php
  90. }
  91.  
  92. public function print_section_info() {
  93. echo __('Write here the CSS rules you want to apply to the whole website.', 'wp-add-custom-css');
  94. }
  95.  
  96. public function main_css_input() {
  97. printf(
  98. '<textarea name="wpacc_settings[main_custom_style]" style="min-height:300px;min-width:700px;">%s</textarea>',
  99. isset( $this->options['main_custom_style'] ) ? esc_attr( $this->options['main_custom_style'] ) : ''
  100. );
  101. }
  102.  
  103. public function init_settings() {
  104. register_setting(
  105. 'wpacc_group',
  106. 'wpacc_settings'
  107. );
  108. add_settings_section(
  109. 'wpacc_main_style',
  110. 'Main CSS',
  111. array( $this, 'print_section_info' ),
  112. 'wp-add-custom-css_settings'
  113. );
  114. add_settings_field(
  115. 'main_custom_style',
  116. 'CSS rules',
  117. array( $this, 'main_css_input' ),
  118. 'wp-add-custom-css_settings',
  119. 'wpacc_main_style'
  120. );
  121. }
  122.  
  123. public function delete_options() {
  124. unregister_setting(
  125. 'wpacc_group',
  126. 'wpacc_settings'
  127. );
  128. delete_option('wpacc_settings');
  129. }
  130.  
  131. public function delete_custom_meta() {
  132. delete_post_meta_by_key('_single_add_custom_css');
  133. }
  134.  
  135. public static function add_wp_var($public_query_vars) {
  136. $public_query_vars[] = 'display_custom_css';
  137. return $public_query_vars;
  138. }
  139.  
  140. public static function display_custom_css(){
  141. $display_css = get_query_var('display_custom_css');
  142. if ($display_css == 'css'){
  143. include_once (plugin_dir_path( __FILE__ ) . '/custom-css.php');
  144. exit;
  145. }
  146. }
  147.  
  148. public function add_custom_css() {
  149. $this->options = get_option( 'wpacc_settings' );
  150. if ( isset($this->options['main_custom_style']) && $this->options['main_custom_style'] != '') {
  151. wp_register_style( 'wp-add-custom-css', get_bloginfo('url') . '?display_custom_css=css' );
  152. wp_enqueue_style( 'wp-add-custom-css' );
  153. }
  154. }
  155.  
  156. public function single_custom_css() {
  157. if ( is_single() || is_page() ) {
  158. global $post;
  159. $single_custom_css = get_post_meta( $post->ID, '_single_add_custom_css', true );
  160. if ( $single_custom_css !== '' ) {
  161. $output = "<style type=\"text/css\">" . $single_custom_css . "</style>\n";
  162. }
  163. echo $output;
  164. }
  165. }
  166.  
  167.  
  168. }
  169. }
  170.  
  171. if(class_exists('Wpacc')) {
  172. add_action('template_redirect', array('Wpacc', 'display_custom_css'));
  173. register_uninstall_hook(__FILE__, array('Wpacc', 'uninstall'));
  174. $wpacc = new Wpacc();
  175. }
  176.  
  177. if(isset($wpacc)) {
  178. function wpacc_settings_link($links) {
  179. $settings_link = '<a href="admin.php?page=wp-add-custom-css_settings">' . __('Settings', 'wp-add-custom-css') . '</a>';
  180. array_unshift($links, $settings_link);
  181. return $links;
  182. }
  183. add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'wpacc_settings_link');
  184. }
  185.  
  186. ?>
  187.  
  188.  
  189. <?php
  190. header("Content-type: text/css");
  191. $custom_css = get_option('wpacc_settings');
  192. $custom_css = wp_kses( $custom_css['main_custom_style'], array( '\'', '\"' ) );
  193. echo $custom_css;
  194. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement