Skip to content

Instantly share code, notes, and snippets.

View imvaskii's full-sized avatar
🎯
Focusing

Bhaskar imvaskii

🎯
Focusing
View GitHub Profile
@imvaskii
imvaskii / remove-img-attr.php
Created January 30, 2017 23:46
WordPress Filter to remove "height" and "width" attributes and values of image only.
<?php
add_filter( 'post_thumbnail_html', 'twr_remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'twr_remove_width_attribute', 10 );
add_filter( 'the_content', 'twr_remove_width_attribute', 10);
function twr_remove_width_attribute($html) {
// loop only through img tags
if ( preg_match_all( '/<img[^>]+>/ims', $html, $matches ) ) {
@imvaskii
imvaskii / remove_anon_filter.php
Last active February 20, 2018 00:18
WordPress: Removes anonymous object filter
<?php
if ( ! function_exists( 'remove_anonymous_object_filter' ) ) {
/**
* Removes anonymous object filter callbacks.
* Can be used for Closure classes, static class and even dynamic classes
*
* @param string $tag Hook name.
* @param string $class Class name
* @param string $method_to_remove Method name
@imvaskii
imvaskii / get_singe_line.php
Created May 16, 2017 07:01
WordPress: Functions to fetch one liner content from post_content
<?php
if ( ! function_exists( 'twr_get_progressive_line') ) {
/**
* Returns proper line string from given $string content by progressively matching each line of the given html blob
* @param string $content
* @param integer $nth_line nth line to scan of given string
* @param integer $scanned_chars characters scanned
* @return mixed|null|string
*/
@imvaskii
imvaskii / post_duplicator.php
Created May 25, 2017 01:18
Post duplicator component
<?php
/**
* Post duplicator component.
*
* @author Bhaskar K C
* @since 24/05/2017
*/
class Component_post_duplicator extends Component {
/**
@imvaskii
imvaskii / array_unique_by_key.php
Last active June 21, 2017 05:05
Returns unique multidimensional array
<?php
if ( ! function_exists( 'twr_array_unique_by_key' ) ) {
/**
* Return unique multidimensional array filtered by given key.
*
* @param $array should be multidimensional array
* @param string $key
* @return array
*/
function twr_array_unique_by_key( $array, $key ) {
@imvaskii
imvaskii / php_codesniffer_wp.sh
Created June 29, 2017 01:26
Runs php code sniffer via bash script
#!/bin/bash
set -e
set -u
set -o pipefail
# installs composer if required
function maybe_install_composer() {
# If composer exists then return
@imvaskii
imvaskii / pre-commit.sh
Created July 3, 2017 01:47
Pre commit hook for git, for testing phpcs via composer
#!/bin/bash
#
# To enable this hook, rename this file to "pre-commit" and move it to .git/hooks/ in your project dir.
# For windows user, if you are using Git for windows (https://git-scm.com/download/win) then use following shebang
# #!C:/Program\ Files/Git/usr/bin/sh.exe
# For windows I prefer you use cygwin so that it supports bash script interpretation
# For windows if you are using https://git-scm.com/download/win then by default it uses cygwin.
# This script assumes the working directory already does have composer dependencies installed.
# @dependency: composer, phpcs, wpcs
@imvaskii
imvaskii / maybe_include_author_posts.php
Created July 5, 2017 05:18
WordPress search functionality to include authors' posts in search result if the search keyword matches authors' name.
<?php
if ( ! function_exists( 'cre_custom_search_query' ) ) {
/**
* Creates custom search query
* @param WP_Query object
* @return posts object
*/
add_action( 'pre_get_posts', 'cre_custom_search_query', 100 );
function cre_custom_search_query( $query ) {
@imvaskii
imvaskii / limit-cront.php
Created July 19, 2017 01:02
Limitize WordPress default scheduled events.
<?php
add_filter( 'schedule_event', 'limit_cron_events' );
if ( ! funciton_exists( 'limit_cron_events' ) ) {
/**
* Limitize WordPress default scheduled tasks
* https://codex.wordpress.org/Plugin_API/Filter_Reference/schedule_event
*
* @param object $event event being scheduled
* @return void
@imvaskii
imvaskii / gist:18472b788ac32c4b16524cf2c25a5d87
Created October 6, 2017 04:07 — forked from stuart11n/gist:9628955
rename git branch locally and remotely
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote