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.
-
AuthorPosts
-
May 6, 2025 at 4:32 pm #30360
pgogy
ParticipantHello
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
PatMay 6, 2025 at 11:37 pm #30361The Assurer
ModeratorIf 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 #30362admin
KeymasterYou 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.
May 9, 2025 at 11:17 am #30366pgogy
ParticipantThanks 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 #30371admin
KeymasterWe 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 #30531admin
KeymasterWe’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:
-
AuthorPosts
- You must be logged in to reply to this topic.