رفتن به مطلب

کش مطالب جدید وردپرس برای افزایش سرعت لود سایت با Transient


M.Javad

پست های پیشنهاد شده

سلام سایت خبرخوان اتوماتیک دارم که با وردپرس ساختم و در صفحه اول با استفاده از ابزارک مطالب دسته بندی های مختلف رو قرار دادم.مشکل لودینگ صفحه اصلی سایت خیلی درگیرم کرده و بعد از ارسال یه تاپیک در stackoverflow یه بنده خدایی توضیح جالبی داد

نقل قول

In your case, I suppose, the widget is getting some RSS feeds, thus making http requests to external resources. These requests are slow, the results must be cached to avoid repeating the requests on every page load. You can have different expiration time (even 30 seconds difference) for each instance of the widget to try to prevent them all updating at the same time. Or even better, you should update them in the background, never making a front-end visitor to fetch those feeds.

و گفت که برای این قضیه باید حتما از Transient API استفاده کنم

نقل قول

Transients API is what you should use to make it fast.

حالا منم کلی سرچ زدم یه چیزایی فهمیدم البته نتونستم درست انجامش بدم

چیزی که من تونستم بنویسم و البته فکر کنم درست هم کار نمیکنه چون از کدی که برای transient نوشتم مطمئن نیستم و همچنین دسته بندی همه مطالب رو هم درست نمایش نمیده و درواقع همه مطالب رو توی همه باکس ها نشون میده

<?php 
// Check for transient. If none, then execute WP_Query
if ( false === ( $featured = get_transient( 'foo_featured_posts' ) ) ) {
	
      $featured = new WP_Query(
	   array(
		'cat' => ''.$link1.'',
		'posts_per_page' => 9
	   ));

	// Put the results in a transient. Expire after 12 hours.
	set_transient( 'foo_featured_posts', $featured, 10 * MINUTE_IN_SECONDS );
} ?>
 
<?php if ( $featured->have_posts() ) : ?>
  
   <?php while ( $featured->have_posts() ) : $featured->the_post(); ?>
      <li>
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
   <?php endwhile; ?>
   
   <?php else: ?>
         // no featured posts found
<?php endif; ?>

<?php wp_reset_postdata(); ?>

دقت داشته باشید که $link1 در ابزارک تعریف شده هستش مشکل از این متغیر نیست.مشکل از کد transient هست.اساتید راهنمایی بفرمایید

با تشکر

ویرایش شده توسط M.Javad
لینک به ارسال

به کمک یکی از دوستان تونستم transient رو در ابزارک مطالب اضافه کنم منتها یه مشکلی داره اینکه موقع انتشار مطلب جدید ابزارک بروز نمیشه و در حالت کش باقی میمونه

<?php
// Creating the widget
class wpb_box extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_box',
// Widget name will appear in UI
__('ابزارک اختصاصی باکس مطالب خبرخوان', 'bigtheme'),
// Widget description
array( 'description' => __( 'ابزارک نمایش باکس مطالب از سایت های مختلف توسط آدرس خوراک یا فید سایت', 'bigtheme' ), )
);
add_action('save_post', array( $this, 'delete_query_caches') );
}
// Creating widget front-end
// This is where the action happens
/**
     * Delete transients
     */
    function delete_query_caches( $post_id ){
        if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;

        $categories = wp_get_post_terms( $post_id, 'category' );

        if( $categories && ! is_WP_Error( $categories ) ) {
            foreach( $categories as $cat ) {
                delete_transient('post'.$cat->term_id.'_query');
            }
        }
    }
