Skip to content

Instantly share code, notes, and snippets.

View fencermonir's full-sized avatar

Md. Monir Hossain fencermonir

View GitHub Profile
@fencermonir
fencermonir / hide_media_library_image.php
Last active November 15, 2019 14:49
Restricting Users to View Only Media Library Items When They Upload.
/**
* Restricting Users to View Only Media Library Items When They Upload
*/
add_filter( 'ajax_query_attachments_args','mmr_show_current_user_attachments' );
function mmr_show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id &&
!current_user_can('activate_plugins') &&
!current_user_can('edit_others_posts') ) {
@fencermonir
fencermonir / remove_shortcode_tag.php
Created November 13, 2019 08:36
remove shortcode tag from content.
$content = $testimonial->post_content;
$content = preg_replace('#\[[^\]]+\]#', '',$content);
apply_filters('the_content', $content);
@fencermonir
fencermonir / wp_media_uploader.js
Created November 9, 2019 11:04
WordPress Media Upload Enable.
/** The "Select Key File" button. */
jQuery( '.mdp-select-key-file-btn' ).on( 'click', function( e ) {
e.preventDefault();
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = jQuery( this );
wp.media.editor.send.attachment = function( props, attachment ) {
jQuery( button ).prev().val( attachment.id );
jQuery( button ).prev().prev().text( attachment.filename );
wp.media.editor.send.attachment = send_attachment_bkp;
};
$time = microtime(TRUE);
$mem = memory_get_usage();
[the code you want to measure here]
print_r(array(
'memory' => (memory_get_usage() - $mem) / (1024 * 1024),
'seconds' => microtime(TRUE) - $time
));
@fencermonir
fencermonir / check_bd_mobile_number.js
Created September 4, 2019 17:47
Checking bangladesh mobile number.
$("#place_order").on('click',function(e){
var mobile = $("#billing_phone").val();
var pattern = /\+?(88)?01[3456789][0-9]{8}\b/;
// return false;
if(!pattern.test(mobile) && mobile !== ''){
e.preventDefault();
// $("#on_form").find(".phone-valid").after('<p class="on_warning">Please enter a valid mobile number.</p>');
alert('Please enter a valid mobile number.');
}
});
<?php
/**
* Get all type posts
*
* @return void
* @author alispx/dompl
**/
function alispx_get_type_posts_data($post_type = 'post')
{
@fencermonir
fencermonir / wp_api.php
Last active July 1, 2019 13:56
Get API response in wp way.
<?php
$url = 'http://example.org/api';
$response = wp_remote_get( esc_url_raw( $url ) );
/* Will result in $api_response being an array of data,
parsed from the JSON response of the API listed above */
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
// details: https://codex.wordpress.org/Function_Reference/wp_remote_retrieve_body
@fencermonir
fencermonir / woocommerce-cart.php
Created June 19, 2019 11:47
woocommerce-cart
functions.php
===============
function mmr_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
// $count = WC()->cart->cart_contents_count;
//$count = WC()->cart->get_cart_contents_count();
?>
@fencermonir
fencermonir / log_file.php
Created May 31, 2019 08:32
Creat log file
/**
* Create log file
*/
function create_log_file($arr,$arr1 = array())
{
/* write log file */
// $time = date( "F jS Y, H:i", time() );
$time = (new DateTime('America/New_York'))->format('F jS Y, H:i A');
$output = "Time: " . "$time\r\n";
$output .= "Browser: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
@fencermonir
fencermonir / gist:f4d1c4f6c95c463b8cc1d6dbd6288a7d
Created May 20, 2019 09:19
Get all attribute of a shortcode.
preg_match_all( '/' . get_shortcode_regex() . '/s', $text, $matches );
$output = array();
if( isset( $matches[2] ) )
{
foreach( (array) $matches[2] as $key => $value )
{
if( $tag === $value ) {
$output[] = shortcode_parse_atts( $matches[3][$key] );
}
}