It has been a while since I have posted a collection of the best hacks WordPress, WordPress is a CMS simple for anyone to learn. But for general users, it can be a nightmare and a potential disaster for the site you just built. By simplifying the system and add some hacks to disable fast or hide certain areas of the CMS you cannot only help customers, give you peace of mind knowing that the site is secure by removing any potential disaster.

When you write code wordpress themes, especially if you do it regularly, really helpful to have a selection of fragments of code in your toolbox a simple “copy-n-paste”, and when the need for functionality. There are useful pieces of code and hacks that will help expand the functionality of the WordPress site, and, of course, to save your time.

1. Replace WordPress Editor Font
add_action( 'admin_head-post.php', 'change_html_editor_font' );
add_action( 'admin_head-post-new.php', 'change_html_editor_font' );

function change_html_editor_font()
	{ ?>

<style type="text/css">#editorcontainer #content, #wp_mce_fullscreen { font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif; }</style>

<?php }

Source →

2. Add Thumbnails To RSS Feed
function wp_rss_post_thumbnail($content)
	{
		global $post;

		if(has_post_thumbnail($post->ID))
			{
				$content =	'<p>' . get_the_post_thumbnail($post->ID) . '</p>' . get_the_content();
			}
		return $content;
	}
add_filter('the_excerpt_rss', 'wp_rss_post_thumbnail');
add_filter('the_content_feed', 'wp_rss_post_thumbnail');
3. Display Content For Registered Users Only
function wp_member_check_shortcode($atts, $content = null)
	{
		if (is_user_logged_in() && !is_null($content) && !is_feed())
			{
				return $content;
			} else {
				return 'Sorry, this part is only available to our members.';
			}
	}
add_shortcode('member', 'wp_member_check_shortcode');

Usage :

[member]This text will be only displayed to registered users.[/member]

Source →

4. Youtube Shortcode
function wp_youtube($atts)
	{
		extract(shortcode_atts(array(
			"value" => 'http://',
			"width" => '475',
			"height" => '350',
			"name"=> 'movie',
			"allowFullScreen" => 'true',
			"allowScriptAccess"=>'always',
		), $atts));
		return '<object style="height: '.$height.'px; width: '.$width.'px"><param name="'.$name.'" value="'.$value.'"><param name="allowFullScreen" value="'.$allowFullScreen.'"></param><param name="allowScriptAccess" value="'.$allowScriptAccess.'"></param><embed src="'.$value.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"></embed></object>';
	}
add_shortcode("youtube", "wp_youtube");

Usage :

[youtube value="http://www.youtube.com/watch?v=6aa0Wi7puiI"]

Source →

5. Disable Theme Changing
function wp_lock_theme()
	{
		global $submenu, $userdata;
		get_currentuserinfo();
		if ($userdata->ID != 1)
			{
				unset($submenu['themes.php'][5]);
				unset($submenu['themes.php'][15]);
			}
	}
add_action('admin_init', 'wp_lock_theme');

Source →

6. WP Snap (Display A Thumbnail Of Any Website)
function wp_snap($atts, $content = null)
	{
		extract(shortcode_atts(array(
			"snap" => 'http://s.wordpress.com/mshots/v1/',
			"url" => 'http://www.wpidea.net', // site source
			"alt" => 'My image',
			"w" => '400', // width
			"h" => '300' // height
		), $atts));

		$img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>';

		return $img;
	}

add_shortcode("snap", "wp_snap");

Source →

7. Remove Jump Link In WordPress Excerpt
	function wp_jump_link($link)
		{
			$offset = strpos($link, '#more-');

			if ($offset)
				{
					$end = strpos($link, '"',$offset);
				}

			if ($end)
				{
					$link = substr_replace($link, '', $offset, $end-$offset);
				}
			return $link;
		}
	add_filter('the_content_more_link', 'wp_jump_link');
8. Send Article To Friend By Email
function wp_direct_email($text="Send by email")
	{
		global $post;

		$title = htmlspecialchars($post->post_title);
		$subject = 'Sur '.htmlspecialchars(get_bloginfo('name')).' : '.$title;
		$body = 'I recommend this page : '.$title.'. You can read it on : '.get_permalink($post->ID);
		$link = '<a rel="nofollow" href="mailto:?subject='.rawurlencode($subject).'&amp;body='.rawurlencode($body).'" title="'.$text.' : '.$title.'">'.$text.'</a>';
		return $link;
	}

Source →

9. Tag To Keyword
	function wp_tag()
		{
			$posttags = get_the_tags();
			$categories = get_the_category();
			if ($posttags)
				{
					foreach($posttags as $tag)
						{
							echo $tag->name . ', ';
						}
				}

			foreach($categories as $category)
				{
					$slug = $category->category_nicename;
				}

			echo $slug;

		}

Will display :

tag1, tag2, ..., category
10. Exclude Categories From RSS Feed
function exFilter($query)
	{
		if ($query->is_feed)
			{
				$query->set('cat','-5'); //Don't forget to change the category ID
			}
		return $query;
	}

add_filter('pre_get_posts','exFilter');