WordPress 개발 중 짧은 코드의 실현 및 관련 함수 사용 기교

3015 단어
사실 짧은 코드를 실현하는 것은 매우 간단하다. 우리는WordPress 안의 한 함수만 사용하면 짧은 코드를 해결할 수 있다. 여기에 자신의 작은 함수를 더하면 짧은 코드를 쉽게 실현할 수 있다.
짧은 코드의 실현 원리는 WP의 일부 동작에 갈고리와 필터 함수를 넣는 것과 같다. 짧은 코드는 단지 봉인된 문장의 출력 내용에 대한 필터를 거쳤을 뿐 일부 주제 기능이 말한 것처럼 그렇게 충격적이고 깊이가 없다.다음은 간단한 예입니다.

function myName() {//         
return "My name's XiangZi !";
}
//     
//xz       
//          [xz]     myName   
add_shortcode('xz', 'myName');

그러면 저희가 문장에 [xz]를 입력하면 얻을 수 있어요.

My name's XiangZi !


짧은 코드의 전삼을 좀 더 깊이 있게 이용하면 뒷글에서 말하겠습니다. 오늘은 짧은 코드의 전삼 메커니즘이 좀 고급스러운 예만 말씀드리겠습니다.

function myName($array,$content) {
var_dump($array);
var_dump($content);
}
 
add_shortcode('xz', 'myName');

기사를 편집할 때 다음을 입력합니다.

[xz a="1" b="2" c="3"]        [/xz]

함수에서 우리는 다음과 같은 것을 얻을 수 있다.

//$array      ,
//      
$array = array('a'=>'1','b'=>'2','c'=>'3');
//$content       
$content = '        ';


shortcode_atts는 코드 플러그인을 짧게 하기 때문이 아니라, 나도 이 함수를 사용하지 않을 거야.shortcodeatts 함수는 주로 짧은 코드에서 변수를 캡처하는 초기 값을 설정하는 데 사용됩니다.이것은 매우 실용적인 함수이다. 사실 이 함수의 진정한 역할은 수조에 있다. 왜냐하면 우리가 짧은 코드에서 캡처한 매개 변수는 모두 수조 형식이기 때문이다.
shortcode_tts 함수는 함수 이름에 의심받지 않도록 자세히 설명합니다.WordPress에서는 주로 짧은 코드 매개 변수의 기본값을 설정하는 데 사용됩니다. 만약 우리가 코드를 추출하여 다른 곳에 사용한다면, 이 함수는 기존의 그룹의 기본값을 설정하는 데 도움을 줄 수 있습니다.
shortcode_atts 함수는 이 함수를 사용하면 매우 간단하다.

shortcode_atts(array(
"url" => 'http://PangBu.Com'
), $url)

위 코드는 $url 그룹 키 값을 url의 구성원 기본값으로 설정하는 것을 의미합니다.http://PangBu.Com'다른 곳에서는 쓸모가 많지 않은 것 같지만, 일부 게으름뱅이들에게는 항상 잊어버리거나 그룹의 수치를 설정하는 것이 귀찮을 때 이 함수가 매우 좋다.
shortcode_atts 함수 설명

/**
 * Combine user attributes with known attributes and fill in defaults when needed.
 *
 * The pairs should be considered to be all of the attributes which are
 * supported by the caller and given as a list. The returned attributes will
 * only contain the attributes in the $pairs list.
 *
 * If the $atts list has unsupported attributes, then they will be ignored and
 * removed from the final returned list.
 *
 * @since 2.5
 *
 * @param array $pairs Entire list of supported attributes and their defaults.
 * @param array $atts User defined attributes in shortcode tag.
 * @return array Combined and filtered attribute list.
 */
function shortcode_atts($pairs, $atts) {
 $atts = (array)$atts;
 $out = array();
 foreach($pairs as $name => $default) {
 if ( array_key_exists($name, $atts) )
  $out[$name] = $atts[$name];
 else
  $out[$name] = $default;
 }
 return $out;
}

좋은 웹페이지 즐겨찾기