Greetings.
I just managed to implement a email search feature, in a light and very user friendly manner, without altering the Sendpress Plugin.
Maybe Josh can use it, if it is to his liking. It’s easy to change this code to also allow search by names.
So here we go.
This changes were made in a custom theme. You just need to edit your functions.php file and add a admin.js file (or use an existing one).
your-theme-directory/functions.php
//simple check to save cpu cycles
if($_REQUEST['page']=='sp-subscribers' && ($_REQUEST['view']=='subscribers' || $_REQUEST['view']=='subscriber') ) {
add_action( 'admin_print_styles', 'my_theme_admin_print_styles' );
add_action( 'admin_print_scripts', 'my_theme_admin_print_scripts' );
function my_theme_admin_print_styles() {
//you can get css file and images at https://jqueryui.com/download/
wp_enqueue_style('jquery-ui-lightness', get_template_directory_uri().'/ui-lightness/jquery-ui-1.10.3.custom.min.css' );
}
function my_theme_admin_print_scripts() {
wp_enqueue_script('jquery-ui-autocomplete');
wp_enqueue_script('my-theme-admin-script', get_template_directory_uri().'/admin.js');
}
}
if($_REQUEST['action']=='generic_ajax') {
add_action('wp_ajax_generic_ajax', 'my_theme_generic_ajax');
function my_theme_generic_ajax() {
global $wpdb;
if( $_GET['ajax_do'] == 'sp_subscriber_search' && strlen($_GET['term']) > 2) {
$sql = "SELECT t1.email AS label, t1.subscriberID AS value
FROM ".$wpdb->prefix."sendpress_subscribers AS t1
RIGHT JOIN ".$wpdb->prefix."sendpress_list_subscribers AS t2 ON (t1.subscriberID = t2.subscriberID AND t2.listID = ".(int)$_GET['listID'].")
WHERE t1.email LIKE '%".$wpdb->escape($_GET['term'])."%'
ORDER BY t1.email";
die(json_encode($wpdb->get_results($sql)));
}
die();
}
}
your-theme-directory/admin.js
jQuery(document).ready(function($) {
//if there is a hidden input for listID, it means we are on a valid search page
$listID = $('input[type="hidden"][name="listID"]').val();
if( $listID ) {
//add search input to subscribers pages
$html = '<form><input type="search" id="sp-search-by-email-input" name="s" value="" /><label> (search by email)</label></form>';
$('#taskbar h2').after($html);
$( "#sp-search-by-email-input" ).autocomplete({
source: ajaxurl+'?action=generic_ajax&ajax_do=sp_subscriber_search',
minLength: 3,
select: function( event, ui ) {
window.location = '?page=sp-subscribers&view=subscriber&subscriberID='+ui.item.value+'&listID='+$listID;
}
});
//add delete button do edit subscriber page
if($('form#subscriber-edit').length > 0) {
$html = ' <a class="btn" href="?page=sp-subscribers&action=delete-subscriber&subscriberID='+$('input[name="subscriberID"]').val()+'&listID='+$('input[name="listID"]').val()+'">Delete from this List</a>';
$('form#subscriber-edit input[type="submit"]').after($html);
}
}
}
Hope you enjoy this and thank toy for this great plugin.
Gon?alo Peres