• Resolved Nick Lewis

    (@nickylew)


    Hi there,

    Came across your plugin today in some research. It looks really powerful!

    I was wondering if it could be used alongside the WordPress core Password Protected form (when a user password protects a page or post).

    I am looking to edit the core form to include two custom inputs (for name and email) as the client wants to be able to collect data about who is using the password protected form.

    The notification would only go to the admin, or one specific email address when the password protected form has been successfully used.

    But, I’m not really sure how to achieve this?
    As the re isn’t clear filters/functions for the password protected form and if it has been authenticated (I can’t seem to find that anyway)

    So, I’m wondering if that would be possible with your plugin, and looking for some basic guidance on how to create that using your plugins built in functionality.

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Kuba Mikita

    (@kubitomakita)

    Hey, I replied on the chat, but here’s the transcription:

    Yeah, I think that would be possible! You’ll need to register the custom trigger first, see: https://docs.bracketspace.com/docs/registering-custom-triggers/

    The authentication is processed here: https://github.com/WordPress/WordPress/blob/master/wp-login.php#L492 but I don’t see there any action

    Ok, I found this: https://github.com/WordPress/WordPress/blob/0caf5278c64ed773bb94ad717684e309f220fdbc/wp-includes/post-template.php#L856
    You’ll have to make a “proxy” by hooking into this filter, checking if $required == false && ! empty( $post->post_password )

    If this conditions met, just do do_action( 'whatever_handle' ) and this will be your action for the custom trigger.

    Thread Starter Nick Lewis

    (@nickylew)

    Excellent, thanks for your help on the chat!

    Thread Starter Nick Lewis

    (@nickylew)

    Hi again @kubitomakita

    I am currently trying to work this out and write my custom notification for this password protected form.

    I have declared my custom trigger, and got that showing up – which is meant to be able to send an email straight away isn’t it? (albeit missing the custom merge tags)

    I don’t get any email at all..

    My code that I am using is:

    if ( isset( $_POST['post_Submit'] ) ) {
    
    		//Add our action ready for our setting up notification
    		add_action( 'admin_post_property_password_form', 'property_password_form_handler' );
    		add_action( 'admin_post_nopriv_property_password_form', 'property_password_form_handler' );
    
    		function property_password_form_handler() {
    			//Set up our action getting the form fields content
    			do_action( 'property_password_sent', $_POST['post_Name'], $_POST['post_Email'], $_POST['post_Course'] );
    
    			exit;
    		}
    	}
    
    	//Decalare our Trigger with the notification plugin
    	class PropertyPasswordForm extends \BracketSpace\Notification\Abstracts\Trigger {
    
    		public function __construct() {
    
    			// Add slug and the title.
    			parent::__construct(
    				'propertypasswordsent',
    				__( 'Property password form sent', 'propertypasswordsent' )
    			);
    
    			// Hook to the action.
    			$this->add_action( 'property_password_sent', 10, 2 );
    
    		}
    
    		public function merge_tags() {}
    
    	}
    
    	//and now register our Trigger
    	register_trigger( new PropertyPasswordForm() );

    The original code we talked about didn’t seem to work correctly – even with commenting out the action this filter just made the password form break and automatically resolve as entered so the content for the page shows up straight away.

    add_filter( 'post_password_required', function( $required, $post ) {
    
    		if ( $required == false && ! empty( $post->post_password ) ) {
    			do_action( 'whatever_handle', $post );
    		}
    
    	}, 10, 2 );

    Secondly.. I’m not sure how to create my merge tags from the input fields.
    I am following your (very handy) tutorial on Smashing Magazine, where your merge tags use content from the post, like the title etc. But I’m really not sure how I can get the content of my do_action where I state the form field inputs and use them in the merge tag.

    Do you have any advice at all?

    I know this is custom work, but I’d be super grateful for any pointers!

    Thanks!

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Hey!

    Sorry, my bad with the filter, I forgot the return statement… It should look like this:

    add_filter( 'post_password_required', function( $required, $post ) {
    
    	if ( $required == false && ! empty( $post->post_password ) ) {
    		do_action( 'whatever_handle', $post );
    	}
    
    	return $required;
    
    }, 10, 2 );

    Regarding your code, I’m not sure this action is even executed. Assuming that you really are handling this with admin-post.php it should rather be:

    add_action( 'admin_post_property_password_form', 'property_password_form_handler' );
    add_action( 'admin_post_nopriv_property_password_form', 'property_password_form_handler' );
    
    function property_password_form_handler() {
    
    	if ( isset( $_POST['post_Submit'] ) ) {
    		//Set up our action getting the form fields content
    		do_action( 'property_password_sent', $_POST['post_Name'], $_POST['post_Email'], $_POST['post_Course'] );
    	}	
    }

    Now, to your merge tags 🙂 It couldn’t be simpler, see:

    class PropertyPasswordForm extends \BracketSpace\Notification\Abstracts\Trigger {
    
    	public function __construct() {
    
    		// Add slug and the title.
    		parent::__construct(
    			'propertypasswordsent',
    			__( 'Property password form sent', 'propertypasswordsent' )
    		);
    
    		// Hook to the action.
    		$this->add_action( 'property_password_sent', 10, 2 );
    
    	}
    
    	// You have to handle your action with the params you are passing.
    	public function action( $name, $email ) {
    
    		// Assign all the needed variables from your action to the class as its properties.	
    		$this->name  = $name;
    		$this->email = $email;
    	
    	}
    
    	// Use the properties to create merge tags.
    	public function merge_tags() {
    
    		$this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\StringTag( array(
    	        'slug'        => 'name',
    	        'name'        => 'Name',
    	        'resolver'    => function( $trigger ) {
    		        // $trigger is the trigger class instance, so you can access all the properties you assigned in the action method.
    	            return $trigger->name;
    	        },
    	    ) ) );
    
    	}
    
    }

    That should clarify it!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Using with WordPress Password Protected form’ is closed to new replies.