Skip to content

Instantly share code, notes, and snippets.

View developer-anuragsingh's full-sized avatar

Anurag Singh developer-anuragsingh

  • Cendyn
  • Gurugram
View GitHub Profile
@developer-anuragsingh
developer-anuragsingh / info.txt
Last active November 16, 2019 06:42
Add 'Host Entry' and 'Virtual Hosts' for development environment
Windows
-----------
Step 1 - Open following location in your file browser
Step 2 - Open 'hosts' file (As administrator)
Step 3 - Add this line in bootom of the file
Step 4 - If you want that YOUR DOMAIN points your local directory than, add
127.0.0.1 YOUR-WEBSITE.COM # Replace 'YOUR-WEBSITE.COM' with your actual domain name
# Above '127.0.0.1' is your local IP (Localhost) address
@developer-anuragsingh
developer-anuragsingh / WP - Enable Maintenance Mode
Created November 14, 2019 08:34
Add a "under maintenance" message, if user is not logged in as admin
function wp_maintenance_mode()
{
if (!current_user_can('edit_themes') || !is_user_logged_in()) {
wp_die('<h1>Under Maintenance</h1><br />Website under planned maintenance. Please check back later.');
}
}
add_action('get_header', 'wp_maintenance_mode');
@developer-anuragsingh
developer-anuragsingh / wp-remove-comments.php
Created November 8, 2019 10:35
Totally remove/disable comments from WordPress Website
<?php
// function.php
// Disable support for comments and trackbacks in post types
function as_remove_comments_support_for_all_cpt() {
$post_types = get_post_types();
foreach ($post_types as $post_type) {
if(post_type_supports($post_type, 'comments')) {
@developer-anuragsingh
developer-anuragsingh / Add-field-in-rest-api.php
Last active October 23, 2019 17:45
Add a new field in existing REST API's response.
// Add a new field in REST API's on an existing WordPress object type.
// ref - https://developer.wordpress.org/reference/functions/register_rest_field/
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
register_rest_field(
'POST_TYPE', // Post type {string/array}
'post-meta', // Field name {string}
array( // Callback {array}
@developer-anuragsingh
developer-anuragsingh / custom-widget-google-map.php
Created October 15, 2019 06:54
Create Google map's custom widget and sidebar to add Lat-long, API Key, Div's class
<?php
/**
* Register Sidebar
*/
function register_google_map_sidebar() {
/* Register first sidebar name Primary Sidebar */
register_sidebar(
@developer-anuragsingh
developer-anuragsingh / custom-widget.js
Created October 15, 2019 06:50
Create WP widget and sidebar with WP Editor
jQuery(document).ready(
function($) {
$( '.widget-control-save' ).click(
function() {
// grab the ID of the save button
var saveID = $( this ).attr( 'id' );
// grab the 'global' ID
var ID = saveID.replace( /-savewidget/, '' );
@developer-anuragsingh
developer-anuragsingh / index.php
Created August 6, 2019 10:07
Multi locations marker for Bing Map
<?php
// Multi locations marker for Bing Map
// Add js file in header along with bing map API key
// Ex - http://www.bing.com/api/maps/mapcontrol?key=XXXXXXXXXX'
// Ref -https://bingmapsv8samples.azurewebsites.net/#Infobox_MultiplePushpins
$all_venue_locations = array(
array(
'name' => 'India Gate',
'address' => 'Address Details, <br> India Gate',
@developer-anuragsingh
developer-anuragsingh / custom_excerpt.php
Last active February 21, 2021 08:19
WP - Get 'excerpt' with HTML tags
/**
* Return post's excerpt without removing HTML tags
*
* @param int $count total content length to be return
*
* @return string
*/
function Get_As_excerpt($count = 150)
{
if (has_excerpt()) {
@developer-anuragsingh
developer-anuragsingh / as-debug-functions.php
Last active March 10, 2022 14:07
WP - Custom functions to debug website coding errors
<?php
// get current template name (php file)
// global $template;
// echo basename($template);
add_filter('show_admin_bar', '__return_false'); // Hide admin bar from frontend
function show_file_path(){
if( isset($_REQUEST['dev']) && (1 == $_REQUEST['dev']) ):
@developer-anuragsingh
developer-anuragsingh / WPML Plugin - Get the ID of original Post
Last active July 26, 2019 06:25
// WPML Plugin - Get the ID of original Post
// WPML Plugin - Get the ID of original Post
global $sitepress; // Assuming the plugin is active
$original_post_id = apply_filters(
'wpml_object_id',
get_the_id(),
get_post_type(get_the_id()),
false,
$sitepress->get_default_language()
);