رفتن به مطلب

خاصیت alt و title اتوماتیک محتوای پست


ilya20

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

سلام و خسته نباشید 

بنده نیاز به کدی دارم که فقط در محتوای پست خاصیت alt و title به صورت اتوماتیک برای تصاویر تعریف کنه (مثلا از عنوان و توضیحات پست برای اینکار استفاده کنه)

 

در صورت امکان راهنمایی کنید

 

سپاس

ویرایش شده توسط ilya20
لینک به ارسال

کد زیر در فایل functions.php قالبتون بعد از

<?php

قرار دهید.

function mehran_add_img_alt_title($content) {
	$dom = new DOMDocument();
	@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
	$xpath = new DOMXpath($dom);
	foreach ($xpath->query('//img') as $node) {
			$node->removeAttribute('alt');
			$node->removeAttribute('title');
			$node->setAttribute('alt', get_the_title());
			$node->setAttribute('title', get_the_title());
	}
	return $dom->saveHTML($dom->documentElement);
}
add_filter('the_content', 'mehran_add_img_alt_title');

کد بالا عنوان پست رو به عنوان alt و title عکس های درون پست ها ست میکنه.

لینک به ارسال
در 4 دقیقه قبل، mehran-b گفته است :

کد زیر در فایل functions.php قالبتون بعد از


<?php

قرار دهید.


function mehran_add_img_alt_title($content) {
	$dom = new DOMDocument();
	@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
	$xpath = new DOMXpath($dom);
	foreach ($xpath->query('//img') as $node) {
			$node->removeAttribute('alt');
			$node->removeAttribute('title');
			$node->setAttribute('alt', get_the_title());
			$node->setAttribute('title', get_the_title());
	}
	return $dom->saveHTML($dom->documentElement);
}
add_filter('the_content', 'mehran_add_img_alt_title');

کد بالا عنوان پست رو به عنوان alt و title عکس های درون پست ها ست میکنه.

 

مشکل حل شد 

 

سپاس از شما دوست عزیز

لینک به ارسال
در 18 دقیقه قبل، mehran-b گفته است :

کد زیر در فایل functions.php قالبتون بعد از


<?php

قرار دهید.


function mehran_add_img_alt_title($content) {
	$dom = new DOMDocument();
	@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
	$xpath = new DOMXpath($dom);
	foreach ($xpath->query('//img') as $node) {
			$node->removeAttribute('alt');
			$node->removeAttribute('title');
			$node->setAttribute('alt', get_the_title());
			$node->setAttribute('title', get_the_title());
	}
	return $dom->saveHTML($dom->documentElement);
}
add_filter('the_content', 'mehran_add_img_alt_title');

کد بالا عنوان پست رو به عنوان alt و title عکس های درون پست ها ست میکنه.

یه مشکل عجیب 

 

وقتی از این دستور استفاده میکنیم اینجوری میشه

vw459e1qwmnn.png
 

حالت دیفالت :

http://s6.uplod.ir/i/00892/uo5ztpt1lt7o.png
 

 

یعنی مثلا ما از تگ h2 استفاده کردیم پایین اون تگ h2 از تگی استفاده نکردیم اندازشون ست میشه 

 

ویرایش شده توسط ilya20
لینک به ارسال
در 7 دقیقه قبل، mehran-b گفته است :

عجیبه.

آدرس پست رو ارسال کنید.

خدمت شما 

https://firmware.ir/sm-j330/

 

https://firmware.ir/desire300-sence5-cooked/

الا کد رو برداشتم لازم بود بگید دوباره بذارم

ویرایش شده توسط ilya20
لینک به ارسال
در 3 دقیقه قبل، mehran-b گفته است :

اون نوشته ابتدای پست که h4 هست تگ پایانش حذف میشه.

فعلا کد رو بردارید و کد صفحه single.php رو بفرستید.

خدمت شما :

 

<?php 
get_header(); 
$typePage='product';
include_once('header-nav.php'); 
$m_top='margin-top:10px;';
$current_user=wp_get_current_user();
if($current_user->ID==NULL){
	$m_top='margin-top:50px;';
}
$template_header = get_option('avpb_setting_template_header','');

