Crocoblock / suggestions

The suggestions for CrocoBlock project
197 stars 79 forks source link

🔥[Jetengine] Advanced Shortcode Generator #3565

Open nreljed opened 3 years ago

nreljed commented 3 years ago

hello friends,

I still have difficulty in solving this enigma since it includes a price and its currency and period

Jetengine Shortcode Generator saves my life but ... something is missing ... term field shortcode

Demonstration

Until now I had to use two widgets which I find not very optimized and no longer suit me..

before

As we are in the era where we must reduce the number of widgets and simplify the design to optimize the rendering as much as possible.. this is where Shortcode Generator comes into play.

My second solution with a single widget (Dynamic Field) price works well with shortcodes but only if it's a meta field..

what if we added a term field?

after

it will be necessary to have parameters , to do the same thing as the widgets like activating the links or not, where can be parent or child depth etc ... we will call them Advanced Shortcode Generator.

The interest is to allow us to do more advanced things combine terms and meta ,freedom to combine information into a single widget . inserting options like (currency) globally on all the prices on the site without juggling between several widgets and redoing the same inline style widget several times in several single page locations , cards, archive page, etc....

I take this opportunity to show you how I manage this problematic before jetengine with my old Frankenstein DIY php code

It might help you understand what I expect to be able to do with jetengine Shortcode Generator

In this example I tried to display the cities and countries Kyiv, Ukraine or Kyiv or Ukraine

/*output taxonomy child, parent*/
add_shortcode( 'child-parent', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){

    $atts = shortcode_atts( array(
        'link' => 'true',
        'taxonomy' => 'property_city' // my default taxonomy 
    ), $atts, 'child-parent' );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );

    ob_start();
    foreach( $terms as $term ){

        if ($atts['link'] !== 'false') {
            printf( '<a href="%s">%s</a>, ',  esc_url( get_term_link($term) ), $term->name );
        } else {
             echo $term->name . ', ';

        }

          if( $term->parent != 0 ){
            $parent_term = get_term( $term->parent, $taxonomy );
            if ($atts['link'] !== 'false') {
                printf( '<a href="%s">%s</a>', esc_url( get_term_link($parent_term) ), $parent_term->name );
            } else {
                echo $parent_term->name;
            }
        }

    }

    return ob_get_clean();
}

/*output only taxonomy parent*/
add_shortcode( 'parent', 'taxonomy_parent' );
function taxonomy_parent( $atts ){

    $atts = shortcode_atts( array(
        'link' => 'true',
        'taxonomy' => 'property_city' // my default taxonomy 
    ), $atts, 'parent' );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );

    ob_start();
    foreach( $terms as $term ){
        if( $term->parent != 0 ){
            $parent_term = get_term( $term->parent, $taxonomy );
            if ($atts['link'] !== 'false') {
                printf( '<a href="%s">%s</a>', esc_url( get_term_link($parent_term) ), $parent_term->name );
            } else {
                echo $parent_term->name;
            }
        }      
    }

    return ob_get_clean();
}

/*output only taxonomy child*/
add_shortcode( 'child', 'taxonomy_child' );
function taxonomy_child( $atts ){

    $atts = shortcode_atts( array(
        'link' => 'true',
        'taxonomy' => 'property_city' // my default taxonomy 
    ), $atts, 'child' );

    global $post;
    $terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );

    ob_start();
    foreach( $terms as $term ){
        if ($atts['link'] !== 'false') {
            printf( '<a href="%s">%s</a>', esc_url( get_term_link($term) ), $term->name );
        } else {
            echo $term->name;
        }
    }

    return ob_get_clean();
}

The idea is to explore as much as possible the possibilities of the Shortcode Generator and allow parameters :)

Solution

-allow to make terms shortcodes -allow to make parameters in shortcodes (look at my example code) Capture d’écran 2021-06-29 021526

-allow to make formatted numeric shortcodes [format number] Capture d’écran 2021-06-29 021426

-allow to make shortcodes more developer friendly and safely

Frankenstein

lifeact commented 3 years ago

follow