Membership Plugin

WordPress Membership Plugin

  • Home
  • Documentation
  • Addons
  • Support
    • Quick Setup
    • Documentation
    • Premium Addon Support
    • Paid Support
    • Support Forum
    • Support Forum Search
    • Forum Login
    • Forum Registration
  • Contact

[Resolved] Adding protection to a post created programmatically

Simple Membership Plugin › Forums › Simple Membership Plugin › Adding protection to a post created programmatically

  • This topic has 5 replies, 3 voices, and was last updated 1 year, 1 month ago by admin.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • May 6, 2025 at 4:32 pm #30360
    pgogy
    Participant

    Hello
    I am writing a little plugin to save all member emails as posts for future access.
    Is it possible to restrict a post created with wp_insert_post?
    It looks like I could unserialize the value in the DB table, add my post id and then re serialize it, but I was hoping there might be a safer way with existing functions.
    Thanks in advance
    Pat

    May 6, 2025 at 11:37 pm #30361
    The Assurer
    Moderator

    If you are able to assign your posts to a specific category, you can use Category Protection:
    https://simple-membership-plugin.com/use-category-protection-membership-site/

    May 7, 2025 at 5:04 am #30362
    admin
    Keymaster

    You can also review the save_postdata function, which handles saving the protection data, to understand how our plugin processes post or page updates. This can serve as a reference if you wish to create your own function for applying protection settings when programmatically creating posts.

    https://github.com/wp-insider/simple-membership/blob/ad0c24100ae22aed1ff3936cf845b43c3b643c26/simple-membership/classes/class.simple-wp-membership.php#L755

    May 9, 2025 at 11:17 am #30366
    pgogy
    Participant

    Thanks both, I had spent a long time in that file before posting a request for help. I wasn’t sure how I could avoid erasing the existing data, but I re-read and noticed that the line

    \SwpmPermission::get_instance( 2 )->apply( array( $post_id ), 'post' )->save();

    Must have done it, so posting it here it helps other people.

    Note this code is using PSR-4 (the slash)
    This is using default members only access (the 2 after get instance).
    Post_id is the id of the post i’ve made and ‘post’ is the post type which I have fixed as it’s always a post for members only.

    Thanks again

    May 10, 2025 at 3:41 am #30371
    admin
    Keymaster

    We will try to add a new function for this that can be used to apply protection to a post programmatically.

    May 31, 2025 at 11:36 am #30531
    admin
    Keymaster

    We’ve added a couple of new functions in the latest version of the plugin to help you apply content protection programmatically. You can find details about these functions in the following section of the documentation:

    https://simple-membership-plugin.com/simple-membership-miscellaneous-php-tweaks/#content-protection-related

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Log In

Please read this message before using our plugin.

Search

Featured Addons and Extensions

  • Membership Form Builder Addon
  • Member Directory Listing Addon
  • WooCommerce Payment Integration
  • Member Data Exporter Addon

Documentation

  • Documentation Index Page

Copyright © 2026 | Simple Membership Plugin | Privacy Policy