繁体   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