Posts categorized: PHP
Stay Logged in to WordPress
I work from home so can afford to leave tabs open for each of my WordPress sites. That way I can jump on anytime and update or add new content very quickly. The problem I kept running into is that WordPress automatically logs out users after 48 hours. Which means I have to log back in every day even when it’s not necessary. So I needed a way to stay logged in to WordPress indefinitely. Fortunately WordPress is very flexible and easy to customize, and the login duration can be changed via several different methods.
How to Check if Post has Taxonomy Term
Something I did not know about when working with Custom Post Types and Custom Taxonomies. Normally when checking if a regular WP Post belongs to a specific category, we can use the WordPress function in_category(). But that does not work with Custom Post Types. To check if a CPT belongs to a specific term in a Custom Taxonomy, use has_term() instead.
Shortcode to Display Recent Posts on Any Post or Page
WordPress provides a widget that can be used to display recent posts in any sidebar or widgetized location. Likewise many WordPress themes provide some sort of “recent post” functionality, so users can display their latest posts in specific locations around the theme. Such functionality is great and useful for displaying recent posts just about anywhere in your theme. Problem is, those methods don’t work for displaying recent posts inside of posts, pages, and custom post types. Like inside of post content itself. For that, we can use a shortcode.
WordPress Post Navigation Redux (New Tags!)
For years WordPress post navigation has been possible thanks to a flexible set of five functions, including posts_nav_link()
, next_post_link()
and next_posts_link()
. These navigational functions continue to work great in many WordPress themes, but there are newer, even more flexible functions available to theme developers. Introduced in WordPress 4, these new navigation functions can make it easier than ever to display nav links for your WordPress-powered posts.
WordPress Backup and Restore Theme Options
After taking the time to set a whole bunch of theme options, it’s nice to be able to make a quick backup of your theme settings. Many themes have this functionality built-in, but for themes that don’t, here is a plug-n-play snippet to create a “Backup/Restore Theme Options” page. You can see the technique in action in my shapeSpace theme (100% free and open-source WordPress starter theme).
Get Comment Info from the WordPress Database
An easy way for visitors to enter their emails is by commenting on a post. We did this recently for people to sign up for a notification email. Instead of using a plugin or custom function for a one-time email list, we just went with WordPress core functionality and used post comments for people to sign up. Then the trick is retrieving the comment information from the database for the sign-up post.
Displaying Theme Data with WordPress
A cool trick you can do with WordPress is display information directly from your theme’s style.css
stylesheet. I recently used this on a site where the theme’s version number is used throughout the template to keep things current and consistent.
Import Feed, Display in Multiple Columns
Recently I worked on a project where a single RSS feed was imported and displayed in multiple columns on the web page. Certain pages display feed items in two columns, others in groups of three or more. This technique uses WordPress’ built-in fetch_feed
functionality to parse external feeds, and a slice of PHP magic to display them in multiple columns. It’s flexible too, enabling any number of columns and feed items from anywhere in your theme/template files.
3 Ways to Reset the WordPress Loop
WordPress does an excellent job of keeping track of what’s happening with the loop, but once you start customizing parameters and setting up multiple loops, it’s a good idea to explicitly reset them using one of three WordPress template tags. In this DigWP post, we’ll explore each of these three loop-reset techniques to get a better understanding of when and how to use them in your WordPress themes.
Create an Articles-Only Feed
WordPress makes it easy to publish content in any number of categories, with any number of tags, and with any type of custom post format. So for example, in addition to full articles, you could also offer screencasts, links, side posts, tweets, and all sorts of other peripheral content. Complementary material may work great for visitors surfing around your site, but including all of that extra stuff in your RSS feed dilutes the potency of your main articles. The idea here is that your visitors will subscribe to the more focused content.
How to Redirect Logged-In Users with WordPress
WordPress provides a variety of functions to redirect logged-in users. So which is best? It really depends on your goals; they’re all good methods, it just depends on what you’re trying to accomplish.
In this DigWP tutorial, we explain each of these methods along with some useful tips and tricks along the way. These techniques enable you to redirect logged-in users to internal pages, external pages, and even return them to the current page. They’re some great tools to have in the ’ol belt. So let’s dig in..
Graphing WordPress Blog Comments Over Time
One of my other sites, CSS-Tricks, has been around a number of years now. There are nearly 1,400 unique pages of content almost all of which have a comment thread. I had a feeling that in the last four years, despite fairly steady growth in traffic and subscribers, that the number of comments per post has dropped. But how to prove it? I don’t know of a way to easily see that data.
Display Total Number of Blogroll Bookmarks
Quick WordPress tip for you today! A reader recently asked about displaying the total number of blogroll bookmarks on their site. This sounds simple enough but not everyone meddles with code these days, so here is a nice PHP snippet that will do the job. You can add this code anywhere in your theme template (wherever you want to display the total number of Blogroll links).
Speed Up Your Blogging with WordPress Shortcodes
Shortcodes are WordPress’ way of creating shortcuts for code snippets, strings of text, or anything else that you need to display on your site. So this means that you can save time by replacing your most commonly typed words and phrases with WordPress shortcodes. For example, if you are frequently typing your blog’s URL, you could place the following code your theme’s functions.php
: