Giter Site home page Giter Site logo

helgatheviking / simple-user-listing Goto Github PK

View Code? Open in Web Editor NEW
20.0 4.0 8.0 337 KB

A shortcode for displaying lists of users in a simple, easy-to-customize (uses template parts) fashion

JavaScript 7.99% PHP 92.01%
wordpress shortcode users directory hacktoberfest

simple-user-listing's Introduction

Simple User Listing

Contributors: helgatheviking
Donate link: https://www.paypal.com/fundraiser/charity/1451316
Tags: users, authors
Requires at least: 4.5.0
Tested up to: 6.2.0
Stable tag: 1.9.3
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html

A shortcode for displaying paginated lists of users.

Description

Other plugins output paginated lists of users, but I needed to be able to customize and style this list to fit my theme. So I created this plugin to use templates that can be overridden and customized by theme developers.

To customize any of the templates, copy the file from the plugin's "templates" folder to your themes "simple-user-listing" folder.

Usage

Place this shortcode anywhere you'd like to display a full list of all your blog's users.

[userlist]

By default the plugin will print out the users based on the "Posts per Page" setting under Settings->Reading, but this and many other settings can be changed via the shortcode's parameters.

Support

If after reading the FAQ you still need help, support is handled in the WordPress forums. Please note that support is limited and does not cover any custom implementation of the plugin.

Please report any bugs, errors, warnings, code problems to Github

Installation

  1. Upload the plugin folder to the /wp-content/plugins/ directory
  2. Activate the plugin through the 'Plugins' menu in WordPress
  3. Add the shortcode [userlist] anywhere you wish to display a list of users

Frequently Asked Questions

How Can I Customize the Output?

The whole reason I wrote this was that other similar plugins had too much control over the output. You can style the output anyway you'd like by adding your own template parts to your theme.

Copy the files you wish to modify from the simple-user-listing/templates folder of the plugin and paste them into a simple-user-listing folder in the root of your theme (so my-theme/simple-user-listing). Now you can change the markup any way you please. It will be similar to template parts for post loops, except you will have access to each user's $user object instead of the $post object.

For more details on what is available in the $user object see the Codex reference on WP_User()

Shortcode Paramaters: How Can I Customize the User Query?###

Simple User Listing supports most of the parameters of the WP_User_Query class as parameters for the shortcode. For example you can pass it a role defining which type of users you'd like to list. You can also adjust the number of users displayed per page. Roles must be in lowercase.

[userlist role="author" number="5"]

As of version 1.2 you can now sort the user list by and of the sort parameters supported by WP_User_Query(). For example, the following would list your users based on number of posts written, with the highest first.

[userlist orderby="post_count" order="DESC"]

As of version 1.4 you can now list users by a meta key. Be careful with this as this is not exactly an efficient query.

[userlist meta_key="foo" meta_value="widgets"]

As of version 1.4.2 you can now include and exclude users with a comma separated list of IDs.

[userlist exclude="1,2,3"]

The full list of supported parameters (shown with default value) is:

'query_id' => 'simple_user_listing',
'role' => '',
'include' => '',
'exclude' => '',
'blog_id' => '',
'number' => get_option( 'posts_per_page', 10 ),
'order' => 'ASC',
'orderby' => 'login',
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '=',
'meta_type' => 'CHAR',
'count_total' => true,

How Can I Sort the Users by Last Name?

As of verison 1.5.2 you could simply use the following as your shortcode:

[userlist meta_key="last_name" orderby="meta_value" order="ASC"]

How can I search by a meta field? ex: Last Name

While you could modify the search-author.php template, if you are only searching by one field it isn't really neccessary. You will, however, need to modify the shortcode's arguments for WP_User_Query. You can do that by filtering sul_user_query_args.

Add the following to your theme's functions.php:

/**
 * Place this in your theme's functions.php file
 * Or a site-specific plugin
 *
 */
// Switch the WP_User_Query args to a meta search
function kia_meta_search( $args ){

  // this $_GET is the name field of the custom input in search-author.php
	$search = ( isset($_GET['as']) ) ? sanitize_text_field($_GET['as']) : false ;

	if ( $search ){
		// if your shortcode has a 'role' parameter defined it will be maintained
		// unless you choose to unset the role parameter by uncommenting the following
		//	unset( $args['role'] );
		$args['meta_key'] = 'last_name';
		$args['meta_value'] = $search;
		$args['meta_compare'] = '=';

		// need to unset the original search args
		if( isset( $args['search'] ) ) unset($args['search']);
	}

	return $args;
}
add_filter('sul_user_query_args', 'kia_meta_search');

