Expanding shortcode capabilities
-
Hi!
I slightly modified your excellent plugin for the needs. And I thought: maybe it’s like the author and she will add it?
To Display a previously created tooltip, you use a shortcode:
[act_tooltip id="XX"/]
Where id – is id of tooltip.
But suppose that we have a website recipes. Each recipe has a list of products. It is selected by the editor when editing a recipe. We want to show the user a tooltip for each ingredient.
In order not to overload the tooltips on the page, we use the shortcode specified above.
But to register compliance for each ingredient the ID of the tooltip is ineffective. So I did the following:
Changed the value of the “title” parameter in the function to replace the ID.
In fileadvanced-custom-tooltips/public/class-advanced-custom-tooltips-public.php
I changed the “tooltip_shortcode” function:/** * Tooltip shortcode. * * @since 1.0.0 */ public function tooltip_shortcode( $atts, $code_content ) { if( !isset( $atts['title'] ) && !isset( $atts['content'] ) && !isset( $atts['id'] ) ) { //No tooltip ID or content provided, do nothing return; } if( isset( $atts['title'] ) && isset( $atts['id'] ) ) { //Both ID and Title tooltip, do nothing return; } $defaults = extract( shortcode_atts(array( 'id' => '', 'title' => '', 'content' => '', ), $atts, 'act-tooltip-shortcode-atts' ) ); if( !isset( $atts['id'] ) && isset( $atts['title'] ) ) { //Plain text tooltip query_posts( 'post_type=act_tooltip&title=' . $title ); if ( have_posts()) : while (have_posts()) : the_post(); if( $code_content ) { $tooltip_text = $code_content; } else { $tooltip_text = get_the_title(); } $tooltip_content = $this->format_tooltip_content( get_the_content() ); endwhile; endif; wp_reset_query(); return '<span class="act-tooltip" title="' . $tooltip_content . '">' . $tooltip_text . '</span>'; } if( isset( $atts['id'] ) && !isset( $atts['title'] ) ) { //ID provided, get this tooltip from db query_posts( 'post_type=act_tooltip&p=' . $id ); if ( have_posts()) : while (have_posts()) : the_post(); if( $code_content ) { $tooltip_text = $code_content; } else { $tooltip_text = get_the_title(); } $tooltip_content = $this->format_tooltip_content( get_the_content() ); endwhile; endif; wp_reset_query(); return '<span class="act-tooltip" title="' . $tooltip_content . '">' . $tooltip_text . '</span>'; } }
Now in the recipe page template code I write the following
echo do_shortcode('[act_tooltip title="'.$ingredient.'" /]');
Where $ingredient is the name of the ingredient. But here the name of the tooltip must be identical to the name of the ingredient.
For me it is very much reduced the time to fill the site. I hope I can help someone.
P.S.: I am from Russia and write through a translator. I apologize for the mistakes in the text.
- The topic ‘Expanding shortcode capabilities’ is closed to new replies.