Skip to content

Instantly share code, notes, and snippets.

@shizhua
shizhua / add-to-menu.php
Last active September 24, 2021 08:27
WordPress AJAX Login/Register
<?php
/**
* Automatically add a Login link to Primary Menu
*/
add_filter( 'wp_nav_menu_items', 'pt_login_link_to_menu', 10, 2 );
function pt_login_link_to_menu ( $items, $args ) {
if( ! is_user_logged_in() && $args->theme_location == apply_filters('login_menu_location', 'primary') ) {
$items .= '<li class="menu-item login-link"><a href="#pt-login">'.__( 'Login/Register', 'ptheme' ).'</a></li>';
}
return $items;
@shizhua
shizhua / add-meta-term-field.php
Last active March 14, 2016 13:55
Assign a color to category
<?php
add_action( 'category_add_form_fields', 'ccp_new_term_color_field' );
function ccp_new_term_color_field() {
wp_nonce_field( basename( __FILE__ ), 'jt_term_color_nonce' ); ?>
<div class="form-field jt-term-color-wrap">
<label for="jt-term-color"><?php _e( 'Color', 'jt' ); ?></label>
<input type="text" name="jt_term_color" id="jt-term-color" value="" class="jt-color-field" data-default-color="#ffffff" />
@shizhua
shizhua / custom-login-form-fields.php
Created March 13, 2016 13:09
Custom redirecting location after user logining in WordPress
<?php
/**
* Print an extra field into the login form
*
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/login_form
*/
function my_login_extra_field() {
?>
<p>
<label for="rd"><?php _e( 'Take me to' ); ?></label>
@shizhua
shizhua / functions.php
Created March 8, 2016 02:44
Add custom post states in WordPress
<?php
add_filter('display_post_states', 'wpsites_custom_post_states');
function wpsites_custom_post_states($states) {
global $post;
if( ('page'==get_post_type($post->ID)) && ('page-templates/custom-page-template.php'==get_page_template_slug($post->ID)) ) {
$states[] = __('Custom state');
}
}
@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();
?>
@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 / 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 / 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 / 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 / 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="">