0

On one of my sites, I use Formidable Forms. I need monthly numbers (simple counts) on submissions for these forms. My current solution is to use this shortcode built-in from Formidable:

[frm-stats id=862 type=count created_at_greater_than="2023-12-31" created_at_less_than="2024-2-1" ] 

This shortcode would give you the number of form submissions for January 2024

The problem is that I need the prior twelve months to be shown, so this is repeated 12 times. And then I do this for multiple forms, so I've got about a hundred of these shortcodes so far and that number is slowly growing.

These must be updated manually, so every year I have to spend way too much time updating all these dates in these shortcodes. I'd like to find a way to populate the created_at_greater_than and created_at_less_than parameters automatically.

Could someone help me figure out a way to put some sort of variable into this shortcode, or possibly point me in the direction to get started making my own custom version of this shortcode that suits my needs?

I don't mind writing php for this, I just honestly don't know where to start since this is an existing plugin I'd be interacting with, and I'm hoping some folks in the community with more experience can help guide me here.

0

    2 Answers 2

    1

    you can generate shortcodes and launch them with this function : https://developer.wordpress.org/reference/functions/do_shortcode/

    so you can do that in your plugin :

    add_shortcode("MY_PLUGIN__forms_with_dates", function ($atts, $content, $tag) { $atts = shortcode_atts([ "months" => 11, "id_form" => NULL, ], $atts, $tag); if (!isset($atts["id_form"])) { return "shortcode $tag : the argument id_form is missing."; } $shortcodes = ""; $today = current_datetime()->setTime(12, 0, 0, 0); $year_today = wp_date("Y"); $month_today = wp_date("m"); foreach (range(0, $atts["months"]) as $months_diff) { $first_day = $today->setDate($year_today, $month_today - $months_diff, 1); $last_day = $today->setDate($year_today, $month_today - $months_diff + 1, 0); $first_day_shortcode = wp_date("Y-m-d", $first_day->getTimestamp()); $last_day_shortcode = wp_date("Y-m-d", $last_day->getTimestamp()); $shortcodes .= "[frm-stats"; $shortcodes .= " id=$atts[id_form]"; $shortcodes .= " type=count"; $shortcodes .= " created_at_greater_than=\"$first_day_shortcode\""; $shortcodes .= " created_at_less_than=\"$last_day_shortcode\""; $shortcodes .= "]"; } return do_shortcode($shortcodes); }); 

    with this new shortcode, you can for example use
    [MY_PLUGIN__forms_with_dates id_form=862 months=3]
    and the result will be equivalent to this :

    [frm-stats id=862 type=count created_at_greater_than="2024-08-01" created_at_less_than="2024-08-31"] [frm-stats id=862 type=count created_at_greater_than="2024-07-01" created_at_less_than="2024-07-31"] [frm-stats id=862 type=count created_at_greater_than="2024-06-01" created_at_less_than="2024-06-30"] [frm-stats id=862 type=count created_at_greater_than="2024-05-01" created_at_less_than="2024-05-31"] 
      0

      I figured it out. I ended up making a shortcode to fill in the plugin's shortcode based on a weird fiscal year (July-June) offset system and the strings that the shortcode is expecting as atts able to be passed.

      I'm not great at explaining, but you can see my code below.

      function quarterly_frm_graph( $atts ) { // Attributes $atts = shortcode_atts( array( 'fields' => '', 'filter_field' => '', 'filter_field_value' => '', 'type' => '', 'data_type' => '', 'height' => 'auto', 'width' => '100%', 'quarter_offset' => 0, ), $atts ); // Set variables that are provided in the shortcode attributes that do not need any further processing $fields = "fields=\"${atts['fields']}\""; $type = "type=\"${atts['type']}\""; $data_type = "data_type=\"${atts['data_type']}\""; $height = "height=\"${atts['height']}\""; $width = "width=\"${atts['width']}\""; $offset = $atts['quarter_offset']; // Set the $filter variable to empty if filter_field attribute is empty, else set it to whatever was provided in the attributes if (empty($atts['filter_field'])) { $filter = ''; } else { // Check to see if filter_field_value is empty if (empty($atts['filter_field_value'])) { return "It would appear `filter_field` has a value, but `filter_field_value` does not, please provide a value to filter by."; } else { $filter = "${atts['filter_field']}=\"${atts['filter_field_value']}\""; } } // Logic for calculating start and end dates for a quarter // Variable setting $now = strtotime('now'); $fiscal_year_start_month = 7; //July // Calculate fiscal year based on year and current month $current_fiscal_year = date('Y', $now); if ((date('m', $now) < $fiscal_year_start_month)) { $current_fiscal_year--; } // Calculate current calendar quarter and fiscal quarter based on current month $current_quarter = floor((date('m', $now) - 1) / 3) + 1; if (($current_quarter == 1) || ($current_quarter == 2)) { $current_fiscal_quarter = ($current_quarter + 2); } else { $current_fiscal_quarter = ($current_quarter - 2); } // Calulate fiscal quarter $fiscal_quarter = ($current_fiscal_quarter + $offset - 1) % 4; if ($fiscal_quarter < 0) { $fiscal_quarter += 4; } $fiscal_quarter++; // Calculate start and end months for the desired quarter if ($fiscal_quarter == 1) { $start_month = 7; } else if ($fiscal_quarter == 2) { $start_month = 10; } else if ($fiscal_quarter == 3) { $start_month = 1; } else if ($fiscal_quarter == 4) { $start_month = 4; } else {echo 'garbage';} // Adjust the fiscal year based on the provided offset parameter $fiscal_year = $current_fiscal_year + floor($offset / 4); $year = $fiscal_year; if ($fiscal_quarter > 2) { $year++; } $start_date_unix = strtotime("${year}-${start_month}-01 00:00:00"); $start_date = date('Y-m-d H:i:s', $start_date_unix); $end_date = date('Y-m-d H:i:s', strtotime("+3 months -1 day 23:59:59", $start_date_unix)); $start = "created_at_greater_than=\"${start_date}\""; $end = "created_at_less_than=\"${end_date}\""; //echo $start.'</br>'; //echo $end; // Build and run the variable-filled shortcode with $filter if it is not empty if (empty($filter)) { // Use current month and year $formidable_shortcode = "[frm-graph $fields $type $data_type $height $width $start $end]"; } else { $formidable_shortcode = "[frm-graph $fields $filter $type $data_type $height $width $start $end]"; } $do_formidable_shortcode = do_shortcode($formidable_shortcode); return $do_formidable_shortcode; } add_shortcode( 'q-frm-graph', 'quarterly_frm_graph' ); 

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.