簡體   English   中英

AJAX 搜索在非管理員用戶上返回“您沒有足夠的權限訪問此頁面”

[英]AJAX Search returns "You do not have sufficient permissions to access this page" on non-admin user

我正在制作這個演示網站: https://theme.artware.gr/

我面臨的問題是搜索功能。 您可以單擊右側/頂部搜索以查看問題。 當我以管理員身份登錄時,搜索工作正常,但是當我以訪客身份查看該頁面時,我收到錯誤:“您沒有足夠的權限訪問此頁面”。

到目前為止,這是我的代碼:

// AJAX Search (no WooCommerce)
if ( !class_exists( 'WooCommerce' ) ) {

  function simple_search_scripts(){
    wp_enqueue_script( 'simple_search', get_stylesheet_directory_uri() . '/js/search.js', array(), '1.0.0', true);
    wp_localize_script('simple_search', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ), 'nonce' => wp_create_nonce('ajax-nonce') ));
  }

  // Create shortcode
  function simple_search(){
    simple_search_scripts(); ?>
    <input type="text" name="keyword" id="keyword" onkeyup="fetch()" placeholder="<?php _e('Αναζήτηση...'); ?>" />
    <div id="datafetch"></div>
    <?php
  }
  add_shortcode('simple_search', 'simple_search');

  // Fetch data
  function simple_search_callback(){
    $the_query = new WP_Query( array( 'posts_per_page' => 10, 's' => esc_attr( $_GET['keyword'] ), 'post_type' => 'post' ) );
    if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post();
      $myquery = esc_attr( $_GET['keyword'] );
      $a = $myquery;
      $search = get_the_title();
      if( stripos("/{$search}/", $a) !== false) { ?>
        <div class="result-sin">
          <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); ?>
          <div class="result-sin-img">
            <?php if ($image) { ?>
            <a href="<?php echo esc_url( post_permalink() ); ?>"><img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" /></a>
            <?php } ?>
          </div>
          <div class="result-sin-tit"><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></div>
          <div class="result-sin-txt"><?php the_excerpt();?></div>
        </div>
      <?php }
    endwhile;
    wp_reset_postdata();
    endif;
    die();
  }
  add_action('wp_ajax_simple_search', 'simple_search_callback' );
  add_action('wp_ajax_nopriv_simple_search', 'simple_search_callback' );

}

和 search.js:

function fetch(){
  jQuery.ajax({
    async: true,
    url: myAjax.ajaxurl,
    type: 'GET',
    data: {
      action: 'simple_search',
      keyword: jQuery('#keyword').val(),
      nonce: myAjax.nonce
    },
    success: function(data) {
      jQuery('#datafetch').html( data );
    }
  });
}

在 header 上,我只需調用一個簡碼[simple_search] 我在 SO 上讀到的所有帖子都說wp_ajax_nopriv_ACTION丟失了。 但我已經有了。

更新我嘗試使用方法 GET,但沒有奏效。 我還使用過: wp_localize_script('simple_search', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ), 'nonce' => wp_create_nonce('ajax-nonce') )); 為了更好地將 admin-ajax.php 排入隊列,這也不起作用。

某處在我的主題中,我有require_once('file.php')有以下行:

if (.current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page;' ) ); }

當我刪除它時,問題就解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM