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

How Not To Allow HTML In Comments

February 2, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to restrict the use of HTML in comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);

Snippet Source/Credit: TheBlog.ca

Automatically Notifying Members On New Posts

January 26, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to automatically notify members on new posts in WordPress.

1
2
3
4
5
6
7
8
function email_members($post_ID)  {
    global $wpdb;
    $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;");    
    $users = implode(",", $usersarray);
    mail($users, "New WordPress recipe online!", 'A new recipe have been published on http://www.wprecipes.com');
    return $post_ID;
}
add_action('publish_post', 'email_members');

Snippet Source/Credit: WordPress Codex

Removing Widgets From WordPress Dashboard

January 19, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to remove widgets from WordPress dashboard.

1
2
3
4
5
6
7
//Remove unwanted widgets from Dashboard
function remove_dashboard_widgets() {
global$wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');

Snippet Source/Credit: WP Guru

Adding Custom Pinterest Button In WordPress Website

January 12, 2021 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following lines of code, you will be able to add custom Pinterest button in your WordPress website. All you have to do is to add to your header above the body tag:

1
<script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>

followed by Edit and add to your page where you want your button to appear.

1
<div class="socialIcon-box"><a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fyourdomain.com&media=<?php bloginfo(template_directory); ?>/images/youricon.png&description=Site%20Name%20and%20Description" class="pin-it-button" count-layout="horizontal"><img src="<?php bloginfo(template_directory); ?>/images/youricon.png" /></a></div>

Snippet Source/Credit: flashalexander.com

Setting Up WordPress Post Expiration Code

January 5, 2021 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The following snippet will let you set WordPress post expiration code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php  //to check against expiration date;
$timestamp = strtotime("now + 8 hours");  
$currentdate = date('YmdHis', $timestamp);
$expirationdate = get_post_custom_values('expiration');
if (is_null($expirationdate)) {
   $expirestring = '30005050235959'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
} else {  
if (is_array($expirationdate)) {
   $expirestringarray = implode($expirationdate);
}
$markup = array("/",":"," ");
$expirestring = str_replace($markup,"",$expirestringarray);
} //else
if (( $expirestring > $currentdate ) || (is_archive())) { ?>
<?php } //end if for expiration; ?>

Snippet Source/Credit: nrbet.com

Changing WordPress Role Access To Menu And Widgets

December 30, 2020 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The following snippet will let you change WordPress role access to menu and widgets.

1
2
3
4
5
6
<?php
// get the the role object
$role_object = get_role('editor');
// add $cap capability to this role object
$role_object->add_cap('edit_theme_options');
?>

Snippet Source/Credit: WP-Snippets.com

Deleting Post Revisions In WordPress

December 23, 2020 By Editorial Staff in WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

The following snippet will let you delete post revision in your WordPress website.

1
2
3
4
$wpdb->query( "
DELETE FROM $wpdb->posts
WHERE post_type = 'revision'
" );

Snippet Source/Credit: Hardeep Asrani

How To Display WordPress Excerpt Dynamic Length

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

The following snippet display the WordPress excerpt dynamic length.

1
2
3
4
5
6
7
8
9
10
11
12
function ms_rp_excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt) >= $limit) {
array_pop($excerpt);
$excerpt = implode(' ', $excerpt).'...';
}
else {
$excerpt = implode(' ', $excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);
return $excerpt;
}

Snippet Source/Credit: Snipplr

Getting Email Alerts For 404 Pages In WordPress

December 16, 2020 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to get email alerts for 404 pages in WordPress.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php // WP 404 ALERTS @ http://wp-mix.com/wordpress-404-email-alerts/
// set status
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
// site info
$blog  = get_bloginfo('name');
$site  = get_bloginfo('url') . '/';
$email = get_bloginfo('admin_email');
// theme info
if (!empty($_COOKIE["nkthemeswitch" . COOKIEHASH])) {
$theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]);
} else {
$theme_data = wp_get_theme();
$theme = clean($theme_data->Name);
}
// referrer
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = clean($_SERVER['HTTP_REFERER']);
} else {
$referer = "undefined";
}
// request URI
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
$request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
} else {
$request = "undefined";
}
// query string
if (isset($_SERVER['QUERY_STRING'])) {
$string = clean($_SERVER['QUERY_STRING']);
} else {
$string = "undefined";
}
// IP address
if (isset($_SERVER['REMOTE_ADDR'])) {
$address = clean($_SERVER['REMOTE_ADDR']);
} else {
$address = "undefined";
}
// user agent
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$agent = clean($_SERVER['HTTP_USER_AGENT']);
} else {
$agent = "undefined";
}
// identity
if (isset($_SERVER['REMOTE_IDENT'])) {
$remote = clean($_SERVER['REMOTE_IDENT']);
} else {
$remote = "undefined";
}
// log time
$time = clean(date("F jS Y, h:ia", time()));
// sanitize
function clean($string) {
$string = rtrim($string);
$string = ltrim($string);
$string = htmlentities($string, ENT_QUOTES);
$string = str_replace("\n", "<br>", $string);
 
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return $string;
}
$message =
"TIME: "            . $time    . "\n" .
"*404: "            . $request . "\n" .
"SITE: "            . $site    . "\n" .
"THEME: "           . $theme   . "\n" .
"REFERRER: "        . $referer . "\n" .
"QUERY STRING: "    . $string  . "\n" .
"REMOTE ADDRESS: "  . $address . "\n" .
"REMOTE IDENTITY: " . $remote  . "\n" .
"USER AGENT: "      . $agent   . "\n\n\n";
mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");
?>

Snippet Source/Credit: WP Mix

Removing News Feed In WordPress Dashboard

December 9, 2020 By Editorial Staff in Featured, WordPress No Comments Tags: Code, Code Snippet, Snippet, WordPress, WordPress Snippet, WordPress Snippets

Using the following snippet, you will be able to remove news feed in WordPress dashboard.

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
add_action('wp_dashboard_setup', 'my_dashboard_widgets');
function my_dashboard_widgets() {
     global $wp_meta_boxes;
     // remove unnecessary widgets
     // var_dump( $wp_meta_boxes['dashboard'] ); // use to get all the widget IDs
     unset(
          $wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins'],
          $wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary'],
          $wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']
     );
     // add a custom dashboard widget
     wp_add_dashboard_widget( 'dashboard_custom_feed', 'News from 10up', 'dashboard_custom_feed_output' ); //add new RSS feed output
}
function dashboard_custom_feed_output() {
     echo '<div class="rss-widget">';
     wp_widget_rss_output(array(
          'url' => 'http://www.get10up.com/feed',
          'title' => 'What\'s up at 10up',
          'items' => 2,
          'show_summary' => 1,
          'show_author' => 0,
          'show_date' => 1,
     ));
     echo "</div>";
}

Snippet Source/Credit: Smashing Magazine

< 1 2 3 4 5 >»

Recently Added Snippets

  • Removing WordPress Dashboard Widget
  • Removing WordPress Dashboard News Feed
  • Making Your WordPress Website SEO Friendly
  • How To Use Shortcode To Display A YouTube Video Thumbnail
  • Counting Retweets In Full Text

Search The Snippet

Featured Snippets

  • Making Your WordPress Website SEO Friendly
  • Counting Your Blogroll Links
  • How To Solve Multiple Loops Problem In WordPress
  • How To Automatically Notify Members On New Posts In WordPress
  • Automatically Notifying Members On New Posts
Code & Me
  • About
  • Recently Added
  • Advertise
  • Terms
  • Privacy Policy
  • Contact
© 2014-19 CodeAndMe.
Proud Member Of G2One Network Family.

↑ Back to top