Ryan Paul

Useful WordPress Stuff

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!


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

 

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

function wp_delete_post_link($link = 'Delete This', $before = '', $after = '')
{
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;
}
$link = "<a onclick=\"return confirm('Are you SURE you want to delete this post/page?')\" href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&amp;post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
echo $before . $link . $after;
}

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:
<?php
$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 ?>

  • val

    Many thanks.  Your Front End Delete Post Link For Authorised Users and  Front End Delete Comment Link   really work well. Many thanks.   Any possibility of  Front End  Make Post Sticky  For Authorised Users on their own Blog  ??