Category: blogs

  • Delete orphan data from post meta in WordPress

    What is orphan data?

    Sometimes there are entries in postmeta table for those ids which is not exist in main post tables during testing new functionality or developing any plugins.

     

    Why you should remove orphan data?

    It’s not necessary if you have limited data. But if you have a huge data in your database, then you will need to remove those unnecessary data.

     

    Use below code snippet for removing orphan data from postmeta table: TAKE A BACKUP BEFORE RUNNING THE SCRIPT.

    DELETE pm FROM wp_postmeta pm
    LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
    WHERE wp.ID IS NULL

     

     

     

  • Fix Unhandled rejection Error: EACCES: permission denied

    Fix Unhandled rejection Error: EACCES: permission denied

    I was trying to install gulp on my mac.

    But I was getting following errors while running command sudo npm install gulp -g

    Unhandled rejection Error: EISDIR: illegal operation on a directory, open '/Users/bhargav/.npm/_cacache/....'
    
    Unhandled rejection Error: EISDIR: illegal operation on a directory, open '/Users/bhargav/.npm/_cacache/....'
    
    npm ERR! cb() never called!
    
    npm ERR! This is an error with npm itself. Please report this error at:
    npm ERR!     <https://npm.community>
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /Users/bhargav/.npm/_logs/2019-02-05T14_33_58_146Z-debug.log

    After a lot of findings from StackOverflow, I found the following solution for that.

    You have to run following two commands and that’s it.

    sudo chown -R $USER:$GROUP ~/.npm
    sudo chown -R $USER:$GROUP ~/.config
    

     

  • BuddyPress Profile Field Duplicator WordPress Plugin

    BuddyPress Profile Field Duplicator WordPress Plugin

    Sometimes people needs to add the same profile field many times. Specially when it comes to add muti-select, radio, checkbox etc., then it will be time consuming and might be irritating.

    So now you don’t need to add the same field again and again. It’s just a matter of one click now.

    See the screenshot below

    You can get this simple plugin from here.

  • Alternative for URLSearchParams

    IE doesn’t support “URLSearchParams”. So sometimes your script will not work in IEs.

    So the alternative for the “URLSearchParams” is as below.

    window.getUrlParameter = function( keyword, query_string ) {
        keyword = keyword.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp( keyword + '=([^&#]*)');
        var results = regex.exec( query_string );
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

     

    We can use the above function like this:

    Suppose you have query string like “abc=test&action=wow” and you want to search values of the particlular query string. So you can use it something like below:

    var query_string = 'abc=test&action=wow';
    
    getUrlParameter( 'abc', query_string ); // You will get value "test"
    
    getUrlParameter( 'action', query_string ); // You will get value "wow"
  • Add custom filter for searching post or users in WordPress backend

    Add custom filter for searching post or users in WordPress backend

    For this, two parts are important.

    Part 1: Adding select dropdown

    Below is the action by which we can add a new search filter for users:

    add_action( 'manage_users_extra_tablenav',  'add_custom_search_filter' );
    /**
     * Add new filter for users list in backend by filter with church name
     *
     * @param  string $which Button position top or bottom.
     * @return html
     */
    function add_custom_search_filter( $which ) {
    
    	// Get all existing church.
    	$select_options = array( 'option1', 'option2', 'option3' );
    
    	// If no churches are there, then don't display filter.
    	if ( empty( $select_options ) ) {
    		return;
    	}
    
    	// Get selected church from filter.
    	$selected_option = ( isset( $_GET['custom_select_' . $which ] ) ) ?
    	                   $_GET['custom_select_' . $which ] :
    	                   '';
    
    	?>
    	<label class="screen-reader-text" for="ff_user_church">
    		<?php _e( 'Select Option&hellip;' ); ?>
    	</label>
    
    	<select name="custom_select_<?php echo $which; ?>">
    
    		<option value=""><?php _e( 'Slect Option&hellip;' ); ?></option>
    		<?php
    
    			// Set options for filter.
    			foreach ( $select_options as $option ) {
    
    				echo sprintf(
    					'<option value="%s" %s>%s</option>',
    					esc_attr( $option ),
    					selected( $option, $selected_option, false ),
    					esc_html( $option )
    				);
    
    			}
    
    		?>
    	</select>
    	<?php
    	// Set submit button.
    	submit_button( __( 'Filter' ), null, $which, false );
    
    }

     

    In the above function, setting the name of the select field is very important. You have to prefix the $which with your name like below:

    <select name="custom_select_<?php echo $which; ?>">

    If you don’t add “$which”, then you won’t be able to search with the selected value.

     

    Part 2: Search query

    By the following action, we can modify user query according to our new custom filter created in part 1.

    add_filter( 'pre_get_users', 'filter_users_by_custom_section' );
    
    
    
    /**
     * Get the user result by church filter.
     *
     * @param  obj $query
     * @return void
     */
    function filter_users_by_custom_section( $query ) {
    
    	// Get filter submit button.
    	$which = key( array_filter( $_GET, function( $v ) { return __( 'Filter' ) === $v; } ) );
    
    	// Get selected church from filter.
    	$selected_option = ( isset( $_GET['custom_select_' . $which ] ) ) ?
    		               $_GET['custom_select_' . $which ] :
    		               '';
    
    	// If anuthing goes wrong. stop.
    	if ( empty( $selected_option ) ) {
    		return;
    	}
    
    	global $pagenow, $wpdb;
    
    	// Get user ids of selected church.
    	if ( is_admin() && 'users.php' == $pagenow ) {
    
    		// MAKE YOUR OWN QUERY TO GET ARRAY OF USER IDs
    		$filtered_users = $wpdb->get_col(
    			$wpdb->prepare(
    				"SELECT DISTINCT user_id FROM TABLE_NAME WHERE value = %s",
    				$selected_option
    			)
    		);
    
    		// Set query vars.
    		// This will filter your filtered user into users main query
    		if ( ! empty( $filtered_users ) ) {
    
    			$query->set( 'include', $filtered_users );
    
    		}
    
    	}
    
    }

    In above function, following are the important part

    1. Getting the value of “$which”. This will match the value of the same variable defined in part 1.
    2. Build your query by which you can get your filtered user ids array where I’ve commented “// MAKE YOUR OWN QUERY TO GET ARRAY OF USER IDs”
    3. And set “include” for user query and the value will be the array of user ids which will get by your custom query according to your needs.

     

    The same thing will be added for posts as well. The only difference will be the actions. Other everything will remain the same.

    Following are the actions:

     

    1. restrict_manage_posts : For creating select dropdown
    2. parse_query : For modifying the post query