Skip to content

Instantly share code, notes, and snippets.

@shizhua
shizhua / like-it-enqueue.php
Last active September 25, 2023 12:48
Add a like button without a plugin in WordPress
add_action( 'wp_enqueue_scripts', 'pt_like_it_scripts' );
function pt_like_it_scripts() {
if( is_single() ) {
wp_enqueue_style( 'like-it', trailingslashit( plugin_dir_url( __FILE__ ) ).'css/like-it.css' );
if (!wp_script_is( 'jquery', 'enqueued' )) {
wp_enqueue_script( 'jquery' );// Comment this line if you theme has already loaded jQuery
}
wp_enqueue_script( 'like-it', trailingslashit( plugin_dir_url( __FILE__ ) ).'js/like-it.js', array('jquery'), '1.0', true );
@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 / 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 / custom-control.js
Last active July 16, 2021 09:11
Multiple checkbox customizer control
jQuery( document ).ready( function() {
/* === Checkbox Multiple Control === */
jQuery( '.customize-control-checkbox-multiple input[type="checkbox"]' ).on( 'change', function() {
checkbox_values = jQuery( this ).parents( '.customize-control' ).find( 'input[type="checkbox"]:checked' ).map(
function() {
return this.value;
}
@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 / 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 / ajax.js
Last active April 27, 2017 04:59
Frontend Image Uploader in WordPress
( function( $ ) {
$('#upload_form').on('submit', function(e){
e.preventDefault();
var $this = $(this),
nonce = $this.find('#image_upload_nonce').val(),
images_wrap = $('#images_wrap'),
status = $('#status'),
formdata = false;
if ( $this.find('#images').val() == '' ) {
@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 / 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>