Prevent website content from being crawled by search crawlers through Ajax

WordPress four six hundred and two Reading mode

For example, if part of the content of the website only wants to be seen by the viewer, but does not want to be captured by the search crawler, the page content can be dynamically loaded through Ajax instead of being directly displayed in HTML. The hidden content can not be seen in the page source code. The search crawler will not normally recognize the content loaded by Ajax, and naturally can not obtain it. Here are the specific methods.

Add JS file

Add a JS file named my script in the theme directory js directory

 /js/my-script.js

Add:

 jQuery(document).ready(function($) { var data = { 'action': 'zm_action', 'data': 'zm_data' }; $.post(zm_script.ajax_url, data, function(response) { $('.zm-content').html(response); }); });
Load JS file

Add the following code to the theme function template functions.php file

 //Load JS file function load_zm_script() { wp_enqueue_script( 'my-script', get_template_directory_uri() . '/ js/my-script.js', array('jquery'), '1.0', true ); wp_localize_script( 'my-script', 'zm_script', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )); } add_action( 'wp_enqueue_scripts', 'load_zm_script' );
Add Ajax action function

Add the following code to the theme function template functions.php file

 //Add ajax action add_action('wp_ajax_zm_action', 'zm_action_callback'); add_action('wp_ajax_nopriv_zm_action', 'zm_action_callback');  function zm_action_callback() { Echo 'The content you don't want to be crawled by the search crawler'; wp_die(); }

Modify the text in it.

Front end call

Finally, add the

 <div class="zm-content"></div>

Because the text content is loaded through Ajax, the search crawler cannot crawl it, and it can only be seen when the web page is opened normally.


The above Ajax action function only loads a paragraph of text, and you can also call WordPress articles, for example, calling 5 articles randomly:

 //Add a random call to 5 articles Ajax action function zm_action_callback() { echo '<ul>'; $args = array( 'orderby' => 'rand', 'posts_per_page' => 5 ); $query = new WP_Query( $args ); while ( $query->have_posts() ) : $query->the_post(); echo '<li>'; echo '<a href="'; echo the_permalink(); echo '">'; echo the_title(); echo '</a>'; echo '</li>'; endwhile; wp_reset_postdata(); echo '</ul>'; wp_die(); }

The above random article calls will not be statically cached, and the articles will be displayed randomly every time they are refreshed.

 Prevent website content from being crawled by search crawlers through Ajax

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments four    Visitors three    Author one
    •  All
      All four

      Is password protection another alternative?
      Open the password. Anyone who wants to see it can enter the password to see that part of the text.
      The reason why I think so is that sometimes the failure of jquery will hinder page loading, so I don't want to reference jquery.

      •  Baa Ticket Movie
        Baa Ticket Movie two

        Those who rely on search should not do this

        •  Blue Lotus
          Blue Lotus three

          The above random article calls will not be statically cached. How can this be achieved???

          I have installed a cache plug-in, and the latest articles can't be excluded

         anonymous

        Comment

        Anonymous netizens
         :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

        Drag the slider to complete validation