Skip to content

Instantly share code, notes, and snippets.

View 50701's full-sized avatar

Sajal Sarkar 50701

View GitHub Profile
@50701
50701 / 01 - Ajax in WordPress
Created September 2, 2025 05:03
Sending some data from the front-end to WordPress using AJAX, then getting a response without reloading the page.
// functions.php
function my_ajax_scripts() {
wp_enqueue_script(
'my-ajax-script',
get_stylesheet_directory_uri() . '/my-ajax.js', // your JS file
array('jquery'),
null,
true
);
@50701
50701 / 01 - Search Form
Last active August 17, 2025 19:01
WordPress search workflow
// You can place this anywhere (header, sidebar, template file):
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text">Search for:</span>
<input type="search" class="search-field" placeholder="Search …" value="<?php echo get_search_query(); ?>" name="s" />
</label>
<button type="submit" class="search-submit">Search</button>
</form>
@50701
50701 / 01 - Comments Template for WordPress posts
Last active August 17, 2025 14:23
WordPress code snippet for displaying blog post comments
<?php
/**
* Comments Template for WordPress posts
* Shows threaded comments up to 10 levels
*/
if ( post_password_required() ) {
return; // Don't show comments if post is password protected
}
if ( comments_open() || get_comments_number() ) : ?>
@50701
50701 / Auto-Mention Parent Commenter
Created August 17, 2025 12:31
Automatically prepends @username when replying to a comment, works with or without threaded comments enabled.
<?php
/**
* Plugin Name: Auto-Mention Parent Commenter
* Description: Automatically prepends @username when replying to a comment, works with or without threaded comments enabled.
* Author: Sajal Sarkar
* Version: 1.0
*/
/**
* Prepend @username to replies before saving the comment.
@50701
50701 / 01 - functions.php — WooCommerce register query var and rewrite rule
Last active August 17, 2025 14:31
Register custom query var and add rewrite rule for pretty URLs.
<?php
/**
* Register 'layout' query var and add rewrite rule for pretty URLs.
*/
function my_register_query_vars( $vars ) {
$vars[] = 'layout'; // Allows ?layout= or /layout/
return $vars;
}
add_filter( 'query_vars', 'my_register_query_vars' );
@50701
50701 / Adding Custom Columns in WordPress Admin List Tables
Created August 8, 2025 08:50
Add custom column to the WordPress admin list table for posts, pages, CPTs, categories, tags, and custom taxonomies.
# Adding Custom Columns in WordPress Admin List Tables
This gist shows how to add custom columns to the WordPress admin list table for:
- **Posts**
- **Pages**
- **Custom Post Types (CPTs)**
- **Categories**
- **Tags**
- **Custom Taxonomies**
@50701
50701 / custom-general-settings-fields.php
Last active August 5, 2025 13:12
Adds a custom section and fields to Settings > General, with proper nonce, sanitization, and escaping.
<?php
/**
* Plugin Name: Custom General Settings Fields
* Description: Adds a custom section and fields to Settings > General, with proper nonce, sanitization, and escaping.
* Version: 1.0
* Author: Sajal Sarkar
*/
/**
@50701
50701 / custom-user-contact-fields.php
Last active August 5, 2025 13:14
Adds extra contact fields to user profiles (All contact fields added are shown in the “Contact Info” section)
<?php
/**
* Plugin Name: Custom User Contact Fields
* Description: Adds extra contact fields to user profiles.
* Version: 1.0
* Author: Sajal Sarkar
*/
@50701
50701 / custom-user-profile-fields.php
Last active August 5, 2025 13:14
Adds custom fields to user profiles in the WordPress admin.
<?php
/**
* Plugin Name: Custom User Profile Fields
* Description: Adds custom fields to user profiles in the WordPress admin.
* Version: 1.0
* Author: Sajal Sarkar
*/
@50701
50701 / taxonomy-meta-fields.php
Last active August 5, 2025 13:13
Adds custom meta fields to taxonomy terms (categories or tags) with nonce protection.
<?php
/**
* Plugin Name: Taxonomy Meta Example
* Description: Adds custom meta fields to taxonomy terms (categories or tags) with nonce protection.
* Version: 1.1
* Author: Sajal Sarkar
*/
/**