Home > Net >  shortcode - within a shortcode WP PHP HTML
shortcode - within a shortcode WP PHP HTML

Time:01-22

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 >
            <span ><i aria-hidden="true" ></i> time </span> 
            <span >'.$shortcode .'</span>
        </div>
        <div >
            <span ><i aria-hidden="true" ></i> Study chapters </span> 
            <span >'.$shortcode1.'</span>
        </div>
        <div >
            <span ><i aria-hidden="true" ></i> Registered students</span> 
            <span >100</span>
        </div>
        <div >
            <span ><i aria-hidden="true" ></i>  level skill</span> 
            <span >מתקדמים</span>
        </div>
         <div >
            <span ><i aria-hidden="true" ></i>language</span> 
            <span >עברית</span>
        </div>
        <div >
            <span ><i aria-hidden="true" ></i> Diploma</span> 
            <span >כן</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

CodePudding user response:

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"');

CodePudding user response:

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;
    });
?>
  • Related