Archive for category techniques

10 Useful WordPress Hook Hacks

  Hooks are very useful in WordPress. They allow you to “hook” a custom function to an existing function, which allows you to modify WordPress’ functionality without editing core files. In this article, we have compiled 10 extremely useful ready-to-use WordPress hooks, along with examples and coding explanations. Want to learn more about CSS coding and web design? Take a look at our upcoming Smashing Book ( $23.90 $29.90 , available worldwide). Pre-order now with 20% discount! What Is A Hook? To achieve a particular effect on a WordPress blog, you have to modify how WordPress works. Some of these modifications are made to what WordPress developers call “core files,” files required by WordPress to work properly. But modifying core files is always a bad idea. It may create a security loophole. Also, you will have lost the modification when you upgrade your WordPress installation. Still, developers need to overwrite some of WordPress’ functionality, which is why WordPress provides the Plugin API . Hooks are one of the main building blocks of WordPress plug-ins. Almost every plug-in uses a hook to overwrite WordPress’ core functionality. How to Use Hooks on Your WordPress Blog Unless you’re writing a plug-in, you would write hooks in the functions.php file. This file is located in the wp-content/themes/yourtheme directory. A hook, as you would expect, “hooks” one function to another. For example, you could write a custom function and attach it to one of WordPress’ core functions: add_action ( ‘publish_post’, ‘myCustomFunction’ ); In this example, we hook our own custom function to WordPress’ publish-post function, which means that every time WordPress executes the publish-post function, it will also execute this hooked function. Of course, we can also remove hooks using the remove_action() function: remove_action ( ‘publish_post’, ‘myCustomFunction’ ); Hooks resources: WordPress plugin API Codex page Adam Brown’s hook database Hooks from WpRecipes 1. Disable WordPress’ Automatic Formatting The problem . You have probably noticed that, by default, WordPress converts normal quotes to “curly” quotes, and makes other little formatting changes when a post is displayed. This is very cool for people who publish normal content, but anyone who uses their blog to discuss code will be annoyed because, when pasted in a text editor, code with curly quotes returns syntax errors. The solution . Simply paste the following code in your functions.php file: function my_formatter($content) { $new_content = ”; $pattern_full = ‘{([raw].*?[/raw])}is’; $pattern_contents = ‘{[raw](.*?)[/raw]}is’; $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($pieces as $piece) { if (preg_match($pattern_contents, $piece, $matches)) { $new_content .= $matches[1]; } else { $new_content .= wptexturize(wpautop($piece)); } } return $new_content; } remove_filter(’the_content’, ‘wpautop’); remove_filter(’the_content’, ‘wptexturize’); add_filter(’the_content’, ‘my_formatter’, 99); Once that’s done, you can use the [raw] shortcode in your posts: [raw]This text will not be automatically formatted.[/raw] Code explanation . Our first step here was to create a function that uses a regular expression to find the [raw] shortcode in your posts’ content. Then we hook our my_formatter() function to WordPress’ the_content() function, which means that my_formatter() will now be automatically called every time the_content() is called. To remove the automatic formatting, we use the remove_filter() function, which lets you delete a hook on a specific function. Source: Disable WordPress automatic formatting on posts using a shortcode 2. Detect The Visitor’s Browser Using A Hook The problem . Cross-browser compatibility is probably the most common problem in Web development. You will save yourself a lot of headaches if you are able to detect the browsers that people use to visit your website and then create a custom class wrapped in the body tag. Few people are aware of it, but WordPress can already detect browsers, and a few global variables are available for us to use. The solution . Nothing hard here: just paste the code below in your functions.php file, then save the file and you’re done! <?php add_filter(’body_class’,'browser_body_class’); function browser_body_class($classes) { global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone; if($is_lynx) $classes[] = ‘lynx’; elseif($is_gecko) $classes[] = ‘gecko’; elseif($is_opera) $classes[] = ‘opera’; elseif($is_NS4) $classes[] = ‘ns4′; elseif($is_safari) $classes[] = ’safari’; elseif($is_chrome) $classes[] = ‘chrome’; elseif($is_IE) $classes[] = ‘ie’; else $classes[] = ‘unknown’; if($is_iphone) $classes[] = ‘iphone’; return $classes; } ?> Once you have saved the file, the function will automatically add a CSS class to the body tag, as shown in the example below: <body class=”home blog logged-in safari”> Code explanation . WordPress has global variables that return true if a visitor is using a particular browser. If the visitor’s browser is Google Chrome, the $is_chrome variable will return true . This is why we create the browser_body_class() function, which returns the name of the visitor’s browser. Once that’s done, we just hook the function to WordPress’ body_class() function. Sources: Browser Detection and the body_class() Function How to detect the visitor’s browser within WordPress 3. Define Default Text In TinyMCE The problem . Many bloggers almost always use the same layout for their blog posts. Posts on my own blog WpRecipes.com are always displayed the same way: some text, some code and then some more text. What about saving time by forcing tinyMCE (WordPress’ visual editor) to display default text? The solution . Once again, hooks are the solution. Just open your functions.php file, paste the code and let the hooks work their magic! <?php add_filter(’default_content’, ‘my_editor_content’); function my_editor_content( $content ) { $content = “If you enjoyed this post, make sure to subscribe to my rss feed.”; return $content; } ?> Code explanation .

10 Useful WordPress Hook Hacks

  Hooks are very useful in WordPress. They allow you to “hook” a custom function to an existing function, which allows you to modify WordPress’ functionality without editing core files. In this article, we have compiled 10 extremely useful ready-to-use WordPress hooks, along with examples and coding explanations. Want to learn more about CSS coding and web design? Take a look at our upcoming Smashing Book ( $23.90 $29.90 , available worldwide). Pre-order now with 20% discount! What Is A Hook? To achieve a particular effect on a WordPress blog, you have to modify how WordPress works. Some of these modifications are made to what WordPress developers call “core files,” files required by WordPress to work properly. But modifying core files is always a bad idea. It may create a security loophole. Also, you will have lost the modification when you upgrade your WordPress installation. Still, developers need to overwrite some of WordPress’ functionality, which is why WordPress provides the Plugin API . Hooks are one of the main building blocks of WordPress plug-ins. Almost every plug-in uses a hook to overwrite WordPress’ core functionality. How to Use Hooks on Your WordPress Blog Unless you’re writing a plug-in, you would write hooks in the functions.php file. This file is located in the wp-content/themes/yourtheme directory. A hook, as you would expect, “hooks” one function to another. For example, you could write a custom function and attach it to one of WordPress’ core functions: add_action ( ‘publish_post’, ‘myCustomFunction’ ); In this example, we hook our own custom function to WordPress’ publish-post function, which means that every time WordPress executes the publish-post function, it will also execute this hooked function. Of course, we can also remove hooks using the remove_action() function: remove_action ( ‘publish_post’, ‘myCustomFunction’ ); Hooks resources: WordPress plugin API Codex page Adam Brown’s hook database Hooks from WpRecipes 1. Disable WordPress’ Automatic Formatting The problem . You have probably noticed that, by default, WordPress converts normal quotes to “curly” quotes, and makes other little formatting changes when a post is displayed. This is very cool for people who publish normal content, but anyone who uses their blog to discuss code will be annoyed because, when pasted in a text editor, code with curly quotes returns syntax errors. The solution . Simply paste the following code in your functions.php file: function my_formatter($content) { $new_content = ”; $pattern_full = ‘{([raw].*?[/raw])}is’; $pattern_contents = ‘{[raw](.*?)[/raw]}is’; $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($pieces as $piece) { if (preg_match($pattern_contents, $piece, $matches)) { $new_content .= $matches[1]; } else { $new_content .= wptexturize(wpautop($piece)); } } return $new_content; } remove_filter(’the_content’, ‘wpautop’); remove_filter(’the_content’, ‘wptexturize’); add_filter(’the_content’, ‘my_formatter’, 99); Once that’s done, you can use the [raw] shortcode in your posts: [raw]This text will not be automatically formatted.[/raw] Code explanation . Our first step here was to create a function that uses a regular expression to find the [raw] shortcode in your posts’ content. Then we hook our my_formatter() function to WordPress’ the_content() function, which means that my_formatter() will now be automatically called every time the_content() is called. To remove the automatic formatting, we use the remove_filter() function, which lets you delete a hook on a specific function. Source: Disable WordPress automatic formatting on posts using a shortcode 2. Detect The Visitor’s Browser Using A Hook The problem . Cross-browser compatibility is probably the most common problem in Web development. You will save yourself a lot of headaches if you are able to detect the browsers that people use to visit your website and then create a custom class wrapped in the body tag. Few people are aware of it, but WordPress can already detect browsers, and a few global variables are available for us to use. The solution . Nothing hard here: just paste the code below in your functions.php file, then save the file and you’re done! <?php add_filter(’body_class’,'browser_body_class’); function browser_body_class($classes) { global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone; if($is_lynx) $classes[] = ‘lynx’; elseif($is_gecko) $classes[] = ‘gecko’; elseif($is_opera) $classes[] = ‘opera’; elseif($is_NS4) $classes[] = ‘ns4′; elseif($is_safari) $classes[] = ’safari’; elseif($is_chrome) $classes[] = ‘chrome’; elseif($is_IE) $classes[] = ‘ie’; else $classes[] = ‘unknown’; if($is_iphone) $classes[] = ‘iphone’; return $classes; } ?> Once you have saved the file, the function will automatically add a CSS class to the body tag, as shown in the example below: <body class=”home blog logged-in safari”> Code explanation . WordPress has global variables that return true if a visitor is using a particular browser. If the visitor’s browser is Google Chrome, the $is_chrome variable will return true . This is why we create the browser_body_class() function, which returns the name of the visitor’s browser. Once that’s done, we just hook the function to WordPress’ body_class() function. Sources: Browser Detection and the body_class() Function How to detect the visitor’s browser within WordPress 3. Define Default Text In TinyMCE The problem

10 Useful WordPress Hook Hacks

  Hooks are very useful in WordPress. They allow you to “hook” a custom function to an existing function, which allows you to modify WordPress’ functionality without editing core files. In this article, we have compiled 10 extremely useful ready-to-use WordPress hooks, along with examples and coding explanations. Want to learn more about CSS coding and web design? Take a look at our upcoming Smashing Book ( $23.90 $29.90 , available worldwide). Pre-order now with 20% discount! What Is A Hook? To achieve a particular effect on a WordPress blog, you have to modify how WordPress works. Some of these modifications are made to what WordPress developers call “core files,” files required by WordPress to work properly. But modifying core files is always a bad idea. It may create a security loophole. Also, you will have lost the modification when you upgrade your WordPress installation. Still, developers need to overwrite some of WordPress’ functionality, which is why WordPress provides the Plugin API . Hooks are one of the main building blocks of WordPress plug-ins. Almost every plug-in uses a hook to overwrite WordPress’ core functionality. How to Use Hooks on Your WordPress Blog Unless you’re writing a plug-in, you would write hooks in the functions.php file. This file is located in the wp-content/themes/yourtheme directory. A hook, as you would expect, “hooks” one function to another. For example, you could write a custom function and attach it to one of WordPress’ core functions: add_action ( ‘publish_post’, ‘myCustomFunction’ ); In this example, we hook our own custom function to WordPress’ publish-post function, which means that every time WordPress executes the publish-post function, it will also execute this hooked function. Of course, we can also remove hooks using the remove_action() function: remove_action ( ‘publish_post’, ‘myCustomFunction’ ); Hooks resources: WordPress plugin API Codex page Adam Brown’s hook database Hooks from WpRecipes 1. Disable WordPress’ Automatic Formatting The problem . You have probably noticed that, by default, WordPress converts normal quotes to “curly” quotes, and makes other little formatting changes when a post is displayed. This is very cool for people who publish normal content, but anyone who uses their blog to discuss code will be annoyed because, when pasted in a text editor, code with curly quotes returns syntax errors. The solution .

10 Useful WordPress Hook Hacks

  Hooks are very useful in WordPress. They allow you to “hook” a custom function to an existing function, which allows you to modify WordPress’ functionality without editing core files. In this article, we have compiled 10 extremely useful ready-to-use WordPress hooks, along with examples and coding explanations. Want to learn more about CSS coding and web design? Take a look at our upcoming Smashing Book ( $23.90 $29.90 , available worldwide). Pre-order now with 20% discount! What Is A Hook? To achieve a particular effect on a WordPress blog, you have to modify how WordPress works. Some of these modifications are made to what WordPress developers call “core files,” files required by WordPress to work properly. But modifying core files is always a bad idea. It may create a security loophole. Also, you will have lost the modification when you upgrade your WordPress installation. Still, developers need to overwrite some of WordPress’ functionality, which is why WordPress provides the Plugin API . Hooks are one of the main building blocks of WordPress plug-ins. Almost every plug-in uses a hook to overwrite WordPress’ core functionality. How to Use Hooks on Your WordPress Blog Unless you’re writing a plug-in, you would write hooks in the functions.php file. This file is located in the wp-content/themes/yourtheme directory. A hook, as you would expect, “hooks” one function to another. For example, you could write a custom function and attach it to one of WordPress’ core functions: add_action ( ‘publish_post’, ‘myCustomFunction’ ); In this example, we hook our own custom function to WordPress’ publish-post function, which means that every time WordPress executes the publish-post function, it will also execute this hooked function. Of course, we can also remove hooks using the remove_action() function: remove_action ( ‘publish_post’, ‘myCustomFunction’ ); Hooks resources: WordPress plugin API Codex page Adam Brown’s hook database Hooks from WpRecipes 1. Disable WordPress’ Automatic Formatting The problem . You have probably noticed that, by default, WordPress converts normal quotes to “curly” quotes, and makes other little formatting changes when a post is displayed. This is very cool for people who publish normal content, but anyone who uses their blog to discuss code will be annoyed because, when pasted in a text editor, code with curly quotes returns syntax errors. The solution . Simply paste the following code in your functions.php file: function my_formatter($content) { $new_content = ”; $pattern_full = ‘{([raw].*?[/raw])}is’; $pattern_contents = ‘{[raw](.*?)[/raw]}is’; $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($pieces as $piece) { if (preg_match($pattern_contents, $piece, $matches)) { $new_content .= $matches[1]; } else { $new_content .= wptexturize(wpautop($piece)); } } return $new_content; } remove_filter(’the_content’, ‘wpautop’); remove_filter(’the_content’, ‘wptexturize’); add_filter(’the_content’, ‘my_formatter’, 99); Once that’s done, you can use the [raw] shortcode in your posts: [raw]This text will not be automatically formatted.[/raw] Code explanation . Our first step here was to create a function that uses a regular expression to find the [raw] shortcode in your posts’ content. Then we hook our my_formatter() function to WordPress’ the_content() function, which means that my_formatter() will now be automatically called every time the_content() is called. To remove the automatic formatting, we use the remove_filter() function, which lets you delete a hook on a specific function. Source: Disable WordPress automatic formatting on posts using a shortcode 2. Detect The Visitor’s Browser Using A Hook The problem . Cross-browser compatibility is probably the most common problem in Web development. You will save yourself a lot of headaches if you are able to detect the browsers that people use to visit your website and then create a custom class wrapped in the body tag. Few people are aware of it, but WordPress can already detect browsers, and a few global variables are available for us to use. The solution . Nothing hard here: just paste the code below in your functions.php file, then save the file and you’re done! <?php add_filter(’body_class’,'browser_body_class’); function browser_body_class($classes) { global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone; if($is_lynx) $classes[] = ‘lynx’; elseif($is_gecko) $classes[] = ‘gecko’; elseif($is_opera) $classes[] = ‘opera’; elseif($is_NS4) $classes[] = ‘ns4′; elseif($is_safari) $classes[] = ’safari’; elseif($is_chrome) $classes[] = ‘chrome’; elseif($is_IE) $classes[] = ‘ie’; else $classes[] = ‘unknown’; if($is_iphone) $classes[] = ‘iphone’; return $classes; } ?> Once you have saved the file, the function will automatically add a CSS class to the body tag, as shown in the example below: <body class=”home blog logged-in safari”> Code explanation . WordPress has global variables that return true if a visitor is using a particular browser. If the visitor’s browser is Google Chrome, the $is_chrome variable will return true . This is why we create the browser_body_class() function, which returns the name of the visitor’s browser. Once that’s done, we just hook the function to WordPress’ body_class() function. Sources: Browser Detection and the body_class() Function How to detect the visitor’s browser within WordPress 3. Define Default Text In TinyMCE The problem . Many bloggers almost always use the same layout for their blog posts. Posts on my own blog WpRecipes.com are always displayed the same way: some text, some code and then some more text.