Skip to content

WP Developing

Learning WP Development My Way

Is it possible to limit the number of frontend submissions to one per user?

How Can We Help?

< Back
You are here:
  • KB Home
  • User Submitted Content
  • Is it possible to limit the number of frontend submissions to one per user?
Print
PostedSeptember 5, 2018
Last Updated OnSeptember 5, 2018
Byphg3a

There is no built-in functionality at this point, but it should be possible using a custom filter function on the user_has_cap hook. The following code (add to your theme’s functions.php file) will count the number of published posts of a certain type by a specific user and remove the permission to add new posts for the specific post type if the user has already published a post.

Note: You need to change the ‘directory_dir_ltg’ part to the post type of your directory listing. The post type name is viewable from:

Directories –> [Your Directory] –> Content Types.

add_filter('user_has_cap', function($allcaps, $cap, $args) {
// Some initializations
$post_type = 'directory_dir_ltg';
$cap_to_check = 'drts_entity_create_' . $post_type;
$limit = 1;

// Bail out if this is not the capability we are looking for
if ($cap[0] !== $cap_to_check) {
    return $allcaps;
}

// Bail out if the current user doesn't have the capability currently
if (!isset($allcaps[$cap_to_check])) {
    return $allcaps;
}

// Bail out if the number of posts by the current user is below the limit
if (count_user_posts($args[1], $post_type) < $limit) {
    return $allcaps;
}

// Remove the capability
unset($allcaps[$cap_to_check]);

return $allcaps;
}, 10, 3);
Tags:
  • page-5
  • user submitted content
Previous Is it possible to integrate a “request” system that notifies all owners of listings in a specific category of the request?
Next Is it possible to not require all required fields to be filled out before saving a draft of a listing?
Table of Contents

Recent Posts

  • Migrating a WordPress Site to SpinupWP the Easy Way
  • When You Get an Invalid Default Value Error While Changing Your MySQL Engine on a Table from MyISAM to InnoDB
  • Headless WordPress
  • Things I’ve Learned Using Flywheel Local
  • Making an Easy Mistake with the gatsby-source-wordpress Plugin

Recent Comments

    Archives

    • November 2020
    • September 2020
    • May 2020
    • April 2020
    • September 2018

    Categories

    • Database
    • Developer Tooling
    • Directories Pro
    • Gatsby
    • Hosting
    • MySQL
    • Plugins
    • SQL
    • Uncategorized
    WP Developing Proudly powered by WordPress