In your case, I believe the best solution would be to use a Parser de HTML, that allows you to find the tags HTML as well as jQuery. However, I don’t know how your WP code is structured, nor if you can include the library (business or whatever reasons), so I will propose a solution using regular expressions.
Full Disclaimer: Regular expressions are costly and can cause slowness, and what I wrote can probably be written in a more intelligent way. I’m also assuming that Wordpress at all times will return the links using double quotation Marks (i and.., "
and not '
). If tomorrow they decide to change, this solution will fall. Finally, the extraction of the attributes is nothing elegant, which can cause adverse reactions in the most experienced programmers.
The secret of the thing, as far as the world of WP is concerned, is to use the right filters. More specifically, two. Filters serve to invoke methods at certain times, and you can read more about filters here. In this case
add_filter( 'post_thumbnail_html', 'build_custom_url', 10 );
add_filter( 'image_send_to_editor', 'build_custom_url', 10 );
are the necessary filters. Just create the method build_custom_url()
to intercept the HTML generated by WP and modify it according to your needs.
Using preg_match_all()
, it is possible to compare the contents of a string ($html) with the Regex parameters and save them in an array($result)
. Making
preg_match_all('/(width|height|src)=("[^"]*")/i', $html, $result);
print_r($result);
you get (for example):
Array
(
[0] => Array
(
[0] => width="247"
[1] => height="359"
[2] => src="http://localhost:8880/wp/wp-content/uploads/2016/07/pg.png"
)
[1] => Array
(
[0] => width
[1] => height
[2] => src
)
[2] => Array
(
[0] => "247"
[1] => "359"
[2] => "http://localhost:8880/wp/wp-content/uploads/2016/07/pg.png"
)
)
I did this test by getting the HTML that WP itself provided me, through one of these filters. See what you can do now
$width = $result[2][0];
$height = $result[2][1];
$src = substr($result, strrpos($result, '/') + 1); //necessário para extraír o nome do arquivo.
Note that it is now simple to build (and return) your HTML as you wish, based on these parameters. With this, the final code is
add_filter( 'post_thumbnail_html', 'build_custom_url', 10 );
add_filter( 'image_send_to_editor', 'build_custom_url', 10 );
function build_custom_url( $html ) {
preg_match_all('/(width|height|src)=("[^"]*")/i', $html, $result);
$width = $result[2][0];
$height = $result[2][1];
$src = substr($result, strrpos($result, '/') + 1); //necessário para extraír o nome do arquivo.
// Reconstruindo a URL. Aqui você adequa a sua necessidade real
$html = '<img src=http://meusite/'.$width.'x'.$height.'/'.$src;
return $html;
}
Match this code to your need, and add it to your archive functions.php
, and everything should work.
Remove the tag attributes
img
is quiet. Now, do you want to change the path of your image based on these parameters? That’s it?– Caio Felipe Pereira
Exactly that @Caiofelipepereira. I want to be able to use the image parameters so I can assemble the image URL as I want... which is easier when I use an image handler via url.
– Weslley Ribeiro