WordPress Snippets

17 posts

Add Pages, Custom Post Types in WordPress Site Feed

If you are running your WordPress site as a CMS, you may come across need to include pages from your site in the main feed, along with your posts. Well, the below code will give you that ability. function add_pages_to_rss_feed( $args ) { if ( isset( $args[‘feed’] ) && !isset( […]

Automatically Add Attributes to All Images in WordPress Posts

The below snippet will allow you to add an attribute to all images embedded in a WordPress post. function sumtips_image_attribute($content) { global $post; $pattern =”/<a(.*?)href=(‘|\”)(.*?).(bmp|gif|jpeg|jpg|png)(‘|\”)(.*?)>/i”; $replacement = ‘<a$1href=$2$3.$4$5 attribute=”attribute_value”‘; $content = preg_replace($pattern, $replacement, $content); return $content; } add_filter(‘the_content’, ‘sumtips_image_attribute’); Replace attribute=”attribute_value” to whatever attribute you need. Example: function sumtips_image_attribute($content) { […]

Search and Delete Unused Post Tags in WordPress

Here are three SQL queries to find and safely delete all unused tags from your WordPress blog database. The below command will show you all the unused tags in the database. SELECT * FROM wp_terms wterms INNER JOIN wp_term_taxonomy wttax ON wterms.term_id = wttax.term_id WHERE wttax.taxonomy = ‘post_tag’ AND wttax.count […]

Display Simple Maintenance Mode Message to Non-Admins

Here’s a snippet to display a simple maintenance mode message to all non-admin visitors of your WordPress blog, while you carry on your tasks in the back-end. function wp_maintenance_mode() { if ( !is_user_logged_in() || !current_user_can( ‘manage_options’ ) ) { wp_die(‘Site is undergoing maintenance at the moment, please come back after […]