2014-09-26 2 views
4

전환 버튼을 사용하여 단축 코드를 실행하고 싶습니다. 스위치가 "ON"이면 단축 코드를 호출하고 "OFF"이면 다른 코드를 호출합니다.AJAX로 워드 프레스 단축 코드로 전화하십시오.

파일 "페이지 recherche.php":

<a href="" id="clicklien">CLICK HERE</a> 


<script> 
$("#clicklien").click(function(e){ 
     e.preventDefault(); 
    $.ajax({ 
    url: 'http://www.capitainebar.com/wp-content/themes/Capitaine-Bar/shortcode-recherche.php', 
    success: function (data) { 
     // this is executed when ajax call finished well 
     console.log('content of the executed page: ' + data); 
     $('body').append(data); 

    }, 
    error: function (xhr, status, error) { 
     // executed if something went wrong during call 
     if (xhr.status > 0) alert('got error: ' + status); // status 0 - when load is interrupted 
    } 
}); 

}); 
</script 

파일을 호출 내가 AJAX와 하나의 링크를 클릭에서 단축 코드를 호출 시도 테스트로

, 그것은 나에게이 있습니다 "shortcode-recherche.php"입니다.

<?php echo do_shortcode('[search-form id="1" showall="1"]'); ?> 

결과는 치명적인 오류입니다. 마치 코드가 "page-recherche.php"보다는 "shortcode-recherche.php"에서 실행되는 것처럼 말입니다.

단축 코드는 AJAX 호출없이 내 페이지에 직접 쓰면 잘 작동합니다. 직접, 워드 프레스가 포함되지 않은 PHP 파일을 호출 할 때

당신은 the result here

+0

수신하는 오류를 게시 할 수 있습니까? – Dez

+0

치명적인 오류 : /homepages/1/d543902707/htdocs/wp-content/themes/Capitaine-Bar/shortcode-recherche.php 온라인 1 –

답변

2

볼 수 있습니다. 이것은 do_shortcode()과 같은 기능이 존재하지 않는다는 것을 의미합니다.

대신 WordPress에 걸린 파일을 요청해야합니다 (일반적으로 404 일지라도). 그런 다음 URL을 플러그인에 알립니다. 쿼리 변수 (쉽게) 또는 규칙 다시 작성 (어렵거나 더 예뻐)을 사용하여이 작업을 수행 할 수 있습니다.

쿼리 변수 : example.org/?custom_shortcode=gallery

Rerwrite 규칙 : example.org/custom_shortcode/gallery/ 당신이 선택하든 옵션


, 플러그인이이 URL에 액세스를 차단하는 경우 인식 할 필요가있다 예를 들어. 끝나면 WordPress에서 404 페이지를 표시하지 못하도록 스크립트를 종료해야합니다.

다음은 functions.php 파일에 간단히 넣을 수있는 예입니다. 당신이 당신의 사이트를 방문하여이 문제를 테스트 할 수 있습니다

function shortcode_test() { 
    if (!empty($_REQUEST['shortcode'])) { 
    // Try and sanitize your shortcode to prevent possible exploits. Users typically can't call shortcodes directly. 
    $shortcode_name = esc_attr($_REQUEST['shortcode']); 

    // Wrap the shortcode in tags. You might also want to add arguments here. 
    $full_shortcode = sprintf('[%s]', $shortcode_name); 

    // Perform the shortcode 
    echo do_shortcode($full_shortcode); 

    // Stop the script before WordPress tries to display a template file. 
    exit; 
    } 
} 
add_action('init', 'shortcode_test'); 

는 URL의 끝에 추가 :

?shortcode=gallery

이 HTML로 확장 된 갤러리 단축 코드를 표시해야합니다. 이 작업이 완료되면 기존 AJAX 함수에 연결하면됩니다.

+0

에서 정의되지 않은 함수 do_shortcode()를 호출합니다. 당신의 도움을 위해 많이! –

+0

'? shortcode = search-form'을 사용하면/homepages/1/d543902707/htdocs/wp-content/plugins/profi-search-filter/includes/shortcode에서이 경고 : 잘못된 문자열 오프셋 'id'를 반환합니다. PHP는 온라인 4'. 내가 준 URL에 내 shortcode의 ID를 어떻게 지정합니까? –

+0

안녕하세요, 제발 내 문제를 살펴주십시오 : http://stackoverflow.com/questions/41248786/wordpress-execute-do-shortcode-inside-ajax-function –

관련 문제