Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Function: Redirect to a random post
* URL: http://wpsites.org/?p=10370
* by Leo
*/
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
<?php if( function_exists( 'pt_time_to_read' ) ) { echo pt_time_to_read( 'Time to read: ', ' Minutes' ); } ?>
@shizhua
shizhua / excerpt-character-count.php
Created August 24, 2015 13:53
Add a Character Counter to Excerpt box
<?php
/**
* Add a Character Counter to Excerpt box
* URL: http://wpsites.org/?p=10503
*/
function excerpt_count_js(){
echo '<script>jQuery(document).ready(function(){
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:5px;right:80px;color:#666;\"><small>Excerpt length: </small><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"excerpt_counter\" readonly=\"\" style=\"background:#fff;\"> <small>character(s). (128 Characters MAX)</small></div>");
jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
@shizhua
shizhua / hide-admin-bar.php
Created August 24, 2015 13:54
Hide the admin bar in wordpress
<?php
add_filter( 'show_admin_bar', '__return_false' );
remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
?>
@shizhua
shizhua / remove-comment-url.php
Created August 24, 2015 13:57
Remove URL field from comment form in wordpress
<?php
/**
* Remove URL field from comment form in wordpress
* by Leo
* URL: http://wpsites.org/?p=10448
*/
add_filter('comment_form_default_fields', 'website_remove_url');
function website_remove_url( $fields ) {
if ( isset($fields['url']) ) {
unset( $fields['url'] );
@shizhua
shizhua / add_action.php
Created August 24, 2015 14:13
insert ad codes after first, second or nth post. http://wpsites.org/?p=10364/
<?php
function insert_between_posts( $post ) {
global $wp_query;
// Check if we're in the right template
if ( ! is_home() )
return;
// Check if we're in the main loop
if ( $wp_query->post != $post )
@shizhua
shizhua / testimonial.js
Created August 25, 2015 14:23
Add a Fancy Testimonial to your wordpress website
jQuery(document).ready(function($) {
var e = $(".mts-testimonial"),
t = $(".testimonials-authors li"),
f = $(".mts-testimonial:first"),
l = $(".testimonials-authors li:first");
f.css({
opacity: 1
});
l.addClass("active-testimonial");
t.hover(function() {
@shizhua
shizhua / prevent-admin-login.php
Created December 15, 2015 05:55
Prevent username "admin" to login page
add_filter( 'wp_authenticate', 'wpsites_no_admin_user' );
function wpsites_no_admin_user($user){
if($user == 'admin'){
exit;
}
}
add_filter('sanitize_user', 'wpsites_sanitize_user_no_admin',10,3);
function wpsites_sanitize_user_no_admin($username, $raw_username, $strict){
if($raw_username == 'admin' || $username == 'admin'){