prosource

워드프레스에서 코멘트 후 참조 페이지로 리다이렉트하려면 어떻게 해야 합니까?

probook 2023. 2. 12. 18:00
반응형

워드프레스에서 코멘트 후 참조 페이지로 리다이렉트하려면 어떻게 해야 합니까?

Wordpress의 다양한 페이지(아카이브, 태그, 검색, 메인 페이지)에 코멘트를 유효하게 하고, 유저가 코멘트를 투고하면, 1개의 투고가 아닌, 자신의 레퍼런스 페이지로 리다이렉트 하고 싶다고 생각하고 있습니다.좋은 생각 있어요?

이것을 당신의 기능에 넣으세요.php:

add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment($location)
{
return $_SERVER["HTTP_REFERER"];
}

WordPress 플러그인 API를 사용합니다.WordPress의 기능을 확장하거나 커스터마이즈하는 적절한 방법입니다.API에 대해 조금 읽으면 Action Reference를 확인해 주세요(링크는 게시하지만 Stack Overflow는 허용하지 않습니다).

작업을 완료하려면 적어도 두 개의 작업 후크가 필요합니다.

  1. comment_post - 코멘트가 데이터베이스에 추가된 후 바로 실행됩니다.
  2. comment_form - 테마 템플릿에서 주석 양식이 인쇄될 때마다 실행됩니다.

기본적으로 사용자가 코멘트폼을 처음 볼 때마다 영속적인 $_SESSION에서 HTTP_REFERER 변수를 캡처합니다.그런 다음 댓글을 달면 리다이렉트합니다.

만들다comment-redirect.phpWordPress에서wp-content/plugins폴더입니다.
이 파일에 넣을 내용에 대한 대략적인 내용은 다음과 같습니다(정리/테스트는 고객님께 맡기겠습니다).

<?php
/*
Plugin Name: Post Comment Redirect
Plugin URI: http://example.com
Description: Redirects you to the previous page after posing a comment
Version: 0.1a
Author: Anonymous
Author URI: http://example.com
License: GPL2
*/

// Run whenever a comment is posted to the database.
// If a previous page url is set, then it is unset and
// the user is redirected.
function post_comment_redirect_action_comment_post() {
  if (isset($_SESSION['PCR_PREVIOUS_PAGE_URL'])) {
    $ref = $_SESSION['PCR_PREVIOUS_PAGE_URL'];
    unset($_SESSION['PCR_PREVIOUS_PAGE_URL']);
    header('Location: '.$ref);
  }
}

// Run whenever comment form is shown.
// If a previous page url is not set, then it is set.
function post_comment_redirect_action_comment_form() {
  if (!isset($_SESSION['PCR_PREVIOUS_PAGE_URL'])) {
    if ($ref = $_SERVER['HTTP_REFERER']) {
      $_SESSION['PCR_PREVIOUS_PAGE_URL'] = $ref;
    }
  }
}

add_action('comment_post', 'post_comment_redirect_action_comment_post');
add_action('comment_form', 'post_comment_redirect_action_comment_form');

플러그인을 저장한 후 wp-admin Plugins 섹션(보통은h**p://your-website-address.com/wp-admin) 근처에 있습니다)에서 플러그인을 활성화합니다.

돌아오지 말 것을 권합니다.$_SERVER["HTTP_REFERER"]믿을 수 없어서요.

사용자 에이전트를 현재 페이지로 참조한 페이지의 주소(있는 경우).이것은 사용자 에이전트에 의해 설정됩니다.모든 사용자 에이전트가 이를 설정하는 것은 아닙니다.또, 기능으로서 HTTP_REFERERER 를 변경하는 기능도 있습니다.한마디로 믿을 수 없다는 것이다.

출처: https://php.net/manual/en/reserved.variables.server.php

여기 대안이 있습니다.

add_filter( 'comment_post_redirect', function ( $location ) {
    return get_permalink($_POST['comment_post_ID']);
} );

실제로 워드프레스에는 (@Timofey의 코멘트에 따라) 더 나은 일관성을 위해 참조자 URL을 처리하는 커스텀 인 함수가 있습니다.

<?php

add_filter( 'comment_post_redirect', 'wpso_4534713' );

function wpso_4534713( $location ) {

    $location = wp_get_referer();
        
    return $location;

}
$ref = $_SERVER["HTTP_REFERER"];
header("Location: $ref");

언급URL : https://stackoverflow.com/questions/4534713/in-wordpress-how-to-redirect-after-a-comment-back-to-the-referring-page

반응형