Show images uploaded by current user
-
First of all, thanks a lot for this amazing plugin. Here is something I’d like to have.
I am allowing multiple users (custom role : Participant) to upload images. I want to show each user his own images on a separate page. I’d like to have a shortcode which will fetch current username and display his images.
-
Please respond!
Thanks for your question and for your patience in awaiting a response. I have been traveling and away from my development/test system. I have begun to work on the support topics that were posted during my absence and I will work on this question as soon as I can.
You wrote “I am allowing multiple users (custom role : Participant) to upload images.” That means that your users are logged in to the site so the WordPress
wp_get_current_user()
function will work; is that right? If so, the solution will require a bit of PHP code to retrieve the user ID and use it to filter the Media library items by author. This can be placed in a function that hooks one of the filters provided by[mla_gallery]
and activated by a custom parameter added to that shortcode.If that approach will work for you I will work up the specific code and post it here. Let me know if there are any problems with that approach.
Hi David,
hope you has safe and sound travel!As you correctly said, my users are logged in to the site, so the function will work.
I’m not a developer and may not understand the code part of the solution. It would be highly appreciated if you also tell me where to put the code that you will provide. Thanks a lot!
Thanks for confirming my assumption and for your patience in awaiting my response.
Here is the complete code for a small custom plugin that will give you a solution:
<?php /* Plugin Name: MLA Current User Example Plugin URI: https://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/ Description: Uses the current logged in user to supply an "Author" parameter for the query Author: David Lingren Version: 1.00 Author URI: https://fairtradejudaica.org/our-story/staff/ Copyright 2016 David Lingren This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA */ /** * Class MLA Current User Example uses the current logged in user to supply an "Author" parameter for the query * * @package MLA Current User Example * @since 1.00 */ class MLACurrentUserExample { /** * Initialization function, similar to __construct() * * @since 1.00 */ public static function initialize() { // The filters are only useful for front-end posts/pages; exit if in the admin section if ( is_admin() ) return; add_filter( 'mla_gallery_attributes', 'MLACurrentUserExample::mla_gallery_attributes_filter', 10, 1 ); } /** * MLA Gallery (Display) Attributes * * The $shortcode_attributes array is where you will find any of your own parameters that are coded in the * shortcode, e.g., [mla_gallery random_category="abc"]. * * @since 1.00 * * @param array the shortcode parameters passed in to the shortcode */ public static function mla_gallery_attributes_filter( $shortcode_attributes ) { global $wpdb; // ignore shortcodes without the author parameter set to "current" if ( empty( $shortcode_attributes['author'] ) || ( 'current' !== $shortcode_attributes['author'] ) ) { return $shortcode_attributes; } // ignore shortcodes with no logged in user $current_user = wp_get_current_user(); if ( !( $current_user instanceof WP_User ) || ( 0 == $current_user->ID ) ) { unset ( $shortcode_attributes['author'] ); } else { $shortcode_attributes['author'] = $current_user->ID; } return $shortcode_attributes; } // mla_gallery_attributes_filter } // Class MLACurrentUserExample /* * Install the filters at an early opportunity */ add_action('init', 'MLACurrentUserExample::initialize'); ?>
You can copy the code to a file named
mla-current-user-example.php
and put the file in your site’s/plugins
directory. Then, go to the Plugins/Installed Plugins admin screen and look for the “MLA Current User Example” plugin. Activate the plugin.To use the plugin, add a parameter to your
[mla_gallery]
shortcode;author=current
. You can use any other parameters you need to get the results you want. The plugin will translate thecurrent
value to the ID of the logged in user and filter the gallery by that value.If you don’t feel comfortable creating and installing the custom plugin file I can send you a ZIP archive version you can use with the Plugins/Add New (Add Plugins) admin screen. Once you have the ZIP archive:
- Go to Plugins/Add New.
- Click “Upload Plugin”, to the right of the Add Plugins title.
- “Browse…” to the location of the ZIP Archive and click on it.
- Click “Install Now”, to the right of “Browse…”
- When the install completes, click “Activate Plugin” at the bottom of the screen.
- When the activation completes, scroll down to “MLA Current User Example” and verify that it is activated.
If you’d like the ZIP version, please send me a note with your e-mail address and I will give you a copy by return e-mail. You can use the Contact Us page at our web site:
I hope that get you the solution you need for your application. I am marking this topic resolved, but please update it if you have any problems or further questions regarding the example plugin. Thanks for your interest in MLA.
Hey David,
I’m really really thankful for this custom plugin. It works perfect. Thanks a ton, once again.
- The topic ‘Show images uploaded by current user’ is closed to new replies.