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

1 Answer 1

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"]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.