Multiple shortcode is not working in foreach
-
Hello, I am creating multiple shortcode with foreach, but always it returns me the last shortcode, any hints? Thanks.
Like I 2 shorcode tag name: cld_form_id-50874d91-05c7-45e4-957c-86b360312861 and cld_form_id-50874d91-05c7-45e4-957c-86b360312862
In a page I put the first one as [cld_form_id-50874d91-05c7-45e4-957c-86b360312861] which has 3 fields
but it is printing two fields where 2nd form have two fields.
Code:<?php /** * The shorcode handler class */ class Shortcode_Handler { private $field_array; private $form_id; function __construct() { add_action('init', [$this, 'cld_add_custom_shortcode']); } public function cld_add_custom_shortcode() { global $wpdb; $table_name = $wpdb->prefix . 'options'; $results = $wpdb->get_results("SELECT * FROM $table_name WHERE option_name LIKE '%cld_form_id-%'"); foreach ($results as $value) { $shorcode_tag_name = $value->option_name; $this->form_id = $value->option_name; $this->field_array = maybe_unserialize($value->option_value); add_shortcode($shorcode_tag_name, [$this, 'cld_generate_form']); } } public function cld_generate_form() { // Get form fields $fields = $this->field_array; $form_id = $this->form_id; $form = '<form id="'.$form_id.'" method="post">'; foreach ($fields as $value) { $form .= '<input name="'.$value.'" value="" />'; } $form .= '<input type="submit" name="submit">'; $form .= '</form>'; return $form; } }Any hints?
Thanks.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Multiple shortcode is not working in foreach’ is closed to new replies.