programing

플러그인으로 WordPress 템플릿을 덮어쓸 수 있는 방법이 있습니까?

iphone6s 2023. 3. 18. 08:22
반응형

플러그인으로 WordPress 템플릿을 덮어쓸 수 있는 방법이 있습니까?

랜딩 페이지를 만들고 싶습니다.플러그인이 일부 GET 또는 POST 요청을 감지한 경우, 플러그인은 워드프레스 테마를 덮어쓰고 자체 요청을 표시해야 합니다.

이런 식으로 작동합니다.

if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
    /* do something to maintain action */
    /* forbid template to display and show plugin's landing page*/
}

WP Codex는 잘 알고 있습니다만, 그것을 할 수 있는 기능이 있는지 기억이 나지 않습니다.물론 검색했지만 아무 결과도 없었어요.

어떤 아이디어라도 미리 주셔서 감사합니다.

갈고리가 필요해요.Codex에는 기재되어 있지 않지만 SO 또는 WordPress StackExchange에서 더 많은 예를 찾을 수 있습니다.

플러그인 파일

<?php
/**
 * Plugin Name: Landing Page Custom Template
 */
add_filter( 'template_include', 'so_13997743_custom_template' );

function so_13997743_custom_template( $template )
{
    if( isset( $_GET['mod']) && 'yes' == $_GET['mod'] )
        $template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';

    return $template;
}

플러그인 폴더의 사용자 지정 템플릿

<?php
/**
 * Custom Plugin Template
 * File: my-custom-page.php
 *
 */

echo get_bloginfo('name');

결과

에서 사이트의 URL을 방문하면 플러그인 템플릿파일이 렌더링 됩니다.다음은 예를 제시하겠습니다. http://example.com/hello-world/?mod=yes.

플러그인 디렉토리 내에 폴더 '/woocommerce/'를 생성해야 합니다.woocommerce 내부에는 이메일 'mail'을 위해 폴더를 추가하고 '/woocommerce/' 내에 필요한 템플릿을 덮어쓸 필요가 있습니다.이 코드를 메인에 복사해서 붙여넣기만 하면 돼플러그인의 php.

<?php
/**
 * Plugin Name: Custom Plugin
 */

function myplugin_plugin_path() {   
  // gets the absolute path to this plugin directory 
  return untrailingslashit( plugin_dir_path( __FILE__ ) ); 
}

add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 ); 
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {

  global $woocommerce;  
  $_template = $template; 
  if ( ! $template_path ) $template_path = $woocommerce->template_url; 
  $plugin_path  = myplugin_plugin_path() . '/woocommerce/'; 
  // Look within passed path within the theme - this is priority 
  $template = locate_template( 
    array( 
      $template_path . $template_name, $template_name 
    ) 
  );

  // Modification: Get the template from this plugin, if it exists 
  if ( ! $template && file_exists( $plugin_path . $template_name ) ) 
    $template = $plugin_path . $template_name;  

  // Use default template 
  if ( ! $template ) 
    $template = $_template; 

  // Return what we found 
  return $template; 
 }
?>

플러그인을 사용하여 참조 템플릿을 재정의합니다.

언급URL : https://stackoverflow.com/questions/13997743/is-there-any-way-to-override-a-wordpress-template-with-a-plugin

반응형