Hide Title - Version 1.0.2

Version Description

  • Added logic to flag when wp_head has run to prevent changes being made to the title in the >head< area.
  • Fixed a bug that caused multiple meta field entries.
Download this release

Release Info

Developer kraftbj
Plugin Icon wp plugin Hide Title
Version 1.0.2
Comparing to
See all releases

Version 1.0.2

Files changed (3) hide show
  1. dojo-digital-hide-title.php +189 -0
  2. readme.txt +76 -0
  3. uninstall.php +5 -0
dojo-digital-hide-title.php ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Hide Title
4
+ Plugin URI: http://dojodigital.com
5
+ Description: Allows authors to hide the title tag on single pages and posts via the edit post screen.
6
+ Version: 1.0.2
7
+ Author: Randall Runnels
8
+ Author URI: http://dojodigital.com
9
+ */
10
+
11
+ if ( !class_exists( 'DojoDigitalHideTitle' ) ) {
12
+
13
+ class DojoDigitalHideTitle {
14
+
15
+ private $slug = 'dojodigital_toggle_title';
16
+ private $selector = '.entry-title';
17
+ private $title;
18
+ private $afterHead = false;
19
+
20
+ /**
21
+ * PHP 4 Compatible Constructor
22
+ */
23
+ function DojoDigitalHideTitle(){ $this->__construct(); }
24
+
25
+ /**
26
+ * PHP 5 Constructor
27
+ */
28
+ function __construct(){
29
+
30
+ add_action( 'add_meta_boxes', array( $this, 'add_box' ) );
31
+ add_action( 'save_post', array( $this, 'on_save' ) );
32
+ add_action( 'delete_post', array( $this, 'on_delete' ) );
33
+ add_action( 'wp_head', array( $this, 'head_insert' ), 3000 );
34
+ add_action( 'the_title', array( $this, 'wrap_title' ) );
35
+ add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
36
+
37
+ } // __construct()
38
+
39
+
40
+ private function is_hidden( ){
41
+
42
+ if( is_singular() ){
43
+
44
+ global $post;
45
+
46
+ $toggle = get_post_meta( $post->ID, $this->slug, true );
47
+
48
+ if( (bool) $toggle ){
49
+ return true;
50
+ } else {
51
+ return false;
52
+ }
53
+
54
+ } else {
55
+ return false;
56
+ }
57
+
58
+ } // is_hidden()
59
+
60
+
61
+ public function head_insert(){
62
+
63
+ if( $this->is_hidden() ){ ?>
64
+ <!-- Dojo Digital Hide Title -->
65
+ <script type="text/javascript">
66
+ jQuery(document).ready(function($){
67
+
68
+ if( $('<?php echo $this->selector; ?>') ) {
69
+ $('<?php echo $this->selector; ?> span.<?php echo $this->slug; ?>').parents('<?php echo $this->selector; ?>:first').hide();
70
+ } else {
71
+ $('h1 span.<?php echo $this->slug; ?>').parents('h1:first').hide();
72
+ $('h2 span.<?php echo $this->slug; ?>').parents('h2:first').hide();
73
+ }
74
+
75
+ });
76
+ </script>
77
+ <noscript><style type="text/css"> <?php echo $this->selector; ?> { display:none !important; }</style></noscript>
78
+ <!-- END Dojo Digital Hide Title -->
79
+
80
+ <?php }
81
+
82
+ // Indicate that the header has run so we can hopefully prevent adding span tags to the meta attributes, etc.
83
+ $this->afterHead = true;
84
+
85
+ } // head_insert()
86
+
87
+
88
+ public function add_box(){
89
+
90
+ $posttypes = array( 'post', 'page' );
91
+
92
+ foreach ( $posttypes as $posttype ){
93
+ add_meta_box( $this->slug, 'Hide Title', array( $this, 'build_box' ), $posttype, 'side' );
94
+ }
95
+
96
+ } // add_box()
97
+
98
+
99
+ public function build_box( $post ){
100
+
101
+ $value = get_post_meta( $post->ID, $this->slug, true );
102
+
103
+ $checked = '';
104
+
105
+ if( (bool) $value ){ $checked = ' checked="checked"'; }
106
+
107
+ wp_nonce_field( $this->slug . '_dononce', $this->slug . '_noncename' );
108
+
109
+ ?>
110
+ <label><input type="checkbox" name="<?php echo $this->slug; ?>" <?php echo $checked; ?> /> Hide the title on singular page views.</label>
111
+ <?php
112
+
113
+ } // build_box()
114
+
115
+
116
+ public function wrap_title( $content ){
117
+
118
+ if( $this->is_hidden() && $content == $this->title && $this->afterHead ){
119
+ $content = '<span class="' . $this->slug . '">' . $content . '</span>';
120
+ }
121
+
122
+ return $content;
123
+
124
+ } // wrap_title()
125
+
126
+
127
+ public function load_scripts(){
128
+
129
+
130
+ // Grab the title early in case it's overridden later by extra loops.
131
+ global $post;
132
+ $this->title = $post->post_title;
133
+
134
+ if( $this->is_hidden() ){
135
+ wp_enqueue_script( 'jquery' );
136
+
137
+ }
138
+
139
+ } // load_scripts()
140
+
141
+
142
+ public function on_save( $postID ){
143
+
144
+ if ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
145
+ || !isset( $_POST[ $this->slug . '_noncename' ] )
146
+ || !wp_verify_nonce( $_POST[ $this->slug . '_noncename' ], $this->slug . '_dononce' ) ) {
147
+ return $postID;
148
+ }
149
+
150
+ $old = get_post_meta( $postID, $this->slug, true );
151
+ $new = $_POST[ $this->slug ] ;
152
+
153
+ if( $old ){
154
+ if ( is_null( $new ) ){
155
+ delete_post_meta( $postID, $this->slug );
156
+ } else {
157
+ update_post_meta( $postID, $this->slug, $new, $old );
158
+ }
159
+ } elseif ( !is_null( $new ) ){
160
+ add_post_meta( $postID, $this->slug, $new, true );
161
+ }
162
+
163
+ return $postID;
164
+
165
+ } // on_save()
166
+
167
+
168
+ public function on_delete( $postID ){
169
+ delete_post_meta( $postID, $this->slug );
170
+ return $postID;
171
+ } // on_delete()
172
+
173
+
174
+ public function set_selector( $selector ){
175
+
176
+ if( isset( $selector ) && is_string( $selector ) ){
177
+ $this->selector = $selector;
178
+ }
179
+
180
+ } // set_selector()
181
+
182
+
183
+ } // DojoDigitalHideTitle
184
+
185
+ $DojoDigitalHideTitle = new DojoDigitalHideTitle();
186
+
187
+ } // !class_exists( 'DojoDigitalHideTitle' )
188
+
189
+ ?>
readme.txt ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Hide Title ===
2
+
3
+ Contributors: dojodigital
4
+ Plugin Name: Hide Title
5
+ Plugin URI: http://hidetitle.dojodigital.com/
6
+ Tags: wp, title
7
+ Author URI: http://dojodigital.com/
8
+ Author: Dojo Digital
9
+ Requires at least: 3.0
10
+ Tested up to: 3.4.2
11
+ Stable tag: 1.0.2
12
+ Version: 1.0.2
13
+
14
+ Allows authors to hide the title on single pages and posts via the edit post screen.
15
+
16
+ == Description ==
17
+
18
+ This plugin allows the author of a post or page to hide the title and it's containing HTML element from the single view ( is_singular() ).
19
+
20
+ == Installation ==
21
+
22
+ 1. Upload the `hide-title` folder to the `/wp-content/plugins/` directory
23
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
24
+
25
+ == Screenshots ==
26
+
27
+ 1. This Meta Box will be added to the Edit screen for pages & posts
28
+
29
+ == Changelog ==
30
+
31
+ = 1.0.2 =
32
+
33
+ * Added logic to flag when wp_head has run to prevent changes being made to the title in the &gt;head&lt; area.
34
+ * Fixed a bug that caused multiple meta field entries.
35
+
36
+ = 1.0.1 =
37
+
38
+ * Changed the jQuery to use a less brute force method of hiding the title.
39
+ * Added a set_selector() method to allow end-users to specify the css selector to hide.
40
+
41
+ == Upgrade Notice ==
42
+
43
+ = 1.0.2 =
44
+
45
+ * This version flags when wp_head has run to prevent changes being made to the title in the <head> area and fixed a glitch reported by several users that caused multiple meta entries to be created.
46
+
47
+ = 1.0.1 =
48
+
49
+ * This version uses a less brute force method of hiding the title by trying to find and hide `.entry-title` before looking for the title inside of `h1` or `h2` tags and hiding them. This version also adds a method for theme editors to change the selector from the default `.entry-title` to whatever they want to use.
50
+
51
+ == Frequently Asked Questions ==
52
+
53
+
54
+ = I upgraded to 1.0.2 and the plugin stopped working. Why? =
55
+
56
+ It is possible that your theme does not have the wp_head function in it's header.php file. In general all themes are suppose to have it, and version 1.0.2 looks for it to prevent adding bad code to the `<head>` area of the page. If you have access to your theme file simply add `<?php wp_head(); ?>` to header.php just before the `</head>` tag. If not, this plugin will no longer be compatible with your theme.
57
+
58
+ = Hey! This plugin is hiding things I don't want hidden! =
59
+
60
+ By default this plugin looks for the `.entry-title` class and hides it. If it doesn't find it it will look for any `h1` or `h2` elements that contain the title and hide them instead. To change the default `.entry-title` selector to something that makes more sense to you, add the following code to the functions.php file of your current theme:
61
+
62
+ `global $DojoDigitalHideTitle;
63
+ // Be sure to replace ".your-selector" with your selector!
64
+ $DojoDigitalHideTitle->set_selector('.your-selector');`
65
+
66
+ As noted in the comments, you'll need to replace the string `.your-selector` with the css selector you'd like hidden. It can be any valid css selector such as `h1`, `.myclass`, `#myid`, etc. I recommend using a class or id to avoid accidentally hiding unforeseen elements.
67
+
68
+ = I don't want to edit my theme files, can't you just add an option page? =
69
+
70
+ I could, but I'd like to avoid adding Yet Another Options Page if I can. If enough people request it though, I'll go ahead and bite the bullet.
71
+
72
+
73
+
74
+
75
+
76
+
uninstall.php ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+ <?php
2
+
3
+ global $wpdb;
4
+
5
+ $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->postmeta . ' WHERE meta_key="dojodigital_toggle_title"' ) );