Skip to content

Instantly share code, notes, and snippets.

@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 )
<?php
add_filter( 'manage_posts_columns', 'ws_cutom_columns_head', 20 );
add_action( 'manage_posts_custom_column', 'ws_cutom_columns_content', 10, 2 );
function ws_cutom_columns_head( $columns ) {
$columns['ws_post_id'] = 'ID';
return $columns;
@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 / category_add_form_fields.php
Last active July 19, 2022 03:23
Add custom field to Category and taxonomies
<?php
// Add the field to the Add New Category page
add_action( 'category_add_form_fields', 'pt_taxonomy_add_new_meta_field', 10, 2 );
function pt_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[cat_icon]"><?php _e( 'Font Awesome Icons', 'pt' ); ?></label>
<input type="text" name="term_meta[cat_icon]" id="term_meta[cat_icon]" value="">
@shizhua
shizhua / wp-snippets.php
Created September 11, 2015 08:41
Useful WordPress Snippets
<?php
/****
* Automatic Linking to Twitter Usernames within a post
****/
function content_twitter_mention($content) {
return preg_replace('/([^a-zA-Z0-9-_&amp;])@([0-9a-zA-Z_]+)/', "$1@$2", $content);
}
add_filter('the_content', 'content_twitter_mention');
add_filter('comment_text', 'content_twitter_mention');
@shizhua
shizhua / random-button-html.php
Last active September 30, 2016 00:09
Create a Random Post button in WordPress
<div class="random-post-button">
<a href="random post url">Random Post</a>
</div>
@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'){
@shizhua
shizhua / tax-like-it.js
Created January 17, 2016 09:34
Add a Like button to category/tag/taxonomy pages
jQuery( document ).on( 'click', '.pt-tax-like-it', function() {
var $this = jQuery(this),
id = jQuery(this).find('.like-button').attr('data-id'),
nonce = jQuery(this).find('.like-button').attr("data-nonce");
jQuery.ajax({
url : taxlikeit.ajax_url,
type : 'post',
data : {
action : 'pt_tax_like_it',
@shizhua
shizhua / 404.php
Created February 20, 2016 09:42
Redirecting 404 to any page in WordPress
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>