skip to Main Content

I wanted to know if there is a way for the chocolate to be directed to two different parameters? And I will explain

this shortcode [group skill] => return $skill;

This is the second shortcode [group lang] => return $lang;

I have a group that I opened through ACF and I want to take out a shortcode every time to a different place, to the place intended for it

This is the original code

<?php 
add_shortcode('group', function ($atts) {
    $group_field = get_field('info_global_course');
    if ($group_field):

        $attributes = shortcode_atts([
            'level_skill' => $group_field[level_skill],
            'lang' =>  $group_field[lang],
            'if_id_true' => $group_field[if_id_true]
        ], $atts);

/* I thought about this way, maybe it's not a rally, but maybe it will give you an idea to help me a little
 *   return $attributes['level_skill'];
 *    return $attributes['lang'];
 */

    endif;
});
?>

editing:
After failed attempts, I preferred to build the design on top of HTML (previously it was built in the form of Elementor)

I have the following code:

<?php 
/*************************************
 * Returns the values ​​from a certain group of the post
 *************************************/
add_shortcode('group', function ($atts,$shortcode_twoo,$shortcode_three) {
    $group_field = get_field('info_global_course');
    if ($group_field):

        $attributes = shortcode_atts([
            'level_skill' => $group_field[level_skill],
            'lang' =>  $group_field[lang],
            'if_id_true' => $group_field[if_id_true]
        ], $atts);

        
    return '
    <div>
        <div class="level_skill border_bottom">
            <span class="box_left"><i aria-hidden="true" class="far fa-clock"></i> time </span> 
            <span class="box_right">'.$shortcode .'</span>
        </div>
        <div class="video border_bottom">
            <span class="box_left"><i aria-hidden="true" class="fas fa-video"></i> Study chapters </span> 
            <span class="box_right">'.$shortcode1.'</span>
        </div>
        <div class="studants border_bottom">
            <span class="box_left"><i aria-hidden="true" class="fas fa-user-graduate"></i> Registered students</span> 
            <span class="box_right">100</span>
        </div>
        <div class="level_skill border_bottom">
            <span class="box_left"><i aria-hidden="true" class="fab fa-superpowers"></i>  level skill</span> 
            <span class="box_right">מתקדמים</span>
        </div>
         <div class="lang border_bottom">
            <span class="box_left"><i aria-hidden="true" class="fas fa-globe"></i>language</span> 
            <span class="box_right">עברית</span>
        </div>
        <div class="if_id_true border_bottom">
            <span class="box_left"><i aria-hidden="true" class="fas fa-sticky-note"></i> Diploma</span> 
            <span class="box_right">כן</span>
        </div>
    </div>
    ';
    endif;
});
?>

The goal is to insert into this array all the shortcodes I will create in the future

That’s why I did:

<?php
     $shortcode = do_shortcode('[time]');
     $shortcode1 = do_shortcode('[chapters]');
?>

or for HTML:
[time] [chapters]

But then I get stuck on the same issue of how I insert the various SHORTCODE variables

2

Answers


  1. Chosen as BEST ANSWER

    Many thanks to everyone who tried to help I found a nice method to combine all the shortcodes into one shortcode

    <?php 
        /********************
         * Consolidation of shortcodes
         **********************/
        add_shortcode('total_topics_chapters_group',function() {
            $output = '';
            $output .= do_shortcode('[time]');
            $output .= do_shortcode('[chapters]');
            $output .= do_shortcode('[group]');
            return $output;
        });
    ?>
    

  2. Short Code Definition Class (Create the file with desired name and add the code in to the file.

    class MyPlugin_ShortCodes
    {
    
      
        /**
         * Register the shortcodes for the public-facing side of the site.
         *
         * @since    1.0.0
         */
        public function register_shortcodes()
        {
            add_shortcode('group', array($this, 'shortcode'));
        }
    
        public function shortcode($attrs, $content = "", $shortcode = "my-plugin")
        {
            $allAttrs = $attrs;
            $attrs = shortcode_atts(array(
                'type' => null,
                'id' => null,
                'code' => null,            
            ), $attrs, $shortcode);
    
            if ($attrs["type"] != null) {
                try {
                    $f = 'do__' . $attrs["type"];
    
                    if (method_exists($this, $f))
                        return $this->$f($attrs, $content, $allAttrs);
    
                    return '[The "type" attribute did not match any known shortcode type. Found: ' . $attrs["type"] . ']';
                } catch (Exception $ex) {
                    return json_encode($ex->getMessage());
                }
            }
    
            return "Don't Know what to process!";
        }
    
        function do__skill($attrs, $content, $allAttrs)
        {
    
            $html = "Put your skills level here";
            
            return $html;
        }
    
        function do__lang($attrs, $content, $allAttrs)
        {
    
            $html = "Do Whateaver you want";
            
            return $html;
        }
    }
    

    Now Add the below lines to your plugin definition file

    $plugin_shortcodes = new MyPlugin_ShortCodes();
    $this->loader->add_action('init', $plugin_shortcodes, 'register_shortcodes');
    

    Shortcodes could be called like…

    do_shortcode('[group type="skill" id="{$ID}"]');
    do_shortcode('[group type="lang" id="{$ID}" code="en"]');
    do_shortcode('[group type="xxxx"');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search