Category: wordpress

  • 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

     

  • WordPress Copy Content Plugin

    WordPress Copy Content Plugin

    Sometimes people needs some part of content from existing posts or pages. Or some needs to add extra content from existing.

    So now you dont need to find that post/page and copy that content and paste. Just install this plugin, search the post/page by it’s title and fetch the content.

    HOW TO USE?

    • Search post/page by it’s title
    • Select from the search result
    • And fetch the content of selected post/pages

     

    You can get the plugin free from here.

  • Comment Mention WordPress Plugin

    Comment Mention WordPress Plugin

    Now you can enable user mention functionality in post comments without using BuddyPress plugin.

    This plugin is useful for those who wanted to enable mention on their blog site.

    What this plugin does? Just type username followed by ‘@’ in comment box. It will send the email to mentioned user.

    Email setting is provided in backend. You can change email subject and content by your own.

    You can get this simple plugin from here.

  • Allow authors to preview the draft post

    In WordPress, only administrator of the site can preview of non-published posts.

    What if you want to allow preview for the author as well?

    Add code snippet from below gist to theme’s functions.php

    https://gist.github.com/BhargavBhandari90/557fd7e1155704e4f268c2016ac536c9

  • Check the largest value of autoload filed in options table

    Check the largest value of autoload filed in options table

    Below query can help you to find which field contains the largest value of autoload field in options table.

    SELECT
    option_name,
    LENGTH(option_value),
    autoload
    FROM
    wp_options
    WHERE
    autoload = 'yes'
    ORDER BY
    `LENGTH(option_value)`
    DESC
    

    NOTE: change your table prefix as per your site.

    WP-CLI:

    wp db query  "SELECT option_name, LENGTH(option_value), autoload FROM wp_options WHERE autoload = 'yes' ORDER BY  LENGTH(option_value) DESC;"