Skip to content

Instantly share code, notes, and snippets.

View milindmore22's full-sized avatar
🏠
Working from home

Milind More milindmore22

🏠
Working from home
View GitHub Profile
@milindmore22
milindmore22 / default-registered-route.php
Last active January 27, 2018 12:34
WP default registered user end point
<?php
// https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php#L64
register_rest_route(
$this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
@milindmore22
milindmore22 / cached_get_item_permissions_check.php
Last active January 27, 2018 11:42
cached_get_item_permissions_check
<?php
function cached_get_item_permissions_check( $request ) {
$error = new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
if ( (int) $request['id'] <= 0 ) {
return $error;
}
@milindmore22
milindmore22 / cached_count_user_posts.php
Last active January 27, 2018 11:54
cached_count_user_posts
<?php
/**
* Cached version of count_user_posts, which is uncached but doesn't always need to hit the db
*
* Count_user_posts is generally fast, but it can be easy to end up with many redundant queries.
* if it's called several times per request. This allows bypassing the db queries in favor of
* the cache
*
* @param int $user_id user id to get count.
@milindmore22
milindmore22 / optimizing-default-wordpress-rest-api-user-endpoint-redundant-db-calls-check-permissions.php
Created January 27, 2018 12:06
Optimizing default WordPress REST API user endpoint for redundant DB calls to check permissions.
<?php
/**
* Rest endpoint filter to change permission callback of item_permission_check.
*
* @param type $endpoints return endpoints.
*/
public function replace_rest_endpoints_permission_callback( $endpoints ) {
if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
@milindmore22
milindmore22 / post-type-count.sql
Last active November 14, 2019 14:29
SQL Query to get post count by post types
SELECT count(*), post_type FROM `wp_posts` WHERE post_type IN( SELECT DISTINCT post_type wp_posts ) GROUP BY post_type ORDER BY `count(*)` DESC
@milindmore22
milindmore22 / on-load-download.js
Created July 21, 2020 10:21
Test On Load Download
(function() {
window.addEventListener('load', () => {
const download_button = document.querySelectorAll( '.download-rt-whitepaper a' );
download_button[0].click();
});
})();
@milindmore22
milindmore22 / amp-script-local-storage.php
Last active October 23, 2020 09:10
Using AMP Script component for local storage.
<?php
/**
* Local Storage Demo
*
* @requires AMP WordPress Plugin.
*/
add_action(
'wp_head',
function() {
@milindmore22
milindmore22 / amp-local-storage-demo.php
Last active October 26, 2020 15:30
Local Storage Demo - on AMP and Non-AMP pages
<?php
/**
* AMP Local Storage.
*
* @package AMP_Local_Storage_Test
* @author Milind, rtCamp
* @license GPL-2.0-or-later
* @copyright 2020 rtCamp pvt. ltd.
*
* @wordpress-plugin
@milindmore22
milindmore22 / amp-server-side-redirect.php
Last active December 7, 2020 19:44
AMP server side redirect
<?php
add_action(
'wp',
function() {
add_action( 'amp_mobile_client_side_redirection', '__return_false' );
if ( ! isset( $_GET['noamp'] ) && wp_is_mobile() && function_exists( 'is_amp_endpoint' ) && ! is_amp_endpoint() && function_exists( 'amp_is_available' ) && amp_is_available() ) {
wp_safe_redirect( esc_url( add_query_arg( 'amp', '1', get_permalink() ) ), 302 );
}
<?php
/**
* Web Stories amp-onerror Workaround plugin bootstrap.
*
* @package Google\Web_Stories_AMP_OnError_Workaround
* @author Pascal Birchler, Google
* @license GPL-2.0-or-later
* @copyright 2021 Google Inc.
*
* @wordpress-plugin