Now the search will return users that match the entered "last_name". You can adjust as needed or use the meta_query array for more complicated meta queries.

How Can I Search By Display Name?

By default the WordPress search relies on username, though wih the search_columns parameter can be made to search the user's email or ID. Frankly, I think this is weird, but that's how WordPress works.

It is much more useful to search by the user's display name, however this requires some trickery via the pre_user_query hook. Similar to pre_get_posts this is your last chance to change the WP_User_Query query before it is executed. I’ve built in a query_id variable so that you don’t go willy-nilly filtering all user queries which could have some unintended side effects.

// Switch user search from user_login to display_name via query_where 
function kia_search_users_by_display_name( $query ) {

  if ( isset( $query->query_vars['query_id'] ) && $query->query_vars['query_id'] == 'simple_user_listing' ) {
     $query->query_where = str_replace( "user_login", "display_name", $query->query_where );
  }

} 
add_action( 'pre_user_query', 'kia_search_users_by_display_name' ); 

How to create very complex user queries | How to query multiple meta keys

It isn't worth the effort to get the shortcode parameters to handle complex arrays. And in the end it isn't necessary as there are several filters in place to permit you to run a complex query. The key will be using the query_id parameter.

For example you could pass a specific ID via shortcode:

[userlist query_id="my_custom_meta_query"]

And then in your theme's functions.php or a site-specific plugin, you could filter the user query args:

add_filter( 'sul_user_query_args', 'sul_custom_meta_query', 10, 2 );

function sul_custom_meta_query( $args, $query_id ){
    // checking the query ID allows us to only target a specific shortcode
	if( $query_id == 'my_custom_meta_query' ){
			$args['meta_query'] = array(
									'relation' => 'OR',
									array(
										'key'       => 'billing_city',
										'value'     => 'oslo',
										'compare'   => '=',
										'type'      => 'CHAR',
									),
									array(
										'key'       => 'first_name',
										'value'     => 'bobby',
										'compare'   => '=',
										'type'      => 'CHAR',
									)
								);

	}
	return $args;
}

For complex queries, you will want to read the WP Codex reference on WP_User_Query.

Does Simple User Listing work with WP_Pagenavi?

Yes! WP Pagenavi supports pagination for WP_User_Query and I configured the navigation-author.php template to automatically use WP Pagenavi if it is installed and activated.

I can't get the search users to work?

The search form will not work with the default permalinks. Try changing your permalinks to some other structure. The reason is form submits via the GET method and so adding those parameters to the URL seem to clash with the parameters already on the URL from the default permalink setup.

S2 Member Conflicts | The search doesn't respect the shortcode parameters

Likely you are experiencing a conflict with another plugin, specifically one that is filtering pre_user_query to modify all user queries. The S2 Member plugin is a known culprit of this. To disable S2 Member's modifications on all Simple User Listing lists, add the following to your theme's functions.php or to a site-specific plugin. Ensure you are using at least SUL 1.5.3.

function kia_protect_sul_from_s2(){
	remove_action('pre_user_query', 'c_ws_plugin__s2member_users_list::users_list_query');
}
add_action( 'simple_user_listing_before_loop', 'kia_protect_sul_from_s2' );

function kia_restore_s2(){
	add_action('pre_user_query', 'c_ws_plugin__s2member_users_list::users_list_query');
}
add_action( 'simple_user_listing_after_loop', 'kia_restore_s2' );

simple-user-listing's People

Contributors

backcourtdev avatar helgatheviking avatar mindctrl avatar nerdworker avatar teemujonkkari avatar wpprodigy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

simple-user-listing's Issues

Adjusting the number of users displayed per page

I have custom template to display all the users on my WordPress site. I'm using the following function in my template to display all the Authors.

<?php echo do_shortcode("[userlist]"); ?>

This works perfectly, but I would like to display all the Authors without navigating to a second page. I currently have my Reading Settings set to 10 post per page and I have 17 users. I have copied the content-author.php , navigation-author.php and search-author.php to my theme. What is the best way to override the wp_pagenavi to display all my users?

