WordPress.org

Ready to get started?Download WordPress

Forums

Routing comments.php to a custom url after the submit button is pressed (3 posts)

  1. design_dolphin
    Member
    Posted 4 years ago #

    I have tried searching, but I don't really know what to search for in this case.

    I have a custom url setup, which is as follows:

    parent category -> custom taxonomy term -> post

    This makes the path look like this:

    http://www.ecodelphinus.com/chronisch-zieken-gehandicapten/wwb/indien-iemand-chronisch-ziek-of-gehandicapt-is-in-de-wwb-is-het-verstandiger-als-die-persoon-gebruik-kan-maken-van-de-ziektewet.html

    Now this works fine, but I noticed that when a comment is submitted the webpage is rerouted to a different path after submission.

    Which is as follows:

    parent category -> child category -> post

    This makes the path look like this:

    http://www.ecodelphinus.com/chronisch-zieken-gehandicapten/beleid/indien-iemand-chronisch-ziek-of-gehandicapt-is-in-de-wwb-is-het-verstandiger-als-die-persoon-gebruik-kan-maken-van-de-ziektewet.html/comment-page-1#comment-31

    Besides being a different path, this also breaks my breadcrumbs and the comment under review is not shown to the visitor.

    How do I reroute the submit function to a custom url?
    Is there any documentation on this?

    Edit:

    I should note that by design a single post can have different paths:

    parent category A -> taxonomy term 1 -> post A
    parent category A -> taxonomy term 2 -> post A

    So any solution should be able to distinguish from that.

  2. design_dolphin
    Member
    Posted 4 years ago #

    o.k. the relevant bit of code that does the routing appears to be in
    wp-includes/comment-template.php:

    /**
     * Retrieve the link to a given comment.
     *
     * @since 1.5.0
     * @uses $comment
     *
     * @param object|string|int $comment Comment to retrieve.
     * @param array $args Optional args.
     * @return string The permalink to the given comment.
     */
    function get_comment_link( $comment = null, $args = array() ) {
    	global $wp_rewrite, $in_comment_loop;
    
    	$comment = get_comment($comment);
    
    	// Backwards compat
    	if ( !is_array($args) ) {
    		$page = $args;
    		$args = array();
    		$args['page'] = $page;
    	}
    
    	$defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
    	$args = wp_parse_args( $args, $defaults );
    
    	if ( '' === $args['per_page'] && get_option('page_comments') )
    		$args['per_page'] = get_option('comments_per_page');
    
    	if ( empty($args['per_page']) ) {
    		$args['per_page'] = 0;
    		$args['page'] = 0;
    	}
    
    	if ( $args['per_page'] ) {
    		if ( '' == $args['page'] )
    			$args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
    
    		if ( $wp_rewrite->using_permalinks() )
    			$link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' );
    		else
    			$link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
    	} else {
    		$link = get_permalink( $comment->comment_post_ID );
    	}
    
    	return apply_filters( 'get_comment_link', $link . '#comment-' . $comment->comment_ID, $comment, $args );
    }

    and I could possibly use for example something like:

    <?php
     $cust_url_route = 'http://your-domain.com' . $_SERVER['REQUEST_URI'];
    ?>

    in comments.php to fetch the initial URI of the post. Then have the function route to that url.

    Now if I can just put all that somehow into functions.php then it should work. Would require me to figure out how to pass the $cust_url_route variable to the comment-template.php piece of code in functions.php though. Right.

  3. design_dolphin
    Member
    Posted 4 years ago #

    Besides being not the proper the way to do this as it hacks core code, is this a safe way to do it?

    In the form in comments.php:

    <?php $cust_url_route  = 'http://domain.com' . $_SERVER['REQUEST_URI']; ?>
    
    <input type="hidden" name="var" value="<?php echo $cust_url_route?>" />

    In wp-comments-post.php change line 93:

    $location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'] . '#comment-' . $comment_id;

    to:

    $cust_url_route =  $_POST['var'];
    $location = empty($_POST['redirect_to']) ? $cust_url_route  : $_POST['redirect_to'] . '#comment-' . $comment_id;

    It works. It would solve the problem for now.

Topic Closed

This topic has been closed to new replies.

About this Topic