Even though I frequently peek at the source code because I’ve been using WordPress since the beginning, I continue to learn new tricks. I’ve created a collection of 21 tips that are practical, witty, entertaining, or great precises that are infrequently used. I hope the list has something new for everyone.
1. WordPress Has A Ton Of Built-In Scripts
With dependency management, you can easily incorporate styles and scripts by using the fantastic wp_enqueue_script() and wp_enqueue_style() functions. But did you realise that WordPress already comes with a tonne of scripts? Some of the more well-known ones include jQuery, numerous jQuery UI components, jQuery Form, SWF Object, Tiny MCE, Jcrop, and Thickbox. The WordPress Codex contains the complete list. I suggest reading “The Developer’s Guide to Conflict-Free JavaScript and CSS in WordPress” right here on Smashing Magazine if you want to understand how to use the enqueue methods properly.
2. Replace Built-In Scripts By Deregistering Them
You can utilize scripts other than the ones that are built-in if you like to stay on the cutting edge. It is customary (though not always recommended) to use a more recent version of jQuery, which can be achieved in the following ways.
function my_scripts_method() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery-new.js');
wp_enqueue_script( 'jquery' );
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
But don’t just do this to boast about employing cutting-edge technology. For optimum compatibility, WordPress uses the version of jQuery that it does.
Only switch to a different version of jQuery if there are compatibility problems, such as when using a plugin that demands it.
3. Force Perfect JPG Images
This is a perfect illustration of the advantages of teamwork. In order to save space and bandwidth, my close friend Lars informed me that WordPress does not provide photographs at their highest possible quality. Naturally, he also provided me with a solution:
add_filter( 'jpeg_quality', 'smashing_jpeg_quality' );
function smashing_jpeg_quality() {
return 100;
}
90% is the standard quality in WordPress. Most of the time, this is acceptable; I doubt many people can tell the difference. However, adjusting the number can be the best option if high-quality images are required on your website (for a portfolio, photography, etc.).
4. FeedBurner Redirection
Almost every blog I’ve worked on uses FeedBurner, but I never know exactly how to set it up by heart. This passage, from Elio’s article “10 Tips to Optimise Your WordPress Theme,” is gratefully acknowledged:
add_action( 'template_redirect' , 'smashing_rss_redirect');
function smashing_rss_redirect() {
if ( is_feed() AND !preg_match( '/feedburner|feedvalidator/i', $_SERVER['HTTP_USER_AGENT'] ) ){
header( 'Location: https://feeds.feedburner.com/my_smashing_feed' );
header( 'HTTP/1.1 302 Temporary Redirect' );
}
}
5. Using General Taxonomy Functions
Several taxonomy routines can manage both your personal taxonomies and the pre-existing tags and categories. The complete list of taxonomy functions can be found in the Codex’s reference to functions. I enjoy utilising the functions get_term(), get_terms(), and wp_get_object_terms() in particular. I make as much use of these functions as I can, even for tags and categories, to make things more modular.
6. Setting Up Sessions In WordPress
Websites frequently employ sessions because they work well for storing data between pages. The session is never established because WordPress doesn’t make any use of them internally. You can start a session on all pages prior to any output by employing the next technique.
add_action( 'init', 'smashing_session_start' );
function smashing_session_start() {
if ( !session_id() ) {
session_start();
}
}
Although sessions are typically fairly secure, incorporate IP checking or additional nonce protection simply to be careful. You’ll be alright, though, as long as you’re only sending non-sensitive data. For further information, read Mark Jaquith’s excellent nonces article.
7. List All Hooked Functions
To do this, I started writing a function. It turned out that WP Recipes provided just what I wanted after I quickly searched Google.
function list_hooked_functions($tag=false){
global $wp_filter;
if ($tag) {
$hook[$tag]=$wp_filter[$tag];
if (!is_array($hook[$tag])) {
trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
return;
}
}
else {
$hook=$wp_filter;
ksort($hook);
}
echo '<pre>';
foreach($hook as $tag => $priority){
echo "<br />>>>>>t<strong>$tag</strong><br />";
ksort($priority);
foreach($priority as $priority => $function){
echo $priority;
foreach($function as $name => $properties) {
echo "t$name<br />";
}
}
}
echo '</pre>';
return;
}
You will receive a nice list of all hooked functions when used without an argument. This will be quite a large list, so you may add a hook to make it a little shorter. When debugging or playing about with hook priority, this is quite helpful. Understanding what is hooked into wp_head() and in what order is crucial, therefore this method is really helpful!
8. Automatically Add Paragraph Tags To Anything
WordPress automatically applies this to both the content and the excerpt, but there is no reason why it shouldn’t be used elsewhere. Wpautop() is the function that converts double line breaks into paragraphs.
$my_text = 'Welcome! Smashing Magazine is a great place to learn new things. I hope you’re having a nice time!'; echo wpautop( $my_text );
Sometimes you’ll want to remove this filter from the content and excerpt, which you can do as follows:
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
9. Send Emails Using WordPress
I recently authored a lengthy article called “Creating Perfect Emails for Your WordPress Website,” which included information on using the wp_mail() methods. You may send emails to users using these features and the great features that come with WordPress.
$message = 'Hello, thanks for reading my post! I hope to see you back soon.';
wp_mail( 'someonesemail@example.com', 'Thanks for reading my post!', $message);
You can also send HTML content by using a filter:
add_filter ("wp_mail_content_type", "smashing_mail_content_type");
function smashing_mail_content_type() {
return "text/html";
}
dedicated server reselling