if($template_header=='theme2'){
	do_shortcode('[avpb_Create_head_theme2]');
	echo '<div class="main_bx" style="'.$m_top.'"> ';
	do_shortcode('[avpb_Create_menu_theme2]');	
}else{
	echo '<div class="main_bx" style="'.$m_top.'"> ';
}
?>


<div class="container">
  <?php if (have_posts()) : while (have_posts()) : the_post();
	global $post;
	setup_postdata( $post );
	
	$idauthor=get_the_author_meta('ID');
	$idauthor_info = get_userdata($idauthor);	
	
	$product=false;
	if(strpos(get_the_content(),'vip_linkdownload')){
		$product=true;
	}
	
	if(get_post_meta_avpb($post->ID,'avpb_product_link',true,'')){
		$product=true;
	}
	
	?>
  <div class="col-md-3 right" style="padding:0px;border-radius:10px;">
    <?php
		if($product==false){
			if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('head-sidebar')):endif;
			if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('sider-site')):endif; 
		}else{
			?>
    <div class="rb_box">
      <div class="rbb_body">
          <?php the_post_thumbnail('medium',array('class' => 'img-responsive responsive--full','title' => get_the_title())); ?>
          <?php $ilyatitle=get_post_meta_avpb($post->ID,'ilyatitle',true,get_the_title()); ?>
          <h2 class="boxinfo-title">
            <a title="<?php echo get_the_title();?>" href="<?php the_permalink()?>"><?php echo $ilyatitle;?></a>
          </h2>
          
          
          <?php
			$avpb_post_developer=get_post_meta_avpb($post->ID,'avpb_post_developer',true,'true');
			$avpb_post_developer='true';
			$avpb_product_link=get_post_meta_avpb($post->ID,'avpb_product_link',true,'');
			if($avpb_post_developer=='true' && $avpb_product_link!=''){
				echo do_shortcode('[avpb_vip_linkdownload]');
			}
	      ?>
            
          
          
        <!--<a class="btn btn-success" style="margin-bottom:10px;width:100%;" href="#" title="دانلود فلشر مربوطه"><span class="icon-download-alt"></span>دانلود فلشر مربوطه</a>-->
        
        <table class="table table-hover" style="margin-bottom:5px;">
        	
            <?php
			if(($x=get_post_meta_avpb($post->ID,'avpb_num_model',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-mobile"></i> شماره مدل</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_build_number',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-mobile"></i> بیلد نامبر</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_ver_android',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-android"></i> ورژن اندورید</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_moshakhase',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-android"></i> مشخصه</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_sim_card',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-android"></i> سیم کارت</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}	
			if(($x=get_post_meta_avpb($post->ID,'avpb_cpu',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-microchip"></i> emui</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_emui',true,''))!=''){
				echo '<tr>
					<td><i class="fa fa-android"></i> emui</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}			
			if(($x=get_post_meta_avpb($post->ID,'avpb_type_ram',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-android"></i> نوع رام</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_mantaghe',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-map-marker"></i> منطقه</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_lang_fa',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-language"></i> زبان فارسی</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_flasher',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-anchor"></i> فلشر</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_ver_file',true,''))!=''){
				echo '<tr>
					<td><i class="fa fa-anchor"></i> فایل ورژن</td>
					<td style="text-align:left"><a class="" href="'.$x.'">دانلود</a></td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_box',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-info-circle"></i> box</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_serial',true,''))!=''){
				echo '<tr>
					<td><i class="fa fa-info-circle"></i> مشخصات سریال</td>
					<td style="text-align:left">'.$x.'</td>
				</tr>';
			}
			if(($x=get_post_meta_avpb($post->ID,'avpb_cert',true,''))!=''){
				echo '<tr class="tablehovertbodytr">
					<td><i class="fa fa-info-circle"></i> cert</td>
					<td style="text-align:left"><a class="" href="'.$x.'">دانلود</a></td>
				</tr>';
			}
			?>
        
        	<?php
			edit_post_link( __( 'Edit', 'textdomain' ), '<tr><td colspan="2">', '</td></tr>', null, 'btn btn-primary btn-edit-post-link' );
			?>
        
        	<tr>
            	<td colspan="2"><hr></td>
            </tr>
        	<tr>
            	<td><i class="fa fa-server"></i> موضوع</td>
                <td style="text-align:left"><?php the_category(' , '); ?></td>
            </tr>
            <tr>
            	<td><i class="glyphicon glyphicon-user"></i> بازدید</td>
                <td style="text-align:left"><?php if(function_exists('the_views')) { the_views(); } ?> بار</td>
            </tr>
            <tr>
            	<td colspan="2"><i class="glyphicon glyphicon-calendar"></i> <?php the_time('j M Y'); ?></td>
            </tr>
            <tr>
            	<td colspan="2"><i class="glyphicon glyphicon-briefcase"></i> منبع : <a style="color:#46a0be" href="https://firmware.ir">سایت مرکزی فریمور موبایل</a></td>
            </tr>
        </table>
        
        <style>
		.tablehovertbodytr{
			overflow: hidden;
			white-space: nowrap;
			display: table-row;
		} 
        </style>
        
      </div>
    </div>
    <?php
			echo '<div class="hidden-sm hidden-xs">';
			if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('head-sidebar')):endif;
			if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('single-product')):endif; 
			echo '</div>';
		}
		?>
  </div>
  <div class="col-md-9 left" style="padding:0px;">
    <article class="post">
      <div class="hreview" style="width:5px;height: 5px;overflow:scroll;margin-top:-5px;"> <span class="item"> <span class="fn entry-title">
        <?php the_title(); ?>
        </span> </span> Reviewed by <span class="reviewer">
        <?php the_author(); ?>
        </span> Rating: <span class="rating">
        <?php rand(3,5) ?>
        .0</span> 
          <div class="summary">
			<?php the_excerpt(); ?>
          </div> 
        </div>
      <div class="psg">
        <div class="post_body" style="width: auto;"> 
        
          <h1>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post_body_title"> <i class="fa fa-caret-left"></i><?php the_title(); ?></a>
          </h1>
          
          <div class="post_body_note" >
            <?php the_content(); ?>
            <hr>
            <?php
			$avpb_description_before_install=get_post_meta_avpb($post->ID,'avpb_description_before_install',true,'');
			if($avpb_description_before_install!=''){
				echo '<h5><i class="fa fa-caret-left"></i> نکات قبل از استفاده</h5>';
				echo $avpb_description_before_install;
			}
			$avpb_description_install_firmware=get_post_meta_avpb($post->ID,'avpb_description_install_firmware',true,'');
			if($avpb_description_install_firmware!=''){
				echo '<h5><i class="fa fa-caret-left"></i> آموزش استفاده از محصول</h5>';
				echo $avpb_description_install_firmware; 
			}
			?>
            
      </div>
      <div class="post_body_footer">
        <div class="footer_item" > <span class="item_title it_red"><span class="glyphicon glyphicon-stop"></span> موضوع : </span> <span class="item_note">
          <?php the_category(' » '); ?>
          </span> </div>
        <div class="footer_item" > <span class="item_title it_blue"><span class="glyphicon glyphicon-stop"></span> نویسنده : </span> <span class="item_note" >
          <a href="<?php echo get_option('siteurl').'/@'.$idauthor_info->user_login; /*get_author_posts_url($idauthor_info->ID,$idauthor_info->user_login);*/ ?>"><?php echo (get_the_author()!=''?get_the_author():$idauthor_info->user_login); ?></a>          
          </span> </div>
        <div class="footer_item" > <span class="item_title it_yellow"><span class="glyphicon glyphicon-stop"></span> </span> <span class="item_note"><a href="<?php the_permalink(); ?>#comments" >
          <?php comments_number('بدون دیدگاه', 'دیدگاه', '% دیدگاه'); ?>
          </a></span> </div>
        <div class="footer_item" > <span class="item_title it_green"><span class="glyphicon glyphicon-stop"></span> </span> <span class="item_note" >
          <?php the_time('j M Y'); ?>
          </span> </div>
      </div>
    </article>
    <div class="tags"> <span>برچسب ها : </span>
      <?php the_tags('', ''); ?>
    </div>
    <div class="author-box">
      <div class="cell"> <?php echo '<img data-src="'.esc_url(get_avatar_url($post->post_author,array('size' => 80))).'" height="80" width="80" />'; 
	  //echo get_avatar(get_the_author_meta('ID'), 80);
	  ?> 
        <div class="clearfix"></div>
      </div>
      <div class="author-box_info">
        <div class="author-name" >
          <a href="<?php echo get_option('siteurl').'/@'; echo $idauthor_info->user_login; ?>" target="_blank"><?php echo (get_the_author()!=''?get_the_author():$idauthor_info->user_login); ?></a>
        </div>
        <div class="clearfix"></div>
        <p> 
          <?php the_author_meta('description'); ?>
        </p>
      </div>
    </div>
	
    <?php
    if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('post_footer')):
    endif; ?>
    <?php if (is_singular() && !is_page()) { ?>
    <div class="clearfix"></div>
    <?php 
		comments_template();
		/*if (is_single() && !post_password_required() && ( comments_open() || '0' != get_comments_number() ))
                comments_template();*/
        }
        ?>
    <?php endwhile;
		
	echo '<div class="hidden-md hidden-lg">';
	if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('head-sidebar')):endif;
	if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('single-product')):endif; 
	echo '</div>';
    
	
