Advertisement
chrishajer

Set post visibility to password protected

Mar 11th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. <?php
  2. // http://www.gravityhelp.com/forums/topic/password-protect-a-post-published-by-a-gf-form
  3. // this code goes in your current theme's functions.php file
  4. // change the 349 here to your form ID
  5. add_action('gform_after_submission_349', 'set_post_password', 10, 2);
  6. function set_post_password($entry, $form) {
  7.     // see if the field containing the password has a value
  8.     // change the 6 here to the field in your form where you accepted the post password
  9.     if(check_not_empty($entry[6],1)) {
  10.         $post = get_post($entry['post_id']);
  11.         $post->post_password = $entry[6];
  12.         wp_update_post($post);
  13.     }
  14. }
  15. // props for this function: http://www.php.net/manual/en/function.empty.php#77617
  16. function check_not_empty($s, $include_whitespace = false){
  17.     if ($include_whitespace) {
  18.         // make it so strings containing white space are treated as empty too
  19.         $s = trim($s);
  20.     }
  21.     return (isset($s) && strlen($s)); // var is set and not an empty string ''
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement