Code & Me
WordPress Code Snippet Repository
  • Home
  • Featured
  • Recently Added
  • About
  • Contact

Securing Your WordPress Blog Uploads Directory

April 21, 2014 By Editorial Staff in Uncategorized No Comments Tags: Blog, Code, Code Snippet, Directory, Secure, Securing, Snippet, Uploads, Uploads Directory, WordPress, WordPress Blog, WordPress Snippet

Although WordPress is a very secure system one actually thinks of BUT since there is always a room open for more improvement, using the following snippet will let you secure your WordPress blog uploads directory.

To do so, first of all, create a file named .htaccess and paste the following code in it. Once you created it, upload the same into your wp-content/uploads directory. Remember at line 5 in the snippet below; make sure to add/update all the extensions you want to make it work for you.

1
2
3
4
5
6
7
8
<Files ~ ".*..*">
Order Allow,Deny
Deny from all
</Files>
<FilesMatch ".(jpg|jpeg|jpe|gif|png|tif|tiff)$">
Order Deny,Allow
Allow from all
</FilesMatch>

Snippet Source/Credit: Jeff Starr

How To Remove WordPress Version Number

April 18, 2014 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, Version Number, WordPress, WordPress Snippet, WordPress Version, WordPress Version Number

WordPress, by default, publicly display its version number in both its RSS and Atom feeds. Well, while using the snippet, you will be able to remove it. Simply paste the following snippet in your functions.php file and you will be able to remove WordPress version number.

1
2
3
4
add_filter('the_generator', 'digwp_complete_version_removal');
function digwp_complete_version_removal() {
return '';
}

Snippet Source/Credit: Jeff Starr

How To Automatically Delete Trash On Daily Basis

By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Delete Trash, Snippet, Trash, WordPress, WordPress Snippet

When you delete a post or a page or a comment, it goes in a Trash instead of being removed permanently. Using the following snippet will ease you job of deletion and will tell WordPress to automatically delete trash itself. Simply copy and paste the following lines of code in your wp-config,php file and you are done.

1
define('EMPTY_TRASH_DAYS', 1);

Note: Make sure that you do replace 1 by X to empty spam comments automatically every X days.

Making Translatable JavaScript Strings On Your WordPress Theme

April 16, 2014 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, JavaScript, JavaScript Strings, Snippet, String, Strings, Theme, WordPress, WordPress Snippet, WordPress Theme

WordPress by default comes with a function known as wp_localize_script(). This function allows you to localize JavaScript strings. Well, with the help of snippet below, you will be able to make translatable JavaScript strings on your WordPress theme. Make sure you paste the following code into your function.php file.

1
2
3
4
5
6
7
8
function prefix_enqueue_custom_script(){
wp_register_script( 'prefix_custom_script', plugin_dir_url( __FILE__ ) .'js/custom-script.js', array( 'jquery' ) );
        wp_enqueue_script( 'prefix_custom_script' );
        wp_localize_script( 'prefix_custom_script', 'prefix_object_name', array(
'upload' => __( 'upload', 'textdomain' ),
'remove' => __( 'remove', 'textdomain' )
) );
}

Snippet Source/Credit: WPTheming.com

How To Add PDF Support To Your WordPress Blog Media Manager

By Editorial Staff in Featured, WordPress No Comments Tags: Blog, Blog Manager, Code, Code Snippet, PDF, PDF Support, Snippet, Support, WordPress, WordPress Blog Manager, WordPress Snippet

WordPress media manager, by default, allows you to filter media with the three types, say, images, audio and video. But, if in case, you required more and want to add PDF support to it, then here is a simple code snippet for you to do the task. Copy and paste the following snippet into your theme’s functions.php file and you are done.

1
2
3
4
5
6
7
8
9
modify_post_mime_types( $post_mime_types ) {
// select the mime type, here: 'application/pdf'
// then we define an array with the label values
$post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) );
// then we return the $post_mime_types variable
return $post_mime_types;
}
// Add Filter Hook
add_filter( 'post_mime_types', 'modify_post_mime_types' );

Snippet Source/Credit: WP Tuts

How To Easily Add Shortcuts To WordPress Toolbar

April 15, 2014 By Editorial Staff in WordPress No Comments Tags: Code Snippet, Shortcut, Shortcuts, Snippet, Toolbar, WordPress, WordPress Snippet, WordPress Toolbar