else : ?>
    <p>
      <?php _e('چیزی پیدا نشد'); ?>
    </p>
    <?php endif; ?>
  </div>
</div>
<?php include_once('footer-dushboard.php'); ?>
<?php get_footer(); ?>

 

لینک به ارسال

این کد رو جایگزین کد قبلی کنید انشاالله حل بشه.

function mehran_add_img_alt_title($content) {
	$dom = new DOMDocument();
	@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
	$post_images = $dom->getElementsByTagName('img');
	if($post_images->length > 0) {
		foreach($post_images as $node) {
			if($node->hasAttribute('alt')) $node->removeAttribute('alt');
			if($node->hasAttribute('title')) $node->removeAttribute('title');
			$node->setAttribute('alt', get_the_title());
			$node->setAttribute('title', get_the_title());
		}
	}
	return $dom->saveHTML($dom->documentElement);
}
add_filter('the_content', 'mehran_add_img_alt_title');

 

لینک به ارسال
در 3 دقیقه قبل، mehran-b گفته است :

این کد رو جایگزین کد قبلی کنید انشاالله حل بشه.


function mehran_add_img_alt_title($content) {
	$dom = new DOMDocument();
	@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
	$post_images = $dom->getElementsByTagName('img');
	if($post_images->length > 0) {
		foreach($post_images as $node) {
			if($node->hasAttribute('alt')) $node->removeAttribute('alt');
			if($node->hasAttribute('title')) $node->removeAttribute('title');
			$node->setAttribute('alt', get_the_title());
			$node->setAttribute('title', get_the_title());
		}
	}
	return $dom->saveHTML($dom->documentElement);
}
add_filter('the_content', 'mehran_add_img_alt_title');

 

