Is it possible to manipulate the output of the image captions within the_content in such a way that not only the caption, but also the image title and/or description (defined in the media library) are output under each image?
Possibly via the functions.php?
Yes it is possible to manipulate the on the_content.
The easiest way which it looks like you are wanting to do is add a filter in your functions.php file.
I just pulled this from the Codex and modified a bit so you can get the gist of what I'm talking about.
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $output, $attr, $content ) {
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '</p>'
. '</div>';
}
What you can then do is if you see the $attr['caption'], you can modify how you would like it to be. So you can do: get_the_title($attr['id']) to add the title or get_post_meta($attr['id'], '_wp_attachment_image_alt', TRUE); to get the image alt.
To make things easier if you want to grab all the details on a specific attachment you can use a function like this and then get whatever you want.
Here's an example of adding the title to your caption. The format is "Title - Caption"
. '<p class="wp-caption-text">' . get_the_title($attr['id']) . " - " . $attr['caption'] . '</p>'
Link to Code reference: https://developer.wordpress.org/reference/hooks/img_caption_shortcode/
Maybe i'm not understanding your question but it seems to me that you're using the Gutenberg editor?
... All the features you're looking for are already built-in.
If you want to set the alt="" or title="" attributes you can do it on the right side action column, by clicking on the image itself, while in the Gutenberg editor.
For the figcaption, you can set it right under the image, by clicking on the image itself, while in the Gutenberg editor.
Related
Building custom woocommerce template on top of storefront.
I want to replace/change some html for the individual item titles within the loop functionality for the category pages. The "add to cart" link, price, etc. are all located in woocommerce/templates/loop directory. None of the files in that directory appear contain the html for the product title for this functionality.
Where do I find this thing? I swear the most annoying thing about building on woocommerce is trying to find where all the pieces live...
woocommerce/includes/wc-template-functions.php
woocommerce_template_loop_product_title function
function woocommerce_template_loop_product_title() {
echo '<h3 class="woocommerce-loop-product__title">' . get_the_title() . '</h3>';
}
You need change the information in the file functions.php, preferably in the theme child file.
What you put there will normally be rewritten because it have priority.
Like Randy wrote it, this is a pluggable function you will find in
wp-content\plugins\woocommerce\includes\wc-template-functions.php
if ( ! function_exists( 'woocommerce_template_loop_product_title' ) ) {
/**
* Show the product title in the product loop. By default this is an H2.
*/
function woocommerce_template_loop_product_title() {
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
To override it, just copy paste the code without the condition in the functions.php file of your child theme and modify it according to your needs.
Ex: Change h2 for h3
function woocommerce_template_loop_product_title() {
echo '<h3 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h3>';
}
I'm trying to replace the default image html output in WordPress editor with responsive images picture tag.
I created a custom function that replaces the default img html and hooked it into editor via image_send_to_editor filter:
function mystic_responsive_insert_image($html, $id, $caption, $title, $align, $url) {
$image_url = wp_get_attachment_url($id);
$attachment_id = attachment_url_to_postid( $image_url );
$alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if ( !$alt_text ) { $alt_text = esc_html( get_the_title($post_id) ); }
$thumb_xl_hpt1 = wp_get_attachment_image_src($attachment_id, 'xl-thumb-hpt1');
$thumb_lg_hpt1 = wp_get_attachment_image_src($attachment_id, 'lg-thumb-hpt1');
$thumb_md_hpt1 = wp_get_attachment_image_src($attachment_id, 'md-thumb-hpt1');
$thumb_sm_hpt1 = wp_get_attachment_image_src($attachment_id, 'sm-thumb-hpt1');
$thumb_xs_hpt1 = wp_get_attachment_image_src($attachment_id, 'xs-thumb-hpt1');
$thumb_xl_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'xl-thumb-hpt1-2x');
$thumb_lg_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'lg-thumb-hpt1-2x');
$thumb_md_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'md-thumb-hpt1-2x');
$thumb_sm_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'sm-thumb-hpt1-2x');
$thumb_xs_hpt1_2x = wp_get_attachment_image_src($attachment_id, 'xs-thumb-hpt1-2x');
$thumb_data = array(
'thumb_xl_hpt1_2x' => $thumb_xl_hpt1_2x[0],
'thumb_large_hpt1_2x' => $thumb_lg_hpt1_2x[0],
'thumb_medium_hpt1_2x' => $thumb_md_hpt1_2x[0],
'thumb_small_hpt1_2x' => $thumb_sm_hpt1_2x[0],
'thumb_xs_hpt1_2x' => $thumb_xs_hpt1_2x[0],
'thumb_xl_hpt1' => $thumb_xl_hpt1[0],
'thumb_large_hpt1' => $thumb_lg_hpt1[0],
'thumb_medium_hpt1' => $thumb_md_hpt1[0],
'thumb_small_hpt1' => $thumb_sm_hpt1[0],
'thumb_xs_hpt1' => $thumb_xs_hpt1[0],
'thumb_alt' => $alt_text
);
$html = '<picture>';
$html .= '<!--[if IE 9]><video style="display: none;"><![endif]-->';
$html .= '<source srcset="' . $thumb_data['thumb_xl_hpt1'] . ', ' . $thumb_data['thumb_xl_hpt1_2x'] . ' 2x" media="(min-width: 1200px)">';
$html .= '<source srcset="' . $thumb_data['thumb_large_hpt1'] . ', ' . $thumb_data['thumb_large_hpt1_2x'] . ' 2x" media="(min-width: 992px)">';
$html .= '<source srcset="' . $thumb_data['thumb_medium_hpt1'] . ', ' . $thumb_data['thumb_medium_hpt1_2x'] . ' 2x" media="(min-width: 768px)">';
$html .= '<source srcset="' . $thumb_data['thumb_small_hpt1'] . ', ' . $thumb_data['thumb_small_hpt1_2x'] . ' 2x" media="(min-width: 576px)">';
$html .= '<!--[if IE 9]></video><![endif]-->';
$html .= '<img srcset="' . $thumb_data['thumb_xs_hpt1'] . ', ' . $thumb_data['thumb_xs_hpt1_2x'] . ' 2x" alt="' . $thumb_data['thumb_alt'] . '">';
$html .= '</picture>';
return $html;
}
add_filter( 'image_send_to_editor', 'mystic_responsive_insert_image', 10, 9 );
I then created a test post and inserted the image into it to see if my code works. It does. But then after I deleted the image and inserted another one, inside the chrome inspector I noticed that the additional html code from the deleted image is still present on the page. Everything except the img tag that was placed inside the picture tag is still there. Deleting the image from the editor removed only the img tag.
I'm making this for someone who is not familiar with html and I'm certain that they will not be able to delete the leftover code from the text editor if they ever edit the images in their posts so I need to figure out why is this happening and how can I fix it?
This is because while removing image, visual editor only removes image in img tag and rest of the HTML is still there. On updating, it updates the HTML in database.
Also, It appends previous HTML with new HTML.
You just have to filter data before it is saved. Have to remove 'picture' tag in which there is no image. You can use 'wp_insert_post_data' hook for this.
I'm trying to use the Advanced Custom Fields plugin shortcode as a attribute in another shortcode.
The company my theme was made by is not offering custom support at this time.
The original shortcode is made list this
public function plan( $atts, $content )
{
$html = array();
extract( shortcode_atts( array(
'price' => '',
'price_info' => '',
'type' => '',
'delay' => '',
'class' => '',
), $atts ) );
$html[] = '<div class="' . ( $class ) . '">';
$html[] = '<div class="plan has-animation" data-delay="' . ( (int)$delay ) . '">';
$html[] = '<div class="plan-container">';
$html[] = '<ins class="plan-price">' .( $price ) . '</ins>';
$html[] = '<span class="price-info">' . ( $price_info ) . '</span>';
$html[] = '<h2 class="second_color">' . ( $type ) . '</h2>';
$html[] = do_shortcode( $content );
$html[] = '</div>';
$html[] = '</div>';
$html[] = '</div>';
return implode("\n", $html);
}
And when i call the shortcode [pt_plan price="$12.99" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"] I want to make the price attribute allow a shortcode as a value to take advantage of Advanced Custom Fields.
However, if I place [acf field='session_price_1'] as the attribute value, like this, [pt_plan price="[acf field='session_price_1']" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"] it comes out as if that was a string and breaks the original pt_plan shortcode.
Could someone point me in the right direction? I thought it would be to add do_shortcode() to the $price variable in the plan shortcode, but it did not work.
Hope all this makes sense and thanks in advance!!
I actually got it to work.
Because the way shortcodes work in WP, the ending ] of the nested shortcode was closing off the original shortcode, screwing everything up.
To get around this, I did the following:
$html[] = '<div class="plan-price">'.do_shortcode('['.$price.']').'</div>';
and
[pt_plan price="acf field='session_price_1'" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"]
and that worked great!
Basically I just typed the text of the shortcode and ' instead of " into the attribute, concatenated the [ and ] in when it prints the attribute value into the , and used do_shortcode() to run the shortcode.
I'm using the woocommerce Dynamic Pricing Plugin - how can I change the "FROM" field and replace with custom text?
Here's my code which is attempting to filter the original plugins code;
The TEST text works fine, but the price is returning as 0.00
add_filter( 'wc_dynamic_pricing_price_html', 'custom_single_price');
function custom_single_price ($html, $_product) {
$from = '<span class="test">' . _x( 'TEST:', 'min_price', 'woocommerce' ) . ' </span>';
$html = '<del>' . WC_Dynamic_Pricing_Compatibility::wc_price( $base_price ) . '</del><ins> ' . $from . WC_Dynamic_Pricing_Compatibility::wc_price( $display_price ) . '</ins>';
return $html;
}
I've seen some variations of this question around, but I've as of yet been unable to tweak them into what I'm looking for.
In a Wordpress post, if an image is uploaded with a caption the image will be output wrapped in a div (with class wp-caption). Perfect.
However, if there is no caption the image is wrapped in a paragraph tag. I'd like to replace this with a div.
So if an image is uploaded without a caption, it is wrapped in a div as well — something like class="wp-nocaption".
My first try was to modify the media.php file. I changed this section:
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align)
. '" style="width: ' . (10 + (int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
to this:
if ( 1 > (int) $width || empty($caption) )
return '<div ' . $id . 'class="wp-nocaption ' . esc_attr($align)
. '" style="width: ' . ($width) . 'px">'
. do_shortcode( $content ) . '</div>';
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align)
. '" style="width: ' . ($width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
Which I hoped would do the same thing for a non-captioned image as a captioned one, except the div would have a different class.
But that didn't work. Does anyone have any idea why?
I also found some articles suggesting using a preg_replace function.
http://css-tricks.com/snippets/wordpress/remove-paragraph-tags-from-around-images/
https://wordpress.stackexchange.com/questions/7090/stop-wordpress-wrapping-images-in-a-p-tag
I tweaked their functions, attempting to replace paragraph tags that wrap an image wrapped in a link with div tags.
function filter_ptags_on_images($content) {
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU',
'/<div class="wp-nocaption">\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/div>/iU',
$content);
}
add_filter('the_content', 'filter_ptags_on_images');
But this doesn't work. I've been reading a lot but still can't wrap my head around the php concept of "backreference". I know the replace section of my function is wrong, but I'm not sure how to fix it.
If you have any suggestions as to what I'm doing wrong on either way would be very much appreciated!
Thank you so much!!