Support » Fixing WordPress » Need help creating a shortcode

  • marcbesson

    (@marcbesson)


    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 (http://codex.wordpress.org/Function_Reference/add_shortcode) 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.

    Marc

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

    (@c3mdigital)

    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 ) );
    
    }
    marcbesson

    (@marcbesson)

    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

    Michael

    (@alchymyth)

    add another bracket before the semicolon:

    ), $atts ) );
    marcbesson

    (@marcbesson)

    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 ?

    Rajan V

    (@rajanit2000)

    Hi.,

    Put the code on function.php in your theme file

    Thanks

    Michael

    (@alchymyth)

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

    add_filter('widget_text', 'do_shortcode');
    marcbesson

    (@marcbesson)

    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.