WordPress.org

Support

Support » Plugins and Hacks » Hacks » wp_get_attachment_image_attributes filter not working

wp_get_attachment_image_attributes filter not working

  • I’ve seen this filter in a few places on the net and I’ve looked at the core WP code, as far as I can see this should work, but it doesn’t work at all.

    function remove_img_title($atts) {
    	unset($atts['title']);
    	return $atts;
    }
    add_filter('wp_get_attachment_image_attributes','remove_img_title',1,1);

    Obviously I’m trying to remove the title attribute from images inserted to posts… I’ve also tried replacing the title with an arbitrary string, but that doesn’t work either. I saw in another forum Q that their filter was only working for posts with feature images, so I tried featuring the image… still doesn’t work.

    Please help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Are the images already inserted into posts? Or are you using functions like wp_get_attachment_image() or do you want the title removed when you insert an image into a post?

    The filter works for me on wp_get_attachment_image().

    I’m trying to prevent the title attribute being added when an image is added to the post when you click “Insert into Post”.

    You’re right, it works when you just echo wp_get_attachment_image() somewhere…

    I would have thought the media dialogue used this function too, but maybe not.

    I guess the media dialogue is using javascript to insert the image to the post, so unless the JS is generated by PHP and has a filter, then we have to remove the title from the post with a content filter or javascript on the front end?

    Moderator keesiemeijer

    @keesiemeijer

    The only way I can think of is adding filters to content_save_pre and the_content
    Put this in your functions.php and see if it works:

    // change the post content just before saving to the database when publishing or updating a new post
    add_filter('content_save_pre', 'remove_image_title');
    // change post content on the front end (for older posts that have the title in images)
    add_filter('the_content', 'remove_image_title');
    
    function remove_image_title($text){
    
    	$upload_dir = wp_upload_dir();
    	$upload_url = preg_quote($upload_dir['baseurl'], '/');
    	$publish_or_edit = false;
    
    	if(is_admin()){
    		$screen = get_current_screen();
    		if($screen->base == 'post') {
    			$publish_or_edit = true;
    			$text = stripslashes($text);
    		}
    	}
            // here starts the regex craziness!!
    	if(preg_match('/<img(.*?)src="'.$upload_url.'(.*?)"(.*?)>/', $text)){
    		//there is an image attachement in the post_content
    		$patterns = array(
    		'/<img(.*?)src="'.$upload_url.'(.*?)"(.*?)title="(.*?)"(.*?)>/',
    		'/<img(.*?)title="(.*?)"(.*?)src="'.$upload_url.'(.*?)"(.*?)>/'
    		);
    		$replacements = array(
    		'<img\1src="'.$upload_dir['baseurl'].'\2"\3\5>',
    		'<img\1\3src="'.$upload_dir['baseurl'].'\4"\5>'
    		);
    		$text = preg_replace($patterns,$replacements,$text);
    	}
    
    	if($publish_or_edit){
    		$text = esc_sql($text);
    	}
    
    	return $text;
    }

    I think images inserted into posts are handled differently; maybe try the image_send_to_editor filter in get_image_send_to_editor().

    wp_get_attachment_image() is used for Featured Images and other images output in templates.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_get_attachment_image_attributes filter not working’ is closed to new replies.
Skip to toolbar