WordPress로 URL에 추가 변수를 전달하는 방법
내 WordPress 설치에 URL에 추가 변수를 전달하는 데 문제가 있습니다.
예를 들면 /news?c=123
어떤 이유로 웹 사이트 루트에서만 작동 www.example.com?c=123
하지만 URL에 더 많은 정보가 포함 된 경우 작동하지 않습니다 www.example.com/news?c=123
. 테마 디렉토리의 functions.php 파일에 다음 코드가 있습니다.
if (isset($_GET['c']))
{
setcookie("cCookie", $_GET['c']);
}
if (isset($_SERVER['HTTP_REFERER']))
{
setcookie("rCookie", $_SERVER['HTTP_REFERER']);
}
어떤 아이디어?
이 문제를 해결할 수있는 솔루션은 거의 없습니다. 먼저 원하는 경우 플러그인을 사용할 수 있습니다.
또는 수동으로 코딩하려면 다음 게시물을 확인하십시오.
또한 확인하십시오 :
"프론트 엔드"(의 컨텍스트에서는 작동하지 않음)에서 "The WordPress Way"를 왕복 wp-admin
하려면 다음 3 가지 WordPress 기능을 사용해야합니다.
- add_query_arg () -새 쿼리 변수를 사용하여 URL을 만듭니다 (예제에서는 'c').
- query_vars의 필터 - 목록 수정하는 공용 조회 변수 (이것은 단지 프런트 엔드에서 작동 WP 쿼리가 백 엔드에 사용되지 않기 때문에, -에 대한 워드 프레스가 알고있는이
wp-admin
-이 또한 로모되지 않도록admin-ajax
) - get_query_var ()-URL에 전달 된 사용자 지정 쿼리 변수의 값을 검색합니다.
참고 : 이렇게하면 슈퍼 글로벌 ( $_GET
)을 만질 필요가 없습니다 .
예
링크를 생성하고 쿼리 변수를 설정해야하는 페이지에서 :
이 페이지로 돌아가는 링크 인 경우 쿼리 변수 만 추가하면됩니다.
<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c ) )?>">
다른 페이지에 대한 링크 인 경우
<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c, site_url( '/some_other_page/' ) ) )?>">
functions.php 또는 일부 플러그인 파일 또는 사용자 정의 클래스 (프런트 엔드에만 해당) :
function add_custom_query_var( $vars ){
$vars[] = "c";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' );
URL에 설정된 쿼리 변수를 검색하고 작업하려는 페이지 / 함수에서 :
$my_c = get_query_var( 'c' );
백엔드 ( wp-admin
)
백엔드 wp()
에서는 실행하지 않으므로 기본 WP 쿼리가 실행되지 않습니다. 결과적으로 없음 query vars
및 query_vars
후크가 실행되지 않습니다.
이 경우 $_GET
슈퍼 글로벌 을 검사하는보다 표준적인 접근 방식으로 되돌려 야합니다 . 이를 수행하는 가장 좋은 방법은 다음과 같습니다.
$my_c = filter_input( INPUT_GET, "c", FILTER_SANITIZE_STRING );
꼬집음에 당신은 시도되고 진실 할 수 있지만
$my_c = isset( $_GET['c'] ? $_GET['c'] : "";
또는 그 일부 변형.
function.php에 다음 코드를 추가하십시오.
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'var1'; // var1 is the name of variable you want to add
return $vars;
}
그러면 $ _GET [ 'var1']을 사용할 수 있습니다.
이것은 자주 방문하는 게시물이기 때문에 누군가에게 도움이 될 경우 내 솔루션을 게시하려고 생각했습니다. WordPress에서 쿼리 변수를 사용하여 다음과 같이 영구 링크도 변경할 수 있습니다.
www.example.com?c=123 to www.example.com/c/123
이를 위해 functions.php 또는 플러그인 기본 파일에 이러한 코드 줄을 추가해야합니다.
에서 shankhan의 anwer
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'c'; // c is the name of variable you want to add
return $vars;
}
또한 이것은 사용자 지정 재 작성 규칙을 추가하기 위해 차단되었습니다.
function custom_rewrite_basic()
{
add_rewrite_rule('^c/([0-9]+)/?', '?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
특정 페이지에 대한 재 작성 규칙을 추가해야하는 경우 해당 페이지 슬러그를 사용하여 해당 특정 페이지에 대한 재 작성 규칙을 작성할 수 있습니다. OP가 묻는 질문 에서처럼
www.example.com/news?c=123 to www.example.com/news/123
이전 함수에 약간의 수정을 추가하여 원하는 동작으로 변경할 수 있습니다.
function custom_rewrite_basic()
{
add_rewrite_rule('^news/([0-9]+)/?', 'news?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
누군가에게 유용하기를 바랍니다.
<?php
$edit_post = add_query_arg('c', '123', 'news' );
?>
<a href="<?php echo $edit_post; ?>">Go to New page</a>
"뉴스"대신 페이지를 추가 할 수 있습니다.
One issue you might run into is is_home()
returns true when a registered query_var is present in the home URL. For example, if http://example.com
displays a static page instead of the blog, http://example.com/?c=123
will return the blog.
See https://core.trac.wordpress.org/ticket/25143 and https://wordpress.org/support/topic/adding-query-var-makes-front-page-missing/ for more info on this.
What you can do (if you're not attempting to affect the query) is use add_rewrite_endpoint()
. It should be run during the init
action as it affects the rewrite rules. Eg.
add_action( 'init', 'add_custom_setcookie_rewrite_endpoints' );
function add_custom_setcookie_rewrite_endpoints() {
//add ?c=123 endpoint with
//EP_ALL so endpoint is present across all places
//no effect on the query vars
add_rewrite_endpoint( 'c', EP_ALL, $query_vars = false );
}
This should give you access to $_GET['c']
when the url contains more information like www.example.com/news?c=123
.
Remember to flush your rewrite rules after adding/modifying this.
This was the only way I could get this to work
add_action('init','add_query_args');
function add_query_args()
{
add_query_arg( 'var1', 'val1' );
}
http://codex.wordpress.org/Function_Reference/add_query_arg
to add parameter to post urls (to perma-links), i use this:
add_filter( 'post_type_link', 'append_query_string', 10, 2 );
function append_query_string( $url, $post )
{
return add_query_arg('my_pid',$post->ID, $url);
}
output:
http://yoursite.com/pagename?my_pid=12345678
참고URL : https://stackoverflow.com/questions/4586835/how-to-pass-extra-variables-in-url-with-wordpress
'Development Tip' 카테고리의 다른 글
레일은 특정 마이그레이션을 실행합니다. (0) | 2020.11.16 |
---|---|
Proper use of mutexes in Python (0) | 2020.11.16 |
부트 스트랩 : 더 넓은 입력 필드 (0) | 2020.11.16 |
"단품 다형성"이란 무엇이며 어떻게 이점을 얻을 수 있습니까? (0) | 2020.11.15 |
모든 브라우저에 대한 기본 시간 초과 설정은 어디에서 찾을 수 있습니까? (0) | 2020.11.15 |