درست نشده 

 

https://firmware.ir/desire300-sence5-cooked/

لینک به ارسال
در 3 دقیقه قبل، mehran-b گفته است :

تگ هدینگ ابتدای پست ها هم درون متن پست ها قرار داره؟

سورس ابتدای پست ها به این شکله

<h4>پست جامع مجموعه رام های رسمی اچ تی سی desire 300</h4>
<p>توجه کنید این پست نسخه جامع بوده و تمامی رامهای ارایه شده این مدل قرار میگیرد و در آینده نیز آپدیت خواهد شد.</p>
<p>&nbsp;</p>
<hr />
<p><strong>در این جدول تمامی مشخصات فریمور این مدل درج می شود. لطفا قبل از دانلود به مشخصات دقت فرمایید</strong></p>
<hr />

 

لینک به ارسال

درسته ولی وقتی کد رو میذارید اینطوری میشه:

<h4>پست جامع مجموعه رام های رسمی اچ تی سی desire 300
<p>توجه کنید این پست نسخه جامع بوده و تمامی رامهای ارایه شده این مدل قرار میگیرد و در آینده نیز آپدیت خواهد شد.</p>
<p>&nbsp;</p>
<hr />
<p><strong>در این جدول تمامی مشخصات فریمور این مدل درج می شود. لطفا قبل از دانلود به مشخصات دقت فرمایید</strong></p>
<hr />

حالا چرا اینطوری میشه خودمم موندم.