Remove the Search Input code no longer works

Not sure when this occurred; but the functions.php code (listed in the FAQ tab) for removing the Search Input no longer works.

I use the Code Snippets plugin for all functions.php changes.

Could you please update this code?

Thank you very much!

Switch to _n for translation

In content-author.php,

<?php printf( __( '%s posts', 'simple-user-listing'), count_user_posts( $user->ID ) ); ?>

fix so it doesn't read "1 posts."

About user search function

Hi Kathy,

I'm writing this regarding to the forum topic http://wordpress.org/support/topic/about-search-function?replies=2#post-5026619 .

Since, you couldn't regenerate the issue, I re-installed a fresh copy of the WP 3.8 and your plugin. Finally, I found the reason behind of this issue. This issue is generated with the s2member plugin. Since, I'm testing with a localhost project;I hosted a sample application in a free hosting account for your reference. It will be automatically expired after next 2 weeks.

http://simplelisting.cuccfree.org/
Username: admin
Password:simple123

You can generate this issue as this way,

  1. Go to http://simplelisting.cuccfree.org/authors/ and do a search as typing the word "ja" in the search area.
  2. You can see 2 searched results as .
    Antawn Jamison and Jason Chaffetz

But it is wrong. Because Antawn Jamison is not an author, he is a subscriber http://simplelisting.cuccfree.org/subscribers/ .

This is happening only with the search function; not with the page loading time.

When you deactivate the s2member plugin; search function is working without any issue.

But I need to use s2member plugin and your plugin both. If you can do something for this, it will be a great help to me.

Thanks,
Chathuranga.

Block Design Discussion

Current Design: https://www.figma.com/file/H5U237OsRZ5XPY4LBxSiO6/SUL-block?node-id=0%3A1

I've definitely seen some blocks that use the pattern you've got going on in the design, where almost all of the work happens in the main editor screen in a sort of "edit" mode. However, given the type of parameters the User Query has, I think there's a strong argument here to put all the major settings in the Inspector and have the editor simply be the Live Preview of the shortcode.

I also think that there should probably be a separate block for multisite and single installation to start with, just to lower the complexity of the blocks and what they are trying to achieve.

Does the Figma file represent the initial settings you'd like the block to have @helgatheviking?

I'm interpreting these settings as follows:

  • Display all users (default behaviour)
  • Ability to custom select users
  • Ability to exclude users by role

FQDN in Picture-Path

Images from local source are linked als

< img src="http:/fqd.name/wp-content/plugins/user-avatar/user-avatar-pic.php?src=http://fqd.name/wp-content/uploads/avatars/3/picture.jpg&amp;w=90&amp;id=3&amp;random=1406624605" alt="" class="avatar avatar-90 photo user-3-avatar" width="90" height="90" >

using the http-request within the ?src-Parameter fails when using security tools like iThemes Security which allows to rewrite such requests for security-reasons (Cross-Site-Scripting-Attack).

Better would be to use relative paths like

< img src="http:/fqd.name/wp-content/plugins/user-avatar/user-avatar-pic.php?src=/wp-content/uploads/avatars/3/picture.jpg&w=90&id=3&random=1406624605" alt="" class="avatar avatar-90 photo user-3-avatar" width="90" height="90" >

Decoded arrows

In content-author.php, the description says "The Template for displaying No Results."

I imported the Templates directory to my theme, and when I ran Theme Check it said that there were was a problem with arrows in navigation-author.php. Somehow they got de-encoded and show up as ← instead of ←

Search from paged page should reset to first page

Ex: if you are on /directory/page/2 and you run a search, you should be sent back to /directory. The easiest way to do this is to make the form action equal to the permalink. This is what is used to be, but it was removed (I think to support custom $_GET params and maybe use as a widget). How to achieve this without changing the action?

Would want to redirect to page 1 when form is submitted, but not in cases where search results may legit span multiple pages.

Form $_POST ? pre_get_posts?

Sadly, this did not work:

function search_filter( $query ) {
  if ( isset( $_POST['as'] ) ) {
        $query->set( 'paged', 0 );
  }
}
add_action( 'pre_get_posts', 'search_filter' );

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.