Support » Fixing WordPress » Need help creating a shortcode

  • Hi everyone,

    I have the Formidable plugin. I want to display the number of unique users that have filled my form, and this option is not included in the plugin.
    Here is a php code that might help me :

    global $wpdb, $frmdb;
    $users = $wpdb->get_var("SELECT COUNT(DISTINCT user_id) FROM $frmdb->entries WHERE form_id=5");

    I was told to do a shortcode with that code, however, even with the codex ( it’s really difficulte for me to understand what I have to do :/.

    I would like to be able to put [observateur] in a text-widget and I would like [observateur] to give the number of distinct user_id that have filled my form.

    Do you know how to help me ?

    Thank you in advance for any help you will offer me.


Viewing 7 replies - 1 through 7 (of 7 total)
  • Chris Olbekson


    Level 12 Bug Squasher & Forum Moderator

    This will let you pass the form id into the shortcode incase you want to use it again with a different form. usage: [observateur form=5]. You can also leave out the form argument and it will default to 5.

    add_shortcode( 'observateur', 'marc_get_user_shortcode' );
    function marc_get_user_shortcode( $atts ) {
        global $wpdb, $frmdb;
        extract( shortcode_atts( array(
                      'form' => 5,
                       ), $atts );
        return $wpdb->get_var(  $wpdb->prepare( "SELECT COUNT(DISTINCT user_id)
                   FROM $frmdb->entries WHERE form_id = %d", $form ) );

    Waouh, thank you for this quick answer.

    If I paste this code at the end of my shortcodes.php (in wp-include, right ?) I have this error :

    Parse error: syntax error, unexpected ‘;’ in /home/poupoudo/public_html/observatoire/wp-includes/shortcodes.php on line 340

    It refers to this line :

    ), $atts );

    I just have to delete “;” ?

    Edit : same error if i paste it into theme-shortcodes.php of my theme
    Parse error: syntax error, unexpected ‘;’ in /home/poupoudo/public_html/observatoire/wp-content/themes/graphene/includes/theme-shortcodes.php on line 14
    The error is at the same line



    Forum Moderator

    add another bracket before the semicolon:

    ), $atts ) );

    Thank you !

    No more error message but it seems that the shortcode is not working.
    If I write [observateur form=8] in a text widget, It just add “[observateur form=8]” on my website in the widget area, and not the number of users that have filled my form.

    The problem may come from the code I was given ?


    Put the code on function.php in your theme file




    Forum Moderator

    to have textwidgets execute shortcode, you need to add this to functions.php of your theme:

    add_filter('widget_text', 'do_shortcode');

    Thank you guys, it works perfectly !

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Need help creating a shortcode’ is closed to new replies.