Close Button and return on Post
-
One thing that’s a pain with WP User Frontend is that there’s no Close button which means your always hitting the back button on the browser to return to the original referral page. This may be three pages depending on circumstances.
Also once you posted you go to the new page which also means hitting the back button multiple times to return to the original screen. This problem was discussed here. https://www.ads-software.com/support/topic/plugin-wp-user-frontend-redirecting-after-posting?replies=15
Either way the intuitive expectation is that after the post or if the Close button is pressed the browser should return to the original referring page (updated in the case of a new or edited post).
Ideally a pop up window or overlay using ajax would be the best for new pages as the author wouldn’t have to leave the original page. However in the case of editing content this wouldn’t work directly as the author would expect the original text to be updated. This is possible via ajax techniques but is complex and best left to later. Incidently some editors use inline techniques to do this on the original page eg Raptor, Front End Editor.
In any case for now I’m just going to concentrate on the minimal changes needed to implement a Close button and return to original page on Post.
The following code and considerations refer to the New Post code only in wpuf-add-post.php and the changes will also have to be added to the Edit Post code.
To get the original page the use of the php variable $_SERVER[‘HTTP_REFERER’] does the trick. However this only works for the original page. The two exceptions where this doesn’t work follows.
1. When the Submit button is pressed Frontend posts to itself.
2. If the user isnt logged in and uses the frontend login prompt to login.The Submit problem is easily solved by providing a hidden Input field with the original referral page as follows.
$wpuf_referer = $_SERVER['HTTP_REFERER']; <input type="hidden" name="wpuf_referer" value="<?php echo $wpuf_referer ?>" />
For the login problem two approaches can be considered.
1. Add the referral page as a query variable to the login url and have the script deal with it.
Original Code.
function shortcode( $atts ) { .... if ( is_user_logged_in() ) { $this->post_form( $post_type ); } else { printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( get_permalink(), false ) ); } .... }
Updated code
$wpuf_referer=''; function shortcode( $atts ) { global $wpuf_referer; .... if ( isset( $_POST['wpuf_post_new_submit'] ) ) { $wpuf_referer = $_POST['wpuf_referer']; } else if (isset( $_GET['wpuf_referer'] ) ) { $wpuf_referer = $_GET['wpuf_referer']; } else { $wpuf_referer = $_SERVER['HTTP_REFERER']; } if ( is_user_logged_in() ) { $this->post_form( $post_type ); } else { printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( get_permalink() . '?wp_referer=' . $wpuf_referer , false ) ); } .... }
2. Delete the login option as really this is an error as an edit/new page link shouldn’t be offered to a non-logged in user anyway. Instead check for this as part of the post_form function.
e.g.
Original Code for function shortcode( $atts )
function shortcode( $atts ) { extract( shortcode_atts( array('post_type' => 'post'), $atts ) ); ob_start(); if ( is_user_logged_in() ) { $this->post_form( $post_type ); } else { printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( get_permalink(), false ) ); } $content = ob_get_contents(); ob_end_clean(); return $content; }
Updated Code for function shortcode( $atts )
function shortcode( $atts ) { extract( shortcode_atts( array('post_type' => 'post'), $atts ) ); ob_start(); $this->post_form( $post_type ); $content = ob_get_contents(); ob_end_clean(); return $content; }
Original code for function post_form( $post_type ) {
function post_form( $post_type ) .... $info = __( "Post It!", 'wpuf' ); $can_post = 'yes'; $info = apply_filters( 'wpuf_addpost_notice', $info ); $can_post = apply_filters( 'wpuf_can_post', $can_post ); ..... }
Updated code for function post_form( $post_type ).
Includes edit_posts security fix.
Also puts the info filter after the can_post filter to allow the can_post filter to change the info message.function post_form( $post_type ) { .... if (!is_user_logged_in() ) { $can_post = 'no'; $info = __( "User is not logged in.", 'wpuf' ); } else if (!current_user_can( 'edit_posts' )) { $can_post = 'no'; $info = __( "User doesn't have post capability.", 'wpuf' ); } else { $can_post = 'yes'; $info = __( "Can't post.", 'wpuf' ); //default message } $can_post = apply_filters( 'wpuf_can_post', $can_post ); $info = apply_filters( 'wpuf_addpost_notice', $info ); ..... }
I’m a purist so I’ve implemented the second approach as it makes Frontend more programmer friendly.
Given the above all thats left to do is the Close button and the Post Submit redirect.
For the close button simply add the following code at the end of the post_form function
if ($wpuf_referer) { //Swap the following lines if you don't want to be bothered updating admin/settings-options.php and wpuf-options-value.php //echo '<div id="wpuf-button-close"><a class="wpuf-button" href="' . $wpuf_referer . '">Close</a></div>'; echo '<div id="wpuf-button-close"><a class="wpuf-button" href="' . $wpuf_referer . '">' . esc_attr( wpuf_get_option( 'close_label' ) ) . '</a></div>'; }
The Post Submit redirect requires the following update
function submit_post() { global $userdata; global $wpuf_referer; ... if ( $post_id ) { //$redirect = apply_filters( 'wpuf_after_post_redirect', get_permalink( $post_id ), $post_id ); $redirect = apply_filters( 'wpuf_after_post_redirect', $wpuf_referer, $post_id ); if ($redirect) { wp_redirect( $redirect ); exit; } } ... }
I will post a link to my complete code later which includes the changes outlined in my other recent posts.
Cheers
TheProfessorhttps://www.ads-software.com/extend/plugins/wp-user-frontend/
- The topic ‘Close Button and return on Post’ is closed to new replies.