چون خودم الان کد رو روی تمام تگ های هدینگ تست کردم و مشکلی نداره فقط انگار روی سایت شما این مورد پیش میاد.

ویرایش شده توسط mehran-b
لینک به ارسال
در 2 دقیقه قبل، mehran-b گفته است :

درسته ولی وقتی کد رو میذارید اینطوری میشه:


<h4>پست جامع مجموعه رام های رسمی اچ تی سی desire 300
<p>توجه کنید این پست نسخه جامع بوده و تمامی رامهای ارایه شده این مدل قرار میگیرد و در آینده نیز آپدیت خواهد شد.</p>
<p>&nbsp;</p>
<hr />
<p><strong>در این جدول تمامی مشخصات فریمور این مدل درج می شود. لطفا قبل از دانلود به مشخصات دقت فرمایید</strong></p>
<hr />

حالا چرا اینطوری میشه خودمم موندم.

چون خودم الان کد رو روی تمام تگ های هدینگ تست کردم و مشکلی نداره فقط انگار روی سایت شما این مورد پیش میاد.

اره فکر کنم اینطوری باشه میشه لطفا بیشتر بررسی کنید 

خیلی عذر میخوام سیستم ما کاملا اختصاصی هست برای همین نمیتونم دسترسی مدیریت بدم خدمتتون وگرنه دسترسی میدادم بهتر بررسی میکردید

 

لینک به ارسال
در 18 دقیقه قبل، ilya20 گفته است :

اره فکر کنم اینطوری باشه میشه لطفا بیشتر بررسی کنید 

خیلی عذر میخوام سیستم ما کاملا اختصاصی هست برای همین نمیتونم دسترسی مدیریت بدم خدمتتون وگرنه دسترسی میدادم بهتر بررسی میکردید

 

باید از روش های قدیمی استفاده کنید.

این پلاگین رو نصب کنید.

بعد تنظیماتش رو مانند تصویر زیر کنید احتمالا حل میشه مشکلتون.

seo.jpg.435ab8e6d7d1cb55583abb5a48a6d4bd.jpg

لینک به ارسال
در 41 دقیقه قبل، ilya20 گفته است :

دل خوشی از افزونه ندارم خیلی ممنون از لطف شما 

اگه با کد اوکی شد چه بهتر نشد عطایش را به لغایش ببخشیم بره 

 

متوجه ام چی میگی. خودم هم زیاد با پلاگین حال نمیکنم.

اینو جایگزین کد قبلی کن.

