Skip to content

Instantly share code, notes, and snippets.

View alokstha1's full-sized avatar

Alok Shrestha alokstha1

View GitHub Profile
@alokstha1
alokstha1 / drupal.php
Created April 19, 2024 05:13
Load a view of a FIELD type for a profile in Drupal
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Drupal\Core\Render;
$nid = 1;
$entity_type = 'node';
$view_mode = 'teaser';
$builder = \Drupal::service('entity_type.manager')->getViewBuilder($entity_type);
$storage = \Drupal::service('entity.manager')->getStorage($entity_type);
@alokstha1
alokstha1 / mysql_cheat_sheet.md
Created November 1, 2023 01:07 — forked from bradtraversy/mysql_cheat_sheet.md
MySQL Cheat Sheet

MySQL Cheat Sheet

Help with SQL commands to interact with a MySQL database

MySQL Locations

  • Mac /usr/local/mysql/bin
  • Windows /Program Files/MySQL/MySQL version/bin
  • Xampp /xampp/mysql/bin

Add mysql to your PATH

@alokstha1
alokstha1 / Script.js
Created May 25, 2021 08:59
Get ajax action after ajaxComplete on WordPress
jQuery(function($) {
/**
* catch AJAX complete events, to catch wordpress actions
* @param {jQuery.Event} event
* @param {jqXHR} xhr XmlHttpRequest object
* @param {Object} settings options for the AJAX request
*/
$(document).ajaxComplete(function(event, xhr, settings) {
// Specify the AJAX action after which you want to execute your JS
if ("data" in settings && settings.data.indexOf("action=your_ajax_action") != -1) {
@alokstha1
alokstha1 / functions.php
Created July 30, 2020 04:39
Convert number into thousand and add K like in Facebook and other
<?php
function return_with_k_in_number( $number ) {
if( $number>1000 ) {
$x = round($number);
$x_number_format = number_format($x);
$x_array = explode(',', $x_number_format);
$x_parts = array('K', 'M', 'B', 'T');
@alokstha1
alokstha1 / memberpress-hooks.php
Created May 28, 2020 13:41 — forked from cartpauj/memberpress-hooks.php
Various Subscription and Transaction status hooks for MemberPress with some helpful comments
<?php
//Capture a new member signup. Only ever triggers once for each new member.
//Does not trigger for exising members who have subscribed to a Membership before.
//
//The user may not be logged in when this is called
//as it is triggered when a user is added over the REST API,
//and also when a user is added from the dashboard (MemberPress -> Members -> Add New)
function mepr_capture_new_member_signup_completed($event) {
$user = $event->get_data();
$txn_data = json_decode($event->args);
@alokstha1
alokstha1 / plugins.php
Created May 28, 2020 08:12
Create database on plugin activation hook
<?php
register_activation_hook( __FILE__, 'plugin_create_db' );
function plugin_create_db() {
// Create DB Here
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'test_table';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
@alokstha1
alokstha1 / functions.php
Created May 28, 2020 08:09
Findout any plugin error
<?php
function tl_save_error() {
update_option( 'plugin_error', ob_get_contents() );
}
add_action( 'activated_plugin', 'tl_save_error' );
@alokstha1
alokstha1 / functions.php
Created April 10, 2020 07:33
Register wp customizer
<?php
function cpm_cc_customize_register( $wp_customize ) {
$wp_customize->add_panel('cpm_cc_option_panel', array(
'capability' => 'edit_theme_options',
'theme_supports' => '',
'title' => __('CC Options'),
'description' => __('Panel to update theme options'), // Include html tags such as <p>.
'priority' => 10 // Mixed with top-level-section hierarchy.
)
@alokstha1
alokstha1 / mepr-active-memberships.php
Created August 9, 2019 04:13 — forked from cartpauj/mepr-active-memberships.php
Get a list of the current user's active MemberPress Subscriptions
<?php
if(class_exists('MeprUtils')) {
$user = MeprUtils::get_currentuserinfo();
if($user !== false && isset($user->ID)) {
//Returns an array of Membership ID's that the current user is active on
//Can also use 'products' or 'transactions' as the argument type
$active_prodcuts = $user->active_product_subscriptions('ids');
if(!empty($active_prodcuts)) {
@alokstha1
alokstha1 / functions.php
Created June 25, 2019 08:13
SQL to get results from the last Month
<?php
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM `$table` WHERE `user_id` = $user_id $extend AND MONTH(created) = MONTH(CURRENT_DATE())
AND YEAR(created) = YEAR(CURRENT_DATE()) order by id DESC" );
//created being a nave of a date column 2019-04-10 19:03:43