public function widget( $args, $instance ) {
		$name = apply_filters( 'widget_title', $instance['name'] );
		$category = apply_filters( 'widget_title', $instance['category'] );
		$id = apply_filters( 'widget_title', $instance['id'] );
$link = apply_filters( 'widget_title', $instance['link'] );
		$display = apply_filters( 'widget_title', $instance['display'] );
		$color = apply_filters( 'widget_title', $instance['color'] );
// This is where you run the code and display the output
?>
<div class="col-sm-6 col-xs-12 padding5">
				<div class="article">
				        <div class="title">		
							<h3><a href="<?php echo $link ?>" target="_blank"><?php echo $name ?></a></h3>
							<div class="clear"></div>
							<div class="line"></div>
					</div>
						
                               <?php
if ( false === ( $slider = get_transient( 'post'.$id.'_query' ) ) ) {

	 $slider = new WP_Query(array(
            'post_status' =>'publish',
            'post_type' =>'post',
            'cat' =>''.$id.'',
            'posts_per_page' =>'9'      
        ));

	// Make a new query cache for 1 week
	set_transient( 'post'.$id.'_query', $slider, 10 * HOUR_IN_SECONDS );
	
}
if( !$slider->have_posts() ) return; ?>
<ul>
<?php while( $slider->have_posts() ) : $slider->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
<?php endwhile; $slider->rewind_posts(); ?>
</ul>
<?php wp_reset_postdata(); ?>
<div class="list"><a href="<?php echo $link ?>" target="_blank">مشاهده آرشیو کامل</a><a href="<?php echo $link ?>/feed" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/rss.png" width="18" height="18" alt="خوراک سایت" ></a></div>
			</div>
				</div>
<?php
echo $args['after_widget'];
}
public function form( $instance ) {
		$name 	= ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
		$category 	= ( isset( $instance[ 'category' ] ) ) ? $instance[ 'category' ] : '';
$link 	= ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
		$color 	= ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
		$id 	= ( isset( $instance[ 'id' ] ) ) ? $instance[ 'id' ] : '';
		$display 	= ( isset( $instance[ 'display' ] ) ) ? $instance[ 'display' ] : '';
?>
			<p>
				<label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان باکس:' ); ?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'id' ); ?>"><?php _e( 'آی دی دسته بندی' ); ?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" type="text" value="<?php echo esc_attr( $id ); ?>" />
			</p>
			<select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" class="widefat" style="width:100%;">
            <?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
            <option <?php selected( $instance['category'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
            <?php } ?>      
        </select>
<p>
				<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو' ); ?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
			</p>
			
			<p>
				<label><?php _e( 'نمایش توضیحات مطالب' ); ?></label>
				<select class="widefat" id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>">
					<option <?php selected( $instance['display'], 'block'); ?> value="block">بله</option> 
					<option <?php selected( $instance['display'], 'none'); ?> value="none">خیر</option> 
				</select>
			</p>
			
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
		$instance['category'] = ( ! empty( $new_instance['category'] ) ) ? strip_tags( $new_instance['category'] ) : '';
$instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
		$instance['link2'] = ( ! empty( $new_instance['link2'] ) ) ? strip_tags( $new_instance['link2'] ) : '';
		$instance['id'] = ( ! empty( $new_instance['id'] ) ) ? strip_tags( $new_instance['id'] ) : '';
		$instance['link3'] = ( ! empty( $new_instance['link3'] ) ) ? strip_tags( $new_instance['link3'] ) : '';
		$instance['link4'] = ( ! empty( $new_instance['link4'] ) ) ? strip_tags( $new_instance['link4'] ) : '';
		$instance['link5'] = ( ! empty( $new_instance['link5'] ) ) ? strip_tags( $new_instance['link5'] ) : '';
		$instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
		$instance['display'] = ( ! empty( $new_instance['display'] ) ) ? strip_tags( $new_instance['display'] ) : '';
		$instance['source'] = ( ! empty( $new_instance['source'] ) ) ? strip_tags( $new_instance['source'] ) : '';
		$instance['time'] = ( ! empty( $new_instance['time'] ) ) ? strip_tags( $new_instance['time'] ) : '';
		return $instance;
	}
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( 'wpb_box' );
}
add_action( 'widgets_init', 'wpb_box' );
?>

 

لینک به ارسال

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع ...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...