WordPress.org

Ready to get started?Download WordPress

Forums

Need help creating a shortcode (8 posts)

  1. marcbesson
    Member
    Posted 1 year ago #

    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

  2. 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 ) );
    
    }
  3. marcbesson
    Member
    Posted 1 year ago #

    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

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    add another bracket before the semicolon:

    ), $atts ) );
  5. marcbesson
    Member
    Posted 1 year ago #

    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 ?

  6. Rajan V
    Member
    Posted 1 year ago #

    Hi.,

    Put the code on function.php in your theme file

    Thanks

  7. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

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

    add_filter('widget_text', 'do_shortcode');
  8. marcbesson
    Member
    Posted 1 year ago #

    Thank you guys, it works perfectly !

Topic Closed

This topic has been closed to new replies.

About this Topic