0

With my first plugin, I had some issues that got solved with the help of users here. Now I am facing another issue: I want to be able to output the plugin content (drop-down select form) twice on the same page. However, only the first time it is shown and the second is blank.

I've read in some topics here and there, that I cannot have the same ids for content on the same page. So what I've tried is:

  1. Added option for a second shortcode that points to another PHP file, where the content of the second file is exactly the same as the original one. Both shortcodes work despite the fact, that there are duplicate ids in the content

  2. Tried with both shortcodes to point to the same PHP file - again only the first one worked

  3. Tried to make the one shortcode with arguments - again only the first one works.

  4. As the select fields are dependent on each other and my ajax needs the elements IDs and I know I can fetch them by class, I've tried to remove ALL the IDs from the HTML output, but yet again only the first placement worked.

So, to summarize, having 2 shortcodes is not a big pain, but I prefer to be able to use one shortcode and be able to place it twice (or more) on the same page.

Any ideas on how to do this?

Here is some code:

The HTML:

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

            <form method="post">

                <div class="row">
                    <select class="hpcdd-select" name="lvl1" id="lvl1">
                        <option value=""><?php echo __('Select Main Category', 'hpcdd') ?></option>
                        <?php
                        foreach ($this->getTopLevelCategories() as $category) {
                            print '<option value="' . $category->term_id . '">' . $category->name . ' (' . $category->count . ')</option>';
                        }
                        ?>
                    </select>
                </div>

                <div class="row">
                    <select class="hpcdd-select" name="lvl2" id="lvl2">
                        <option value=""><?php echo __('Select First Subcategory', 'hpcdd') ?></option>
                    </select>
                </div>

                <?php if (get_option('hpcdd_levels_setting') == 3 || get_option('hpcdd_levels_setting') == 4) { ?>
                    <div class="row">
                        <select class="hpcdd-select" name="lvl3" id="lvl3">
                            <option value=""><?php echo __('Select Second Subcategory', 'hpcdd') ?></option>
                        </select>
                    </div>
                <?php } ?>

                <?php if (get_option('hpcdd_levels_setting') == 4) { ?>
                    <div class="row">
                        <select class="hpcdd-select" name="lvl4" id="lvl4">
                            <option value=""><?php echo __('Select Third Subcategory', 'hpcdd') ?></option>
                        </select>
                    </div>
                <?php } ?>

                <div class="hpcdd-button">
                    <button type="submit" name="submit" title="<?php echo __('Show Products', 'hpcdd') ?>"
                            class="button hpcdd-submit">
                        <span><?php echo __('Show Products', 'hpcdd') ?></span>
                    </button>
                </div>

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

            </form>

        </div>
    </div>

The class file:

private function define_public_hooks()
{

    // Some code 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 function hpcdd_shortcodes_init()
{
    add_shortcode('hpcdd_show_selector', array($this, 'hpcdd_shortcode'));
}

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

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

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

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

    ob_start();

    $this->toHtml();

    return ob_get_clean();
}

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

    ob_start();

    $this->toHtml();

    return ob_get_clean();
}

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

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

    ob_start();

    $this->toHtmlMobile();

    return ob_get_clean();
}


/**
 *
 */
public function toHtmlMobile()
{
    include_once plugin_dir_path(dirname(__FILE__)) . 'public/partials/hpcdd-public-display-mobile.php';
}
3
  • 3
    "only the first time it is shown and the second is blank" - did you intentionally use include_once and have you tried using include instead?
    – Sally CJ
    Jun 22, 2022 at 7:12
  • F*ck. I'm stupid! Please delete this, it's just too embarrassing. :D
    – milenmk
    Jun 22, 2022 at 7:54
  • Well, it happens, even to me, sometimes.. and I can't delete your question, so you should do that yourself.. :)
    – Sally CJ
    Jun 22, 2022 at 8:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.