Wednesday, June 22, 2022
HomeWordPress DevelopmentCan't output plugin twice with shortcode

Can’t output plugin twice with shortcode


With my first plugin, I had some points that bought solved with the assistance of customers right here. Now I’m dealing with one other situation: I would like to have the ability to output the plugin content material (drop-down choose kind) twice on the identical web page. Nevertheless, solely the primary time it’s proven and the second is clean.

I’ve learn in some matters right here and there, that I can not have the identical ids for content material on the identical web page. So what I’ve tried is:

  1. Added possibility for a second shortcode that factors to a different PHP file, the place the content material of the second file is strictly the identical as the unique one. Each shortcodes work regardless of the very fact, that there are duplicate ids within the content material

  2. Tried with each shortcodes to level to the identical PHP file – once more solely the primary one labored

  3. Tried to make the one shortcode with arguments – once more solely the primary one works.

  4. Because the choose fields are depending on one another and my ajax wants the weather IDs and I do know I can fetch them by class, I’ve tried to take away ALL the IDs from the HTML output, however but once more solely the primary placement labored.

So, to summarize, having 2 shortcodes is just not an enormous ache, however I choose to have the ability to use one shortcode and be capable to place it twice (or extra) on the identical web page.

Any concepts on how to do that?

Right here is a few code:

The HTML:

<div class="hpcdd-selector-box" id="<?php echo $this->getWidgetId(); ?>">
        <div class="block-content" id="hpcdd-form">

            <kind technique="put up">

                <div class="row">
                    <choose class="hpcdd-select" title="lvl1" id="lvl1">
                        <possibility worth=""><?php echo __('Choose Most important Class', 'hpcdd') ?></possibility>
                        <?php
                        foreach ($this->getTopLevelCategories() as $class) {
                            print '<possibility worth="' . $category->term_id . '">' . $category->title . ' (' . $category->depend . ')</possibility>';
                        }
                        ?>
                    </choose>
                </div>

                <div class="row">
                    <choose class="hpcdd-select" title="lvl2" id="lvl2">
                        <possibility worth=""><?php echo __('Choose First Subcategory', 'hpcdd') ?></possibility>
                    </choose>
                </div>

                <?php if (get_option('hpcdd_levels_setting') == 3 || get_option('hpcdd_levels_setting') == 4) { ?>
                    <div class="row">
                        <choose class="hpcdd-select" title="lvl3" id="lvl3">
                            <possibility worth=""><?php echo __('Choose Second Subcategory', 'hpcdd') ?></possibility>
                        </choose>
                    </div>
                <?php } ?>

                <?php if (get_option('hpcdd_levels_setting') == 4) { ?>
                    <div class="row">
                        <choose class="hpcdd-select" title="lvl4" id="lvl4">
                            <possibility worth=""><?php echo __('Choose Third Subcategory', 'hpcdd') ?></possibility>
                        </choose>
                    </div>
                <?php } ?>

                <div class="hpcdd-button">
                    <button sort="submit" title="submit" title="<?php echo __('Present Merchandise', 'hpcdd') ?>"
                            class="button hpcdd-submit">
                        <span><?php echo __('Present Merchandise', 'hpcdd') ?></span>
                    </button>
                </div>

                <img id="loader" class="loader" src="<?php print plugins_url("https://wordpress.stackexchange.com/", __DIR__) . 'img/loader.gif' ?>" hidden/>

            </kind>

        </div>
    </div>

The category file:

personal perform define_public_hooks()
{

    // Some code right here //


    add_shortcode('hpcdd_category_selector', array($this, 'show_selector_by_shortcode'));
    add_shortcode('hpcdd_category_selector_mobile', array($this, 'show_selector_by_shortcode_mobile'));

    add_action('init', array($this, 'hpcdd_shortcodes_init'));

}

//Code to create shortcode with arguments
/**
 * Central location to create all shortcodes.
 */
public perform hpcdd_shortcodes_init()
{
    add_shortcode('hpcdd_show_selector', array($this, 'hpcdd_shortcode'));
}

public perform hpcdd_shortcode($atts = [])
{
    // normalize attribute keys, lowercase
    $atts = array_change_key_case((array)$atts, CASE_LOWER);

    // override default attributes with person attributes
    $hpcdd_atts = shortcode_atts(
        array(
            'title' => 'hpcddtest',
            'id' => '',
        ), $atts
    );

    return $this->getContent($hpcdd_atts['name'], $hpcdd_atts['id']);
}

public perform getContent($title, $id)
{
    $this->setWidgetId($title . '_' . $id);

    ob_start();

    $this->toHtml();

    return ob_get_clean();
}

//Code for shortcode [hpcdd_category_selector]
public perform show_selector_by_shortcode()
{
    $this->setWidgetId(rand(1, 99999));

    ob_start();

    $this->toHtml();

    return ob_get_clean();
}

/**
 *
 */
public perform toHtml()
{
    include_once plugin_dir_path(dirname(__FILE__)) . 'public/partials/hpcdd-public-display.php';
}

//Code for shortcode [hpcdd_category_selector_mobile]
public perform show_selector_by_shortcode_mobile()
{
    $this->setWidgetId(rand(1, 99999));

    ob_start();

    $this->toHtmlMobile();

    return ob_get_clean();
}


/**
 *
 */
public perform toHtmlMobile()
{
    include_once plugin_dir_path(dirname(__FILE__)) . 'public/partials/hpcdd-public-display-mobile.php';
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments