What Are We Actually Going to Create
In this tutorial we are going to create a front-end text size changer that alters the font size of the posts as per the reader’s convenience. Suppose you are on a blog reading a post and you find it tough traversing through the lines of the post because of the text size. Here comes the need of the text size changer to increase/decrease the text size of the post accordingly. Increasing/Decreasing font size is generally considered due to a number of factors such as:- To adjust the text as per your screen resolution
- To alter the default text size shown by the browser
- To increase clarity of words
- More convenient than the use of Ctrl+ or Ctrl- in your browser
- For visually impaired persons who are unable to read smaller fonts
Step 1: Decide Which Part to Resize
This is the most important step in which you have to decide which element you want to associate with the text size changer. Though you can put the text size changer in the home page (index) itself, it is considered wise to put it in the single.php page to be utilized while viewing a single post. In order to make an element resizable we have to wrap that part using a unique div class. Since we are using the Twenty Twelve theme, here is what our ‘single.php‘ file looks like after adding the code highlighted below to wrap the post within the ‘resize
‘ class.
[php]<code><?php</code></pre>
<div><code>/**</code></div>
<div><code> </code><code>* The Template for displaying all single posts.</code></div>
<div><code> </code><code>*</code></div>
<div><code> </code><code>* @package WordPress</code></div>
<div><code> </code><code>* @subpackage Twenty_Twelve</code></div>
<div><code> </code><code>* @since Twenty Twelve 1.0</code></div>
<div><code> </code><code>*/</code></div>
<div></div>
<div><code>get_header(); ?></code></div>
<div><code> </code><code><div id=</code><code>"primary"</code> <code>class</code><code>=</code><code>"site-content"</code><code>></code></div>
<div><code> </code><code><div id=</code><code>"content"</code> <code>role=</code><code>"main"</code><code>></code></div>
<div><code> </code><code><div </code><code>class</code><code>=</code><code>"resize"</code><code>></code></div>
<div></div>
<div><code> </code><code><?php </code><code>while</code> <code>( have_posts() ) : the_post(); ?></code></div>
<div></div>
<div><code> </code><code><?php get_template_part( </code><code>'content'</code><code>, get_post_format() ); ?></code></div>
<div><code> </code></div>
<div><code> </code><code></div><!-- #resize --></code></div>
<div></div>
<div><code> </code><code><nav </code><code>class</code><code>=</code><code>"nav-single"</code><code>></code></div>
<div><code> </code><code><h3 </code><code>class</code><code>=</code><code>"assistive-text"</code><code>><?php _e( </code><code>'Post navigation'</code><code>, </code><code>'twentytwelve'</code> <code>); ?></h3></code></div>
<div></div>
<div><code> </code><code><span </code><code>class</code><code>=</code><code>"nav-previous"</code><code>><?php previous_post_link( </code><code>'%link'</code><code>, </code><code>'<span>'</code> <code>. _x( </code><code>'←'</code><code>, </code><code>'Previous post link'</code><code>, </code><code>'twentytwelve'</code> <code>) . </code><code>'</span> %title'</code> <code>); ?></span></code></div>
<div></div>
<div><code> </code><code><span </code><code>class</code><code>=</code><code>"nav-next"</code><code>><?php next_post_link( </code><code>'%link'</code><code>, </code><code>'%title <span>'</code> <code>. _x( </code><code>'→'</code><code>, </code><code>'Next post link'</code><code>, </code><code>'twentytwelve'</code> <code>) . </code><code>'</span>'</code> <code>); ?></span></code></div>
<div></div>
<div><code> </code><code></nav><!-- .nav-single --></code></div>
<div></div>
<div><code> </code><code><?php</code></div>
<div><code> </code><code>// If comments are open or we have at least one comment, load up the comment template</code></div>
<div><code> </code><code>if</code> <code>( comments_open() || </code><code>'0'</code> <code>!= get_comments_number() )</code></div>
<div><code> </code><code>comments_template( </code><code>''</code><code>, true );</code></div>
<div><code> </code><code>?></code></div>
<div></div>
<div><code> </code><code><?php </code><code>endwhile</code><code>; </code><code>// end of the loop. ?></code></div>
<div></div>
<div><code> </code><code></div><!-- #content --></code></div>
<div><code> </code><code></div><!-- #primary --></code></div>
<div></div>
<div><code><?php get_sidebar(); ?></code></div>
<div></div>
<div><code><?php get_footer(); ?></code></div>
<pre>
[/php]
Step 2: Adding the Links to Resize Text
The next step is to add the two links in the page which will act as two buttons to resize the text. You can place them anywhere within your page but avoid placing them within the loop. Here we have placed them in our single.php file and have associated them with two unique IDs. [php]<code><?php</code></pre> <div><code>/**</code></div> <div><code> </code><code>* The Template for displaying all single posts.</code></div> <div><code> </code><code>*</code></div> <div><code> </code><code>* @package WordPress</code></div> <div><code> </code><code>* @subpackage Twenty_Twelve</code></div> <div><code> </code><code>* @since Twenty Twelve 1.0</code></div> <div><code> </code><code>*/</code></div> <div></div> <div><code>get_header(); ?></code></div> <div><code> </code><code><div id=</code><code>"primary"</code> <code>class</code><code>=</code><code>"site-content"</code><code>></code></div> <div><code> </code><code><div id=</code><code>"content"</code> <code>role=</code><code>"main"</code><code>></code></div> <div><code> </code><code><div </code><code>class</code><code>=</code><code>"resize"</code><code>></code></div> <div></div> <div><code> </code><code><?php </code><code>while</code> <code>( have_posts() ) : the_post(); ?></code></div> <div></div> <div><code> </code><code><?php get_template_part( </code><code>'content'</code><code>, get_post_format() ); ?></code></div> <div><code> </code></div> <div><code> </code><code></div><!-- #resize --></code></div> <div></div> <div><code> </code><code><nav </code><code>class</code><code>=</code><code>"nav-single"</code><code>></code></div> <div><code> </code><code><h3 </code><code>class</code><code>=</code><code>"assistive-text"</code><code>><?php _e( </code><code>'Post navigation'</code><code>, </code><code>'twentytwelve'</code> <code>); ?></h3></code></div> <div><code> </code><code><span </code><code>class</code><code>=</code><code>"nav-previous"</code><code>><?php previous_post_link( </code><code>'%link'</code><code>, </code><code>'<span>'</code> <code>. _x( </code><code>'←'</code><code>, </code><code>'Previous post link'</code><code>, </code><code>'twentytwelve'</code> <code>) . </code><code>'</span> %title'</code> <code>); ?></span></code></div> <div></div> <div><code> </code><code><span </code><code>class</code><code>=</code><code>"nav-next"</code><code>><?php next_post_link( </code><code>'%link'</code><code>, </code><code>'%title <span>'</code> <code>. _x( </code><code>'→'</code><code>, </code><code>'Next post link'</code><code>, </code><code>'twentytwelve'</code> <code>) . </code><code>'</span>'</code> <code>); ?></span></code></div> <div></div> <div><code> </code><code></nav><!-- .nav-single --></code></div> <div></div> <div><code> </code><code><?php</code></div> <div><code> </code><code>// If comments are open or we have at least one comment, load up the comment template</code></div> <div><code> </code><code>if</code> <code>( comments_open() || </code><code>'0'</code> <code>!= get_comments_number() )</code></div> <div><code> </code><code>comments_template( </code><code>''</code><code>, true );</code></div> <div><code> </code><code>?></code></div> <div></div> <div><code> </code><code><?php </code><code>endwhile</code><code>; </code><code>// end of the loop. ?></code></div> <div></div> <div><code> </code><code></div><!-- #content --></code></div> <div><code> </code><code></div><!-- #primary --></code></div> <div><code><p><a id=</code><code>"increase-font"</code> <code>href=</code><code>"#"</code><code>>[ A+ ] </a>/<a id=</code><code>"decrease-font"</code> <code>href=</code><code>"#"</code><code>>[ A- ] </a> </p></code></div> <div><code><?php get_sidebar(); ?></code></div> <div></div> <div><code><?php get_footer(); ?></code></div> <pre> [/php]Step 3: Adding the jQuery Magic
Now it’s time to add the jQuery magic to our theme in order to make the two increase/decrease links function. Under your theme’s js folder create a JavaScript file named resize.js. Now open the file and add the following piece of code. The code is clearly explained using the comments. [js]<code>// This prevents the execution of the code before the document is finished loading.</code></pre> <div><code>jQuery(document).ready(</code><code>function</code><code>() {</code></div> <div></div> <div><code> </code><code>// The 'A+' element in the page is associated with the jQuery click() event.</code></div> <div><code> </code><code>jQuery(</code><code>'#increase-font'</code><code>).click(</code><code>function</code><code>(event) {</code></div> <div></div> <div><code> </code><code>// This prevents the default action of the click() event from being triggered.</code></div> <div><code> </code><code>event.preventDefault();</code></div> <div><code> </code><code>// The jQuery each() event interates over each elements belonging to the 'resize' class</code></div> <div><code> </code><code>jQuery(</code><code>'.resize'</code><code>).each(</code><code>function</code><code>() {</code></div> <div><code> </code><code>// Call to a custom function to increase the text size</code></div> <div><code> </code><code>changeTextSize(</code><code>this</code><code>, change);</code></div> <div><code> </code><code>});</code></div> <div><code> </code><code>});</code></div> <div></div> <div><code> </code><code>// The 'A-' element in the page is associated with the jQuery click() event.</code></div> <div><code> </code><code>jQuery(</code><code>'#decrease-font'</code><code>).click(</code><code>function</code><code>(event) {</code></div> <div></div> <div><code> </code><code>// This prevents the default action of the click() event from being triggered.</code></div> <div><code> </code><code>event.preventDefault();</code></div> <div><code> </code><code>// The jQuery each() event interates over each elements belonging to the 'resize' class</code></div> <div><code> </code><code>jQuery(</code><code>'.resize'</code><code>).each(</code><code>function</code><code>() {</code></div> <div><code> </code><code>// Call to a custom function to decrease the text size</code></div> <div><code> </code><code>changeTextSize(</code><code>this</code><code>, -change);</code></div> <div><code> </code><code>});</code></div> <div><code> </code><code>});</code></div> <div><code>});</code></div> <div></div> <div><code>// Three variables have been used to define range of the text size and the increment/decrement value respectively.</code></div> <div><code>var</code> <code>min = 8, max = 100, change = 2;</code></div> <div></div> <div><code>// Defines a custom function with two parameters determining the element to be resized and the size</code></div> <div><code>function</code> <code>changeTextSize(element, value) {</code></div> <div><code> </code><code>var</code> <code>currentSize = parseFloat(jQuery(element).css(</code><code>'font-size'</code><code>));</code></div> <div><code> </code><code>var</code> <code>newSize = currentSize + value;</code></div> <div><code> </code><code>if</code> <code>(newSize <= max && newSize >= min) {</code></div> <div><code> </code><code>jQuery(element).css(</code><code>'font-size'</code><code>, newSize + </code><code>'px'</code><code>);</code></div> <div><code> </code><code>}</code></div> <div><code>}</code></div> <pre> [/js]Step 4: Referencing the Script in WordPress
This is the final step in which we need to add a reference to the resize.js script in WordPress so that it executes the code. By default the WordPress installation already contains the jQuery library. We just need to reference the script within the theme. Open your functions.php file and add the following code snippet. [php]<code>function</code> <code>wptuts_resize_text () {</code></pre> <div><code> </code><code>// The array('jquery') is used to create dependency on the jQuery library in order to use it properly.</code></div> <div><code> </code><code>wp_enqueue_script( </code><code>'resize'</code><code>, get_template_directory_uri() . </code><code>'/js/resize.js'</code><code>, </code><code>array</code><code>( </code><code>'jquery'</code> <code>) );</code></div> <div><code>}</code></div> <div><code>add_action( </code><code>'wp_enqueue_scripts'</code><code>, </code><code>'wptuts_resize_text'</code> <code>);</code></div> <pre>[/php] That’s it. Now if you are viewing a post, you can increment or decrement the font size of the post using theA+ and A- links in the page. You can use your own CSS to properly style and place the two links in your website as you see fit.Revisions
- March 27, 2013 @ 13:00:35 [Current Revision] by PeterLugg
- March 27, 2013 @ 13:05:41 [Autosave] by PeterLugg
- March 27, 2013 @ 12:56:48 by PeterLugg
Revision Differences
March 27, 2013 @ 13:05:41 [Autosave] | Current Revision | ||
---|---|---|---|
Content | |||
Unchanged: This article is taken from here: <a href="http:// wp.tutsplus.com/tutorials/ theme-development/creating- a-wordpress-post-text-size- changer-using-jquery/" target="_blank" >http://wp.tutsplus.com/tutorials/ theme-development/creating- a-wordpress-post-text-size- changer-using-jquery/</a> | Unchanged: This article is taken from here: <a href="http:// wp.tutsplus.com/tutorials/ theme-development/creating- a-wordpress-post-text-size- changer-using-jquery/" target="_blank" >http://wp.tutsplus.com/tutorials/ theme-development/creating- a-wordpress-post-text-size- changer-using-jquery/</a> | ||
Unchanged: <h2>What Are We Actually Going to Create</h2> | Unchanged: <h2>What Are We Actually Going to Create</h2> | ||
Unchanged: In this tutorial we are going to create a front-end text size changer that alters the font size of the posts as per the reader’s convenience. Suppose you are on a blog reading a post and you find it tough traversing through the lines of the post because of the text size. Here comes the need of the text size changer to increase/decrease the text size of the post accordingly. Increasing/Decreasing font size is generally considered due to a number of factors such as: | Unchanged: In this tutorial we are going to create a front-end text size changer that alters the font size of the posts as per the reader’s convenience. Suppose you are on a blog reading a post and you find it tough traversing through the lines of the post because of the text size. Here comes the need of the text size changer to increase/decrease the text size of the post accordingly. Increasing/Decreasing font size is generally considered due to a number of factors such as: | ||
Unchanged: <ul> | Unchanged: <ul> | ||
Unchanged: <li>To adjust the text as per your screen resolution</li> | Unchanged: <li>To adjust the text as per your screen resolution</li> | ||
Unchanged: <li>To alter the default text size shown by the browser</li> | Unchanged: <li>To alter the default text size shown by the browser</li> | ||
Unchanged: <li>To increase clarity of words</li> | Unchanged: <li>To increase clarity of words</li> | ||
Unchanged: <li>More convenient than the use of Ctrl+ or Ctrl- in your browser</li> | Unchanged: <li>More convenient than the use of Ctrl+ or Ctrl- in your browser</li> | ||
Unchanged: <li>For visually impaired persons who are unable to read smaller fonts</li> | Unchanged: <li>For visually impaired persons who are unable to read smaller fonts</li> | ||
Unchanged: </ul> | Unchanged: </ul> | ||
Unchanged: In this tutorial we will be creating two links, <strong> A+</strong> and <strong> A-</strong>, in the front-end which will increase or decrease the text size of the post accordingly with ease. | Unchanged: In this tutorial we will be creating two links, <strong> A+</strong> and <strong> A-</strong>, in the front-end which will increase or decrease the text size of the post accordingly with ease. | ||
Unchanged: <hr /> | Unchanged: <hr /> | ||
Unchanged: <h2>Step 1: Decide Which Part to Resize</h2> | Unchanged: <h2>Step 1: Decide Which Part to Resize</h2> | ||
Unchanged: This is the most important step in which you have to decide which element you want to associate with the text size changer. Though you can put the text size changer in the home page (index) itself, it is considered wise to put it in the <strong> single.php</strong> page to be utilized while viewing a single post. | Unchanged: This is the most important step in which you have to decide which element you want to associate with the text size changer. Though you can put the text size changer in the home page (index) itself, it is considered wise to put it in the <strong> single.php</strong> page to be utilized while viewing a single post. | ||
Unchanged: In order to make an element resizable we have to wrap that part using a unique div class. Since we are using the <a href="http:// wordpress.org/ extend/themes/ twentytwelve">Twenty Twelve theme</a>, here is what our ‘<strong>single.php</strong>‘ file looks like after adding the code highlighted below to wrap the post within the ‘<code>resize</code>‘ class. | Unchanged: In order to make an element resizable we have to wrap that part using a unique div class. Since we are using the <a href="http:// wordpress.org/ extend/themes/ twentytwelve">Twenty Twelve theme</a>, here is what our ‘<strong>single.php</strong>‘ file looks like after adding the code highlighted below to wrap the post within the ‘<code>resize</code>‘ class. | ||
Unchanged: [php]<code> <?php</ code></pre> | Unchanged: [php]<code> <?php</ code></pre> | ||
Unchanged: <div>< code>/**< /code></div> | Unchanged: <div>< code>/**< /code></div> | ||
Unchanged: <div>< code> < /code><code>* The Template for displaying all single posts.</code> </div> | Unchanged: <div>< code> < /code><code>* The Template for displaying all single posts.</code> </div> | ||
Unchanged: <div>< code> < /code>< code>*< /code></div> | Unchanged: <div>< code> < /code>< code>*< /code></div> | ||
Unchanged: <div>< code> < /code><code>* @package WordPress< /code></div> | Unchanged: <div>< code> < /code><code>* @package WordPress< /code></div> | ||
Unchanged: <div>< code> < /code><code>* @subpackage Twenty_Twelve< /code></div> | Unchanged: <div>< code> < /code><code>* @subpackage Twenty_Twelve< /code></div> | ||
Unchanged: <div>< code> < /code><code>* @since Twenty Twelve 1.0</code> </div> | Unchanged: <div>< code> < /code><code>* @since Twenty Twelve 1.0</code> </div> | ||
Unchanged: <div>< code> < /code>< code>*/< /code></div> | Unchanged: <div>< code> < /code>< code>*/< /code></div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code>get_header(); ?></code> </div> | Unchanged: <div>< code>get_header(); ?></code> </div> | ||
Unchanged: <div>< code> </code> <code><div id=</code> <code>" primary" </code> <code>class< /code>< code>=< /code>< code>" site-content" </code> <code>> </code> </div> | Unchanged: <div>< code> </code> <code><div id=</code> <code>" primary" </code> <code>class< /code>< code>=< /code>< code>" site-content" </code> <code>> </code> </div> | ||
Unchanged: <div>< code> < /code>< code><div id=</code> <code>" content" </code> <code>role= </code> <code> "main" </code> <code>> </code> </div> | Unchanged: <div>< code> < /code>< code><div id=</code> <code>" content" </code> <code>role= </code> <code> "main" </code> <code>> </code> </div> | ||
Unchanged: <div>< code> < /code>< code><div </code> <code>class</code> <code>= </code> <code>" resize" </code> <code> ></code> </div> | Unchanged: <div>< code> < /code>< code><div </code> <code>class</code> <code>= </code> <code>" resize" </code> <code> ></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><?php </code> <code>while</code> <code>( have_posts() ) : the_post(); ?></code> </div> | Unchanged: <div>< code> < /code>< code><?php </code> <code>while</code> <code>( have_posts() ) : the_post(); ?></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><?php get_template_part( </code> <code>'content'< /code><code>, get_post_format() ); ?></code> </div> | Unchanged: <div>< code> < /code>< code><?php get_template_part( </code> <code>'content'< /code><code>, get_post_format() ); ?></code> </div> | ||
Unchanged: <div>< code> < /code></div> | Unchanged: <div>< code> < /code></div> | ||
Unchanged: <div>< code> < /code>< code>< /div><!-- #resize --></code> </div> | Unchanged: <div>< code> < /code>< code>< /div><!-- #resize --></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><nav </code> <code>class</code> <code>= </code> <code>" nav-single" </code> <code> ></code> </div> | Unchanged: <div>< code> < /code>< code><nav </code> <code>class</code> <code>= </code> <code>" nav-single" </code> <code> ></code> </div> | ||
Unchanged: <div>< code> < /code>< code><h3 </code> <code>class</code> <code>= </code> <code>"assistive- text"< /code>< code>><?php _e( </code> <code>'Post navigation'< /code><code>, </code> <code>'twentytwelve'</code> <code>); ?></h3> </code> </div> | Unchanged: <div>< code> < /code>< code><h3 </code> <code>class</code> <code>= </code> <code>"assistive- text"< /code>< code>><?php _e( </code> <code>'Post navigation'< /code><code>, </code> <code>'twentytwelve'</code> <code>); ?></h3> </code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-previous" </code> <code> ><?php previous_post_link( </code> <code>'%link'</code> <code>, </code> <code>'<span> '</code> <code>. _x( </code> <code>'&larr;'< /code><code>, </code> <code>'Previous post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> %title'</code> <code>); ?></span> </code> </div> | Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-previous" </code> <code> ><?php previous_post_link( </code> <code>'%link'</code> <code>, </code> <code>'<span> '</code> <code>. _x( </code> <code>'&larr;'< /code><code>, </code> <code>'Previous post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> %title'</code> <code>); ?></span> </code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-next" </code> <code> ><?php next_post_link( </code> <code>'%link'</code> <code>, </code> <code>'%title <span>'</code> <code>. _x( </code> <code>'&rarr;'< /code><code>, </code> <code>'Next post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> '</code> <code>); ?></span> </code> </div> | Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-next" </code> <code> ><?php next_post_link( </code> <code>'%link'</code> <code>, </code> <code>'%title <span>'</code> <code>. _x( </code> <code>'&rarr;'< /code><code>, </code> <code>'Next post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> '</code> <code>); ?></span> </code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code>< /nav><!-- .nav-single --></code> </div> | Unchanged: <div>< code> < /code>< code>< /nav><!-- .nav-single --></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code>< ?php</code> </div> | Unchanged: <div>< code> < /code>< code>< ?php</code> </div> | ||
Unchanged: <div>< code> < /code><code>// If comments are open or we have at least one comment, load up the comment template</ code></div> | Unchanged: <div>< code> < /code><code>// If comments are open or we have at least one comment, load up the comment template</ code></div> | ||
Unchanged: <div>< code> < /code>< code>if</code> <code>( comments_open() || </code> <code>'0'</code> <code>!= get_comments_number() )</code> </div> | Unchanged: <div>< code> < /code>< code>if</code> <code>( comments_open() || </code> <code>'0'</code> <code>!= get_comments_number() )</code> </div> | ||
Unchanged: <div>< code> < /code>< code>comments_template( </code> <code>''</code> <code>, true );</code> </div> | Unchanged: <div>< code> < /code>< code>comments_template( </code> <code>''</code> <code>, true );</code> </div> | ||
Unchanged: <div>< code> < /code>< code>?> </code> </div> | Unchanged: <div>< code> < /code>< code>?> </code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><?php </code> <code>endwhile</ code><code>; </code> <code>// end of the loop. ?></code> </div> | Unchanged: <div>< code> < /code>< code><?php </code> <code>endwhile</ code><code>; </code> <code>// end of the loop. ?></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code>< /div><!-- #content --></code> </div> | Unchanged: <div>< code> < /code>< code>< /div><!-- #content --></code> </div> | ||
Unchanged: <div>< code> </code> <code>< /div><!-- #primary --></code> </div> | Unchanged: <div>< code> </code> <code>< /div><!-- #primary --></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code><?php get_sidebar(); ?></code> </div> | Unchanged: <div>< code><?php get_sidebar(); ?></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code><?php get_footer(); ?></code> </div> | Unchanged: <div>< code><?php get_footer(); ?></code> </div> | ||
Unchanged: <pre> | Unchanged: <pre> | ||
Unchanged: [/php] | Unchanged: [/php] | ||
Unchanged: <h2>Step 2: Adding the Links to Resize Text</h2> | Unchanged: <h2>Step 2: Adding the Links to Resize Text</h2> | ||
Unchanged: The next step is to add the two links in the page which will act as two buttons to resize the text. You can place them anywhere within your page but avoid placing them within the loop. Here we have placed them in our <strong> single.php</strong> file and have associated them with two unique IDs. | Unchanged: The next step is to add the two links in the page which will act as two buttons to resize the text. You can place them anywhere within your page but avoid placing them within the loop. Here we have placed them in our <strong> single.php</strong> file and have associated them with two unique IDs. | ||
Unchanged: [php]<code> <?php</ code></pre> | Unchanged: [php]<code> <?php</ code></pre> | ||
Unchanged: <div>< code>/**< /code></div> | Unchanged: <div>< code>/**< /code></div> | ||
Unchanged: <div>< code> < /code><code>* The Template for displaying all single posts.</code> </div> | Unchanged: <div>< code> < /code><code>* The Template for displaying all single posts.</code> </div> | ||
Unchanged: <div>< code> < /code>< code>*< /code></div> | Unchanged: <div>< code> < /code>< code>*< /code></div> | ||
Unchanged: <div>< code> < /code><code>* @package WordPress< /code></div> | Unchanged: <div>< code> < /code><code>* @package WordPress< /code></div> | ||
Unchanged: <div>< code> < /code><code>* @subpackage Twenty_Twelve< /code></div> | Unchanged: <div>< code> < /code><code>* @subpackage Twenty_Twelve< /code></div> | ||
Unchanged: <div>< code> < /code><code>* @since Twenty Twelve 1.0</code> </div> | Unchanged: <div>< code> < /code><code>* @since Twenty Twelve 1.0</code> </div> | ||
Unchanged: <div>< code> < /code>< code>*/< /code></div> | Unchanged: <div>< code> < /code>< code>*/< /code></div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code>get_header(); ?></code> </div> | Unchanged: <div>< code>get_header(); ?></code> </div> | ||
Unchanged: <div>< code> </code> <code><div id=</code> <code>" primary" </code> <code>class< /code>< code>=< /code>< code>" site-content" </code> <code>> </code> </div> | Unchanged: <div>< code> </code> <code><div id=</code> <code>" primary" </code> <code>class< /code>< code>=< /code>< code>" site-content" </code> <code>> </code> </div> | ||
Unchanged: <div>< code> < /code>< code><div id=</code> <code>" content" </code> <code>role= </code> <code> "main" </code> <code>> </code> </div> | Unchanged: <div>< code> < /code>< code><div id=</code> <code>" content" </code> <code>role= </code> <code> "main" </code> <code>> </code> </div> | ||
Unchanged: <div>< code> < /code>< code><div </code> <code>class</code> <code>= </code> <code>" resize" </code> <code> ></code> </div> | Unchanged: <div>< code> < /code>< code><div </code> <code>class</code> <code>= </code> <code>" resize" </code> <code> ></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><?php </code> <code>while</code> <code>( have_posts() ) : the_post(); ?></code> </div> | Unchanged: <div>< code> < /code>< code><?php </code> <code>while</code> <code>( have_posts() ) : the_post(); ?></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><?php get_template_part( </code> <code>'content'< /code><code>, get_post_format() ); ?></code> </div> | Unchanged: <div>< code> < /code>< code><?php get_template_part( </code> <code>'content'< /code><code>, get_post_format() ); ?></code> </div> | ||
Unchanged: <div>< code> < /code></div> | Unchanged: <div>< code> < /code></div> | ||
Unchanged: <div>< code> < /code>< code>< /div><!-- #resize --></code> </div> | Unchanged: <div>< code> < /code>< code>< /div><!-- #resize --></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><nav </code> <code>class</code> <code>= </code> <code>" nav-single" </code> <code> ></code> </div> | Unchanged: <div>< code> < /code>< code><nav </code> <code>class</code> <code>= </code> <code>" nav-single" </code> <code> ></code> </div> | ||
Unchanged: <div>< code> < /code>< code><h3 </code> <code>class</code> <code>= </code> <code>"assistive- text"< /code>< code>><?php _e( </code> <code>'Post navigation'< /code><code>, </code> <code>'twentytwelve'</code> <code>); ?></h3> </code> </div> | Unchanged: <div>< code> < /code>< code><h3 </code> <code>class</code> <code>= </code> <code>"assistive- text"< /code>< code>><?php _e( </code> <code>'Post navigation'< /code><code>, </code> <code>'twentytwelve'</code> <code>); ?></h3> </code> </div> | ||
Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-previous" </code> <code> ><?php previous_post_link( </code> <code>'%link'</code> <code>, </code> <code>'<span> '</code> <code>. _x( </code> <code>'&larr;'< /code><code>, </code> <code>'Previous post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> %title'</code> <code>); ?></span> </code> </div> | Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-previous" </code> <code> ><?php previous_post_link( </code> <code>'%link'</code> <code>, </code> <code>'<span> '</code> <code>. _x( </code> <code>'&larr;'< /code><code>, </code> <code>'Previous post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> %title'</code> <code>); ?></span> </code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-next" </code> <code> ><?php next_post_link( </code> <code>'%link'</code> <code>, </code> <code>'%title <span>'</code> <code>. _x( </code> <code>'&rarr;'< /code><code>, </code> <code>'Next post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> '</code> <code>); ?></span> </code> </div> | Unchanged: <div>< code> < /code>< code><span </code> <code>class</code> <code>= </code> <code>" nav-next" </code> <code> ><?php next_post_link( </code> <code>'%link'</code> <code>, </code> <code>'%title <span>'</code> <code>. _x( </code> <code>'&rarr;'< /code><code>, </code> <code>'Next post link'</code> <code>, </code> <code>'twentytwelve'</code> <code>) . </code> <code>'</span> '</code> <code>); ?></span> </code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code>< /nav><!-- .nav-single --></code> </div> | Unchanged: <div>< code> < /code>< code>< /nav><!-- .nav-single --></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code>< ?php</code> </div> | Unchanged: <div>< code> < /code>< code>< ?php</code> </div> | ||
Unchanged: <div>< code> < /code><code>// If comments are open or we have at least one comment, load up the comment template</ code></div> | Unchanged: <div>< code> < /code><code>// If comments are open or we have at least one comment, load up the comment template</ code></div> | ||
Unchanged: <div>< code> < /code>< code>if</code> <code>( comments_open() || </code> <code>'0'</code> <code>!= get_comments_number() )</code> </div> | Unchanged: <div>< code> < /code>< code>if</code> <code>( comments_open() || </code> <code>'0'</code> <code>!= get_comments_number() )</code> </div> | ||
Unchanged: <div>< code> < /code>< code>comments_template( </code> <code>''</code> <code>, true );</code> </div> | Unchanged: <div>< code> < /code>< code>comments_template( </code> <code>''</code> <code>, true );</code> </div> | ||
Unchanged: <div>< code> < /code>< code>?> </code> </div> | Unchanged: <div>< code> < /code>< code>?> </code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code><?php </code> <code>endwhile</ code><code>; </code> <code>// end of the loop. ?></code> </div> | Unchanged: <div>< code> < /code>< code><?php </code> <code>endwhile</ code><code>; </code> <code>// end of the loop. ?></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code>< code>< /div><!-- #content --></code> </div> | Unchanged: <div>< code> < /code>< code>< /div><!-- #content --></code> </div> | ||
Unchanged: <div>< code> </code> <code>< /div><!-- #primary --></code> </div> | Unchanged: <div>< code> </code> <code>< /div><!-- #primary --></code> </div> | ||
Unchanged: <div>< code><p><a id=</code> <code>"increase- font"</code> <code>href= </code> <code> "#" </code> <code>>[ A+ ] </a>/<a id=</code> <code>"decrease- font"</code> <code>href= </code> <code> "#" </code> <code>>[ A- ] </a> </p>< /code></div> | Unchanged: <div>< code><p><a id=</code> <code>"increase- font"</code> <code>href= </code> <code> "#" </code> <code>>[ A+ ] </a>/<a id=</code> <code>"decrease- font"</code> <code>href= </code> <code> "#" </code> <code>>[ A- ] </a> </p>< /code></div> | ||
Unchanged: <div>< code><?php get_sidebar(); ?></code> </div> | Unchanged: <div>< code><?php get_sidebar(); ?></code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code><?php get_footer(); ?></code> </div> | Unchanged: <div>< code><?php get_footer(); ?></code> </div> | ||
Unchanged: <pre> | Unchanged: <pre> | ||
Unchanged: [/php] | Unchanged: [/php] | ||
Unchanged: <h2>Step 3: Adding the jQuery Magic</h2> | Unchanged: <h2>Step 3: Adding the jQuery Magic</h2> | ||
Unchanged: Now it’s time to add the jQuery magic to our theme in order to make the two increase/decrease links function. Under your theme’s <strong> js</strong> folder create a JavaScript file named <strong> resize.js</strong>. Now open the file and add the following piece of code. The code is clearly explained using the comments. | Unchanged: Now it’s time to add the jQuery magic to our theme in order to make the two increase/decrease links function. Under your theme’s <strong> js</strong> folder create a JavaScript file named <strong> resize.js</strong>. Now open the file and add the following piece of code. The code is clearly explained using the comments. | ||
Unchanged: [js]<code>// This prevents the execution of the code before the document is finished loading.</ code></pre> | Unchanged: [js]<code>// This prevents the execution of the code before the document is finished loading.</ code></pre> | ||
Unchanged: <div>< code>jQuery( document).ready( </code> <code> function</ code><code>() {</code> </div> | Unchanged: <div>< code>jQuery( document).ready( </code> <code> function</ code><code>() {</code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> </code> <code>// The 'A+' element in the page is associated with the jQuery click() event.</code> </div> | Unchanged: <div>< code> </code> <code>// The 'A+' element in the page is associated with the jQuery click() event.</code> </div> | ||
Unchanged: <div>< code> </code> <code>jQuery( </code> <code>'#increase-font'< /code>< code>).click( </code> <code> function</ code>< code>(event) {</code> </div> | Unchanged: <div>< code> </code> <code>jQuery( </code> <code>'#increase-font'< /code>< code>).click( </code> <code> function</ code>< code>(event) {</code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code><code>// This prevents the default action of the click() event from being triggered.< /code></div> | Unchanged: <div>< code> < /code><code>// This prevents the default action of the click() event from being triggered.< /code></div> | ||
Unchanged: <div>< code> < /code>< code>event.preventDefault( );</code> </div> | Unchanged: <div>< code> < /code>< code>event.preventDefault( );</code> </div> | ||
Unchanged: <div>< code> < /code><code>// The jQuery each() event interates over each elements belonging to the 'resize' class</code> </div> | Unchanged: <div>< code> < /code><code>// The jQuery each() event interates over each elements belonging to the 'resize' class</code> </div> | ||
Unchanged: <div>< code> < /code>< code>jQuery( </code> <code> '.resize'< /code>< code>).each( </code> <code>function</ code><code>() {</code> </div> | Unchanged: <div>< code> < /code>< code>jQuery( </code> <code> '.resize'< /code>< code>).each( </code> <code>function</ code><code>() {</code> </div> | ||
Unchanged: <div>< code> < /code><code>// Call to a custom function to increase the text size</code> </div> | Unchanged: <div>< code> < /code><code>// Call to a custom function to increase the text size</code> </div> | ||
Unchanged: <div>< code> < /code>< code>changeTextSize( </code> <code> this</code> <code>, change);</ code></div> | Unchanged: <div>< code> < /code>< code>changeTextSize( </code> <code> this</code> <code>, change);</ code></div> | ||
Unchanged: <div>< code> < /code>< code>});< /code></div> | Unchanged: <div>< code> < /code>< code>});< /code></div> | ||
Unchanged: <div>< code> </code> <code>}) ;</code> </div> | Unchanged: <div>< code> </code> <code>}) ;</code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> </code> <code>// The 'A-' element in the page is associated with the jQuery click() event.</code> </div> | Unchanged: <div>< code> </code> <code>// The 'A-' element in the page is associated with the jQuery click() event.</code> </div> | ||
Unchanged: <div>< code> </code> <code>jQuery( </code> <code>'#decrease-font'< /code>< code>).click( </code> <code> function</ code>< code>(event) {</code> </div> | Unchanged: <div>< code> </code> <code>jQuery( </code> <code>'#decrease-font'< /code>< code>).click( </code> <code> function</ code>< code>(event) {</code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div>< code> < /code><code>// This prevents the default action of the click() event from being triggered.< /code></div> | Unchanged: <div>< code> < /code><code>// This prevents the default action of the click() event from being triggered.< /code></div> | ||
Unchanged: <div>< code> < /code>< code>event.preventDefault( );</code> </div> | Unchanged: <div>< code> < /code>< code>event.preventDefault( );</code> </div> | ||
Unchanged: <div>< code> < /code><code>// The jQuery each() event interates over each elements belonging to the 'resize' class</code> </div> | Unchanged: <div>< code> < /code><code>// The jQuery each() event interates over each elements belonging to the 'resize' class</code> </div> | ||
Unchanged: <div>< code> < /code>< code>jQuery( </code> <code> '.resize'< /code>< code>).each( </code> <code>function</ code><code>() {</code> </div> | Unchanged: <div>< code> < /code>< code>jQuery( </code> <code> '.resize'< /code>< code>).each( </code> <code>function</ code><code>() {</code> </div> | ||
Unchanged: <div>< code> < /code><code>// Call to a custom function to decrease the text size</code> </div> | Unchanged: <div>< code> < /code><code>// Call to a custom function to decrease the text size</code> </div> | ||
Unchanged: <div>< code> < /code>< code>changeTextSize( </code> <code> this</code> <code>, -change);< /code></div> | Unchanged: <div>< code> < /code>< code>changeTextSize( </code> <code> this</code> <code>, -change);< /code></div> | ||
Unchanged: <div>< code> < /code>< code>});< /code></div> | Unchanged: <div>< code> < /code>< code>});< /code></div> | ||
Unchanged: <div>< code> </code> <code>}) ;</code> </div> | Unchanged: <div>< code> </code> <code>}) ;</code> </div> | ||
Unchanged: <div>< code>});< /code></div> | Unchanged: <div>< code>});< /code></div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div><code>// Three variables have been used to define range of the text size and the increment/decrement value respectively.< /code></div> | Unchanged: <div><code>// Three variables have been used to define range of the text size and the increment/decrement value respectively.< /code></div> | ||
Unchanged: <div>< code>var</code> <code>min = 8, max = 100, change = 2;</code> </div> | Unchanged: <div>< code>var</code> <code>min = 8, max = 100, change = 2;</code> </div> | ||
Unchanged: <div></div> | Unchanged: <div></div> | ||
Unchanged: <div><code>// Defines a custom function with two parameters determining the element to be resized and the size</code> </div> | Unchanged: <div><code>// Defines a custom function with two parameters determining the element to be resized and the size</code> </div> | ||
Unchanged: <div>< code>function</code> <code>changeTextSize(element, value) {</code> </div> | Unchanged: <div>< code>function</code> <code>changeTextSize(element, value) {</code> </div> | ||
Unchanged: <div>< code> </code> <code>var</code> <code>currentSize = parseFloat(jQuery( element).css( </code> <code>'font-size'< /code>< code>));< /code></div> | Unchanged: <div>< code> </code> <code>var</code> <code>currentSize = parseFloat(jQuery( element).css( </code> <code>'font-size'< /code>< code>));< /code></div> | ||
Unchanged: <div>< code> </code> <code>var</code> <code>newSize = currentSize + value;</code> </div> | Unchanged: <div>< code> </code> <code>var</code> <code>newSize = currentSize + value;</code> </div> | ||
Unchanged: <div>< code> </code> <code>if</code> <code>(newSize <= max && newSize >= min) {</code> </div> | Unchanged: <div>< code> </code> <code>if</code> <code>(newSize <= max && newSize >= min) {</code> </div> | ||
Unchanged: <div>< code> < /code>< code>jQuery( element).css( </code> <code>'font-size'< /code><code>, newSize + </code> <code>'px'</code> <code>) ;</code> </div> | Unchanged: <div>< code> < /code>< code>jQuery( element).css( </code> <code>'font-size'< /code><code>, newSize + </code> <code>'px'</code> <code>) ;</code> </div> | ||
Unchanged: <div>< code> </code> <code>}</code> </div> | Unchanged: <div>< code> </code> <code>}</code> </div> | ||
Unchanged: <div>< code>}< /code></div> | Unchanged: <div>< code>}< /code></div> | ||
Unchanged: <pre> | Unchanged: <pre> | ||
Unchanged: [/js] | Unchanged: [/js] | ||
Unchanged: <h2>Step 4: Referencing the Script in WordPress</h2> | Unchanged: <h2>Step 4: Referencing the Script in WordPress</h2> | ||
Unchanged: This is the final step in which we need to add a reference to the <strong> resize.js</strong> script in WordPress so that it executes the code. By default the WordPress installation already contains the jQuery library. We just need to reference the script within the theme. Open your <strong> functions.php< /strong> file and add the following code snippet. | Unchanged: This is the final step in which we need to add a reference to the <strong> resize.js</strong> script in WordPress so that it executes the code. By default the WordPress installation already contains the jQuery library. We just need to reference the script within the theme. Open your <strong> functions.php< /strong> file and add the following code snippet. | ||
Unchanged: [php]<code> function</code> <code>wptuts_resize_text () {</code> </pre> | Unchanged: [php]<code> function</code> <code>wptuts_resize_text () {</code> </pre> | ||
Unchanged: <div>< code> </code> <code>// The array('jquery') is used to create dependency on the jQuery library in order to use it properly.< /code></div> | Unchanged: <div>< code> </code> <code>// The array('jquery') is used to create dependency on the jQuery library in order to use it properly.< /code></div> | ||
Unchanged: <div>< code> </code> <code>wp_ enqueue_script( </code> <code>'resize'</ code><code>, get_template_ directory_uri() . </code> <code>'/ js/resize.js'< /code><code>, </code> <code>array</code> <code>( </code> <code>'jquery'</code> <code>) );</code> </div> | Unchanged: <div>< code> </code> <code>wp_ enqueue_script( </code> <code>'resize'</ code><code>, get_template_ directory_uri() . </code> <code>'/ js/resize.js'< /code><code>, </code> <code>array</code> <code>( </code> <code>'jquery'</code> <code>) );</code> </div> | ||
Unchanged: <div>< code>}< /code></div> | Unchanged: <div>< code>}< /code></div> | ||
Unchanged: <div>< code>add_action( </code> <code>'wp_ enqueue_scripts'< /code><code>, </code> <code>'wptuts_resize_ text'</code> <code>) ;</code> </div> | Unchanged: <div>< code>add_action( </code> <code>'wp_ enqueue_scripts'< /code><code>, </code> <code>'wptuts_resize_ text'</code> <code>) ;</code> </div> | ||
Unchanged: <pre>[/php] | Unchanged: <pre>[/php] | ||
Added: | |||
Unchanged: That’s it. Now if you are viewing a post, you can increment or decrement the font size of the post using the<strong>A+ </strong> and <strong>A- </strong> links in the page. You can use your own CSS to properly style and place the two links in your website as you see fit. | Unchanged: That’s it. Now if you are viewing a post, you can increment or decrement the font size of the post using the<strong>A+ </strong> and <strong>A- </strong> links in the page. You can use your own CSS to properly style and place the two links in your website as you see fit. |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.