function mehran_add_img_alt_title_process($matches) {
	global $post;
	$title = $post->post_title;
	$alttext_rep = "%title";
	$titletext_rep = "%title";
	$override= "on";
	$override_title= "on";

	# take care of unsusal endings
	$matches[0]=preg_replace('|([\'"])[/ ]*$|', '\1 /', $matches[0]);

	### Normalize spacing around attributes.
	$matches[0] = preg_replace('/\s*=\s*/', '=', substr($matches[0],0,strlen($matches[0])-2));
	### Get source.

	preg_match('/src\s*=\s*([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/', $matches[0], $source);

	$saved=$source[2];

	### Swap with file's base name.
	preg_match('%[^/]+(?=\.[a-z]{3}\z)%', $source[2], $source);
	### Separate URL by attributes.
	$pieces = preg_split('/(\w+=)/', $matches[0], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
	### Add missing pieces.

	$postcats=get_the_category();
	$cats="";
	if ($postcats) {
		foreach($postcats as $cat) {
			$cats = $cat->slug. ' '. $cats;
		}
	}

	$posttags = get_the_tags();

	$tags="";
	if ($posttags) {
		foreach($posttags as $tag) {
			$tags = $tag->name . ' ' . $tags;
		}
	}

	if (!in_array('title=', $pieces) || $override_title=="on") {
		$titletext_rep=str_replace("%title", $post->post_title, $titletext_rep);
		$titletext_rep=str_replace("%name", $source[0], $titletext_rep);
		$titletext_rep=str_replace("%category", $cats, $titletext_rep);
		$titletext_rep=str_replace("%tags", $tags, $titletext_rep);
	
		$titletext_rep=str_replace('"', '', $titletext_rep);
		$titletext_rep=str_replace("'", "", $titletext_rep);
	
		$titletext_rep=str_replace("_", " ", $titletext_rep);
		$titletext_rep=str_replace("-", " ", $titletext_rep);
		//$titletext_rep=ucwords(strtolower($titletext_rep));
		if (!in_array('title=', $pieces)) {
			array_push($pieces, ' title="' . $titletext_rep . '"');
		} else {
			$key=array_search('title=',$pieces);
			$pieces[$key+1]='"'.$titletext_rep.'" ';
		}
	}

	if (!in_array('alt=', $pieces) || $override=="on" ) {
		$alttext_rep=str_replace("%title", $post->post_title, $alttext_rep);
		$alttext_rep=str_replace("%name", $source[0], $alttext_rep);
		$alttext_rep=str_replace("%category", $cats, $alttext_rep);
		$alttext_rep=str_replace("%tags", $tags, $alttext_rep);
		$alttext_rep=str_replace("\"", "", $alttext_rep);
		$alttext_rep=str_replace("'", "", $alttext_rep);
		$alttext_rep=(str_replace("-", " ", $alttext_rep));
		$alttext_rep=(str_replace("_", " ", $alttext_rep));
	
		if (!in_array('alt=', $pieces)) {
			array_push($pieces, ' alt="' . $alttext_rep . '"');
		} else {
			$key=array_search('alt=',$pieces);
			$pieces[$key+1]='"'.$alttext_rep.'" ';
		}
	}
	return implode('', $pieces).' /';
}
function mehran_add_img_alt_title($content) {
	return preg_replace_callback('/<img[^>]+/', 'mehran_add_img_alt_title_process', $content);
}
add_filter('the_content', 'mehran_add_img_alt_title', 100);

 

لینک به ارسال
در 12 دقیقه قبل، mehran-b گفته است :

متوجه ام چی میگی. خودم هم زیاد با پلاگین حال نمیکنم.

اینو جایگزین کد قبلی کن.


function mehran_add_img_alt_title_process($matches) {
	global $post;
	$title = $post->post_title;
	$alttext_rep = "%title";
	$titletext_rep = "%title";
	$override= "on";
	$override_title= "on";

	# take care of unsusal endings
	$matches[0]=preg_replace('|([\'"])[/ ]*$|', '\1 /', $matches[0]);

	### Normalize spacing around attributes.
	$matches[0] = preg_replace('/\s*=\s*/', '=', substr($matches[0],0,strlen($matches[0])-2));
	### Get source.

	preg_match('/src\s*=\s*([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/', $matches[0], $source);

	$saved=$source[2];

	### Swap with file's base name.
	preg_match('%[^/]+(?=\.[a-z]{3}\z)%', $source[2], $source);
	### Separate URL by attributes.
	$pieces = preg_split('/(\w+=)/', $matches[0], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
	### Add missing pieces.

	$postcats=get_the_category();
	$cats="";
	if ($postcats) {
		foreach($postcats as $cat) {
			$cats = $cat->slug. ' '. $cats;
		}
	}

	$posttags = get_the_tags();

	$tags="";
	if ($posttags) {
		foreach($posttags as $tag) {
			$tags = $tag->name . ' ' . $tags;
		}
	}

	if (!in_array('title=', $pieces) || $override_title=="on") {
		$titletext_rep=str_replace("%title", $post->post_title, $titletext_rep);
		$titletext_rep=str_replace("%name", $source[0], $titletext_rep);
		$titletext_rep=str_replace("%category", $cats, $titletext_rep);
		$titletext_rep=str_replace("%tags", $tags, $titletext_rep);
	
		$titletext_rep=str_replace('"', '', $titletext_rep);
		$titletext_rep=str_replace("'", "", $titletext_rep);
	
		$titletext_rep=str_replace("_", " ", $titletext_rep);
		$titletext_rep=str_replace("-", " ", $titletext_rep);
		//$titletext_rep=ucwords(strtolower($titletext_rep));
		if (!in_array('title=', $pieces)) {
			array_push($pieces, ' title="' . $titletext_rep . '"');
		} else {
			$key=array_search('title=',$pieces);
			$pieces[$key+1]='"'.$titletext_rep.'" ';
		}
	}

	if (!in_array('alt=', $pieces) || $override=="on" ) {
		$alttext_rep=str_replace("%title", $post->post_title, $alttext_rep);
		$alttext_rep=str_replace("%name", $source[0], $alttext_rep);
		$alttext_rep=str_replace("%category", $cats, $alttext_rep);
		$alttext_rep=str_replace("%tags", $tags, $alttext_rep);
		$alttext_rep=str_replace("\"", "", $alttext_rep);
		$alttext_rep=str_replace("'", "", $alttext_rep);
		$alttext_rep=(str_replace("-", " ", $alttext_rep));
		$alttext_rep=(str_replace("_", " ", $alttext_rep));
	
		if (!in_array('alt=', $pieces)) {
			array_push($pieces, ' alt="' . $alttext_rep . '"');
		} else {
			$key=array_search('alt=',$pieces);
			$pieces[$key+1]='"'.$alttext_rep.'" ';
		}
	}
	return implode('', $pieces).' /';
}
function mehran_add_img_alt_title($content) {
	return preg_replace_callback('/<img[^>]+/', 'mehran_add_img_alt_title_process', $content);
}
add_filter('the_content', 'mehran_add_img_alt_title', 100);

 

بلاخره این دستور جواب داد 

خیلی ممنون از لطف شما واقعا زحمت کشیدید 

لینک به ارسال

کد اخری رو کمی با عجله نوشتم الان که با دقت به کد نگاه کردم دیدم میشه فشرده تر و سبک تر هم کردش. نسخه بهینه ترش میشه این.

function mehran_add_img_alt_title_process($matches) {
	$title = get_the_title();
	$alttext_rep = "%title";
	$titletext_rep = "%title";
	$override= "on";
	$override_title= "on";

	# take care of unsusal endings
	$matches[0]=preg_replace('|([\'"])[/ ]*$|', '\1 /', $matches[0]);

	### Normalize spacing around attributes.
	$matches[0] = preg_replace('/\s*=\s*/', '=', substr($matches[0],0,strlen($matches[0])-2));
	### Get source.

	preg_match('/src\s*=\s*([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/', $matches[0], $source);

	$saved=$source[2];

	### Swap with file's base name.
	preg_match('%[^/]+(?=\.[a-z]{3}\z)%', $source[2], $source);
	### Separate URL by attributes.
	$pieces = preg_split('/(\w+=)/', $matches[0], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
	### Add missing pieces.

	if (!in_array('title=', $pieces) || $override_title=="on") {
		$titletext_rep=str_replace("%title", $title, $titletext_rep);
	
		$titletext_rep=str_replace('"', '', $titletext_rep);
		$titletext_rep=str_replace("'", "", $titletext_rep);
	
		$titletext_rep=str_replace("_", " ", $titletext_rep);
		$titletext_rep=str_replace("-", " ", $titletext_rep);
		//$titletext_rep=ucwords(strtolower($titletext_rep));
		if (!in_array('title=', $pieces)) {
			array_push($pieces, ' title="' . $titletext_rep . '"');
		} else {
			$key=array_search('title=',$pieces);
			$pieces[$key+1]='"'.$titletext_rep.'" ';
		}
	}

	if (!in_array('alt=', $pieces) || $override=="on" ) {
		$alttext_rep=str_replace("%title", $title, $alttext_rep);
		$alttext_rep=str_replace("\"", "", $alttext_rep);
		$alttext_rep=str_replace("'", "", $alttext_rep);
		$alttext_rep=(str_replace("-", " ", $alttext_rep));
		$alttext_rep=(str_replace("_", " ", $alttext_rep));
	
		if (!in_array('alt=', $pieces)) {
			array_push($pieces, ' alt="' . $alttext_rep . '"');
		} else {
			$key=array_search('alt=',$pieces);
			$pieces[$key+1]='"'.$alttext_rep.'" ';
		}
	}
	return implode('', $pieces).' /';
}
function mehran_add_img_alt_title($content) {
	return preg_replace_callback('/<img[^>]+/', 'mehran_add_img_alt_title_process', $content);
}
add_filter('the_content', 'mehran_add_img_alt_title', 100);

 

لینک به ارسال

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

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

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

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

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

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

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

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

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