WordPress gives you the freedom to customize the WordPress Toolbar, thanks to the add_node() functions.  All you have to do is to simply paste the following snippet into your theme’s functions.php file. As soon as you are done, make sure to edit the following by changing id with “gmail” (the ID of the) followed by saving it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// add a link to the WP Toolbar
function custom_toolbar_link($wp_admin_bar) {
$args = array(
'id' => 'gmail',
'title' => 'Gmail',
'href' => 'https://mail.google.com/mail/#inbox',
'meta' => array(
'class' => 'gmail',
'title' => 'sales@digwp.com'
)
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 999);

Snippet Source/Credit: Jeff Starr

How To Force Your WordPress Blog To Break Out Of Frames

By Editorial Staff in Featured, WordPress No Comments Tags: Blog, Code Snippet, Frame, Snippet, WordPress, WordPress Blog, WordPress Snippet

You often noticed that there are many people who used another people’s content to make money by showing their ads in an add-on frame. I am sure, you definitely want to stop this thing around, using the following snippet, you will be able to force your WordPress blog to break out of frames, which means the visitor will only see your blog but not the one who put on an add-on frame. Simply paste the following code into your functions.php file, and you are done.

1
2
3
4
5
6
7
8
9
10
11
// Break Out of Frames for WordPress
function break_out_of_frames() {
if (!is_preview()) {
echo "\n<script type=\"text/javascript\">";
echo "\n<!--";
echo "\nif (parent.frames.length > 0) { parent.location.href = location.href; }";
echo "\n-->";
echo "\n</script>\n\n";
}
}
add_action('wp_head', 'break_out_of_frames');

Snippet Source/Credit: WP Mix

How To Remove Login Shake Effect At The Time When Error Occurs

April 13, 2014 By Editorial Staff in WordPress No Comments Tags: Code Snippet, Error, Login, Login Shake, Shake, Snippet, WordPress, WordPress Snippet

Using the following snippet will let you remove the login shake effect when error occurs. Simply paste the following code in your theme’s functions.php file and you are done.

1
2
3
4
function wps_login_error() {
        remove_action('login_head', 'wp_shake_js', 12);
}
add_action('login_head', 'wps_login_error');

Snippet Source/Credit: WordPress Support Forum

How To Automatically Add Twitter And Facebook Buttons To Your Posts

By Editorial Staff in Featured, WordPress No Comments Tags: Code Snippet, Facebook, Post, Snippet, Twitter, WordPress, WordPress Post, WordPress Snippet

Adding the following snippet to your theme’s functions.php file, you will be able to add Twitter and Facebook buttons to your posts. It will display both Facebook and Twitter buttons at the bottom of this post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function share_this($content){
    if(!is_feed() && !is_home()) {
        $content .= '<div class="share-this">
                    <a href="http://twitter.com/share"
class="twitter-share-button"
data-count="horizontal">Tweet</a>
                    <script type="text/javascript"
src="http://platform.twitter.com/widgets.js"></script>
                    <div class="facebook-share-button">
                        <iframe
src="http://www.facebook.com/plugins/like.php?href='.
urlencode(get_permalink($post->ID))
.'&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;colorscheme=light&amp;height=21"
scrolling="no" frameborder="0" style="border:none;
overflow:hidden; width:200px; height:21px;"
allowTransparency="true"></iframe>
                    </div>
                </div>';
    }
    return $content;
}
add_action('the_content', 'share_this');

Snippet Source/Credit: WPRecipes

Displaying Pingbacks/Trackbacks Count Within Admin Post Columns

April 11, 2014 By Editorial Staff in Featured, WordPress No Comments Tags: Code Snippet, Pingback, Snippet, Trackback, WordPress, WordPress Blog, WordPress Snippet

By default, in your WordPress blog, you did notice that the post list in your WordPress dashboard showed up only the comment count. But, what if it will be the case, if you wish to want trackback as well as pingback count. Well, this can be possible by doing by simply paste the following snippet in your theme’s functions.php file and you are done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function commentCount($type = 'comments'){
if($type == 'trackbacks'):
$typeSql = 'comment_type = "trackback"';
$oneText = 'One :trackback';
$moreText = '% :trackbacks';
$noneText = 'No :trackbacks';
elseif($type == 'pingbacks'):
$typeSql = 'comment_type = "pingback"';
$oneText = 'One :pingback';
$moreText = '% :pingbacks';
$noneText = 'No :pingbacks';
endif;
global $wpdb;
    $result = $wpdb->get_var('
        SELECT
            COUNT(comment_ID)
        FROM
            '.$wpdb->comments.'
        WHERE
            '.$typeSql.' AND
            comment_approved="1" AND
            comment_post_ID= '.get_the_ID()
    );
if($result == 0):
echo str_replace('%', $result, $noneText);
elseif($result == 1):
echo str_replace('%', $result, $oneText);
elseif($result > 1):
echo str_replace('%', $result, $moreText);
endif;
}
add_filter('manage_posts_columns', 'posts_columns_counts', 1);
add_action('manage_posts_custom_column', 'posts_custom_columns_counts', 1, 2);
function posts_columns_counts($defaults){
    $defaults['wps_post_counts'] = __('Counts');
    return $defaults;
}
function posts_custom_columns_counts($column_name, $id){
if($column_name === 'wps_post_counts'){
commentCount('trackbacks'); echo "<br />";
commentCount('pingbacks');
          }
}

Snippet Source/Credit: InstantShift

«< 62 63 64 65 66 >»

Recently Added Snippets

  • How To Set Role And Capabilities In WordPress
  • How To Set WordPress Options As Value
  • List Author Comments On Author Page
  • WordPress Display Page
  • Removing WordPress Version From Head And Feeds

Search The Snippet

Featured Snippets

  • How To Set WordPress Options As Value
  • WordPress Display Page
  • Removing WordPress Version From Head And Feeds
  • Delete Link In Comments In Your WordPress Website
  • Creating Database For Your WordPress Website
Code & Me
  • About
  • Recently Added
  • Advertise
  • Terms
  • Privacy Policy
  • Contact
© 2014-19 CodeAndMe.
Proud Member Of G2One Network Family.

↑ Back to top