Useful WordPress Snippets

I’m constantly looking and re looking for various tiny tricks and tips I’ve used over the years on multiple WordPress powered websites. I’ve grown sick of trying to remember which sites, and so I’ve spent a few hours compiling this list. This list is primarily for my own use, but if you should find it helpful… then knock yourself out 🙂 This list will definitely be expanding so I can use it for a one-stop-shop for every website I build.

But for now folks.. I hope you find this stuff useful.

I’d love to hear from people if anyone uses these… so leave a comment!

You plum!

Add Comments Delete & Spam To Front End:

function delete_comment_link($id) { 
if (current_user_can('edit_post')) { 
echo '| <a href="'.admin_url(">Delete</a> ';
echo '| <a href="'.admin_url(">Spam</a>'; 

Add the following after edit_comment_link()


Set a maximum number of post revisions to avoid DB bloat:

if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5);

Display All Settings Link:

 function all_settings_link() { add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php'); } add_action('admin_menu', 'all_settings_link');

This will display an extra ‘All Settings’ link in your Settings menu

Individual Dynamic Link To A Post / Page:

<a href="<?php echo get_permalink(1); ?>"><?php echo get_the_title(1); ?></a>

This will output a single dynamic link to a post or a page.

Individual Dynamic Link To A Category:

<a href="<?php echo get_category_link(1); ?>"><?php echo get_cat_name(1); ?></a>

This will output a single dynamic link to a category.

Front End Delete Post Link For Authorised Users with Javascript Double Confirmation

If you don’t have a functions.php in your WordPress theme, create ‘functions.php’ and add this function to it – preferably at the top.

    function wp_delete_post_link() {
    global $post;
    if ( $post->post_type == 'page' ) { if ( !current_user_can( 'edit_page', $post->ID ) ) { return; } }
    else { if ( !current_user_can( 'edit_post', $post->ID ) ) return; }

    $delLink = wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID);
    $variable = $post->post_title;
    $variable = str_replace( "'", "\\'", $variable);
    $variable = str_replace( '"', '\"', $variable);

    $url = get_bloginfo('url');
    if (current_user_can('edit_post', $post->ID)){ echo "<a href="\"$delLink\"">Delete</a>"; }

function javascriptdeletecheck() { ?>
<script type="text/javascript">
if (self != top) { 
	if (document.images)  { top.location.replace(window.location.href); }
    else { top.location.href = window.location.href; }

function checkStuff(val) {
	var check1=confirm("YOU ARE ABOUT TO DELETE A POST ENTITLED:\n----------\n\n "+val+"");

if (check1) {
	var check2=confirm("Are you 100% sure, because you won't be able to get it back?");
	if (check2) { return true; } else { return false; }

else { return false; }


<?php } 

Then add the following into your template within The Loop wherever you wish the delete link to go.

<?php wp_delete_post_link('Delete', ' [ <strong class="delete">', '</strong> ]') ?>

Front End Delete Comment Link

Open ‘comments.php’ and look for the edit comment link which will be something like:

<?php edit_comment_link('Edit',' [ ',' ] '); ?>

and add the following after it:

<?php if (current_user_can('edit_post', $post->ID)) { ?><strong>[ <a onclick="return confirm('Are you SURE you want to delete this comment?')" href='<?php echo wp_nonce_url("/wp-admin/comment.php?action=deletecomment&amp;c=$comment->comment_ID&amp;_wp_original_http_referer=" . wp_get_referer(), 'delete-comment_' . $comment->comment_ID.""); ?>'>Delete</a> ]</strong><?php } ?>

Get Slug:

You should be able to put the code below into your functions.php… or into the html head of your theme:

$post_id = get_post($post->ID); // get current post or page
$slug = $post_id->post_name; // define slug as $slug

Then use the following to echo the slug:

<?php echo $slug; ?>

Retrieve Post ID Outside of the Loop

Put this code into your functions.php:

function function_name() {
global $wp_query;
$thePostID = $wp_query->post->ID;

Then use the following to echo the Post ID:

<?php echo $post->ID ?>
Posted in: WordPress