웹사이트 검색

테마 및 플러그인 단축 코드 충돌을 피하는 방법


우리 모두는 이것을 경험했습니다. 정말 훌륭한 프리미엄 테마를 구입하고 놀라운 플러그인(예를 들어 내 플러그인 중 하나!)도 구입하지만 테마와 플러그인을 함께 사용하는 것은 전혀 작동하지 않습니다. 그래서 뭐? 플러그인이나 테마에 버그가 있나요?

대부분 이런 일이 발생하는 경우는 단축 코드를 사용할 때입니다. "평가"를 처리하기 위해 플러그인을 구입했다고 가정해 보겠습니다. 문서를 주의 깊게 읽고 추천을 표시하려면 다음 단축 코드 [추천]을 포함해야 한다는 것을 알고 있습니다. 그러나 그렇게 하면 아무 것도 나타나지 않습니다.

단축 코드가 회원평가로 대체되지 않는 이유는 테마와 플러그인이 정확히 동일한 단축 코드를 정의하기 때문입니다.

플러그인 파일은 테마 파일보다 먼저 로드되므로 덮어쓰기되며 이 경우 완전히 고려되지 않습니다.

그래서 테마 단축 코드를 등록 취소하고 플러그인 단축 코드로 바꾸는 간단한 방법을 보여드리고 싶었습니다.

1단계: 간단한 플러그인 생성

간단한 플러그인을 만들어야 합니다:

<?php
/*
Plugin Name: Avoid Shortcodes conflicts
Plugin URL: http://remicorson.com
Description: A little plugin to avoid conflicts bewteen shortcodes
Version: 1.0
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
Text Domain: rc_asc
Domain Path: languages
*/

2단계: 단축 코드가 존재하는지 확인

이것이 가장 중요한 단계입니다. 단축번호가 있는지 확인하는 곳입니다. WordPress에는 등록된 모든 단축 코드 목록을 저장하는 “$shortcode_tags” 전역 변수가 있습니다. 따라서 이 변수를 살펴보고 우리가 찾고 있는 단축 코드가 해당 변수에 포함되는지 확인하면 됩니다.

/**
 * Check if a shortcode is already registered
 *
 * @since 1.0
 *
 * @param $shortcode string The shortcode slug to test
 *
 * @return void
 */
function rc_asc_shortcode_exists( $shortcode = false ) {
	
	global $shortcode_tags;

	//echo '<pre>'; var_dump($shortcode_tags); echo '</pre>';
 
	if ( ! $shortcode )
		return false;
 
	if ( array_key_exists( $shortcode, $shortcode_tags ) )
		return true;
 
	return false;

}

이것은 찾을 단축 코드 슬러그를 포함하여 보다 구체적인 함수에서 사용할 일반 함수입니다. 이 함수는 단축 코드가 있으면 TRUE를 반환하고, 없으면 FALSE를 반환합니다.

3단계: 단축 코드 제거 및 새 단축 코드 등록

다음 함수는 방금 만든 rc_asc_shortcode_exists()를 사용하는 것입니다. 단순히 단축 코드가 있는지 확인하고, 있으면 대체하고, 아직 등록되지 않은 경우 단축 코드를 추가합니다.

/**
 * Check if a shortcode is already registered and replace it
 *
 * @since 1.0
 *
 * @return void
 */
function rc_asc_replace_shortcode() {

	$shortcode = 'testimonials';
	
	if( rc_asc_shortcode_exists( $shortcode ) ) {
		remove_shortcode( $shortcode );
		add_shortcode( $shortcode, 'my_testimonials_function' );
	} else {
		add_shortcode( $shortcode, 'my_testimonials_function' );
	}
}

4단계: 새로운 단축 코드 기능 정의

당신이 해야 할 일은 “my_testimonials_shortcode()” 함수의 내용을 정의하는 것뿐입니다. 그러면 끝입니다!

/**
 * Creates the new shortcode
 *
 * @since 1.0
 *
 * @return void
 */
function my_testimonials_function() {

	return 'this replaces the previous shortcode!';
}

보시다시피 이전에 선언된 단축 코드가 이제 올바른 단축 코드로 대체되었습니다.