Title: Shortcode for showing a random name?
Last modified: January 28, 2019

---

# Shortcode for showing a random name?

 *  Resolved [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * (@mastababa)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/)
 * How can I display a random name in the sidebar of my site? I’m thinking of a 
   shortcode along the lines of [name-dictionary dic=’1′ name=’random’].
 * Similarly, I would love to be able to include particular names in my site content.
   So, a shortcode like [name-dictionary dic=’1′ name=’somename’] or [name-dictionary
   dic=’1′ name=’12’] would work for that.
 * Does this exist?
 * If not, I’m assuming the dictionaries are added via a custom content type. What’s
   the content type? This, so that I can write my own shortcode for this.

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [Jeroen Peters](https://wordpress.org/support/users/jeroenpeters1986/)
 * (@jeroenpeters1986)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11139561)
 * Hi [@mastababa](https://wordpress.org/support/users/mastababa/) ,
 * at the moment those shortcodes do not exist. I could add it but at the moment
   I don’t have tons of time to develop.
 * The plugin is ordened in an old-fashion way and I have to tell you it uses it’s
   own two tables, and not custom content types (for now).
    I’m sorry for this.
 * Thank you for sharing your ideas! I might implement it in the future, but I don’t
   have the time in a short term. I do hope my answers helped so you could create
   your own.
 * Kind regards,
 * Jeroen Peters
 *  Thread Starter [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * (@mastababa)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11139632)
 * Understood.
 * On your next update, consider adding the following function to shortcode.php.
   It adds a shortcode to display a random name.
 * Use like this: [namedirectory_random dir=”2″]
 *     ```
       function name_directory_show_random($attributes) {
           $dir = null;
           extract(shortcode_atts(
               array('dir' => '1'),
               $attributes
           ));
   
           $name_filter = array();
           $directory = name_directory_get_directory_properties($dir);
           $names = name_directory_get_directory_names($directory, $name_filter);
           $num_names = count($names);
   
       	if ($num_names > 0) {
       		$entry = $names[array_rand($names)];
   
       	    ob_start();
   
       		echo '<div class="name_directory_random_name">';
               echo '<div class="name_directory_name_box">';
               echo '<a name="namedirectory_' . sanitize_html_class($entry['name']) . '"></a>';
               echo '<strong>' . htmlspecialchars($entry['name']) . '</strong>';
               if(! empty($directory['show_description']) && ! empty($entry['description']))
               {
                   $print_description = html_entity_decode(stripslashes($entry['description']));
   
                   /* This toggles the read more/less indicators, these need extra html */
                   if(! empty($directory['nr_words_description']))
                   {
                       $num_words = intval($directory['nr_words_description']);
                       $short_desc = name_directory_get_words($print_description, $num_words);
                       $print_description = str_replace($short_desc, "", $print_description);
                       if(! empty($print_description))
                       {
                           echo '<br /><div>
                             <input type="checkbox" class="name-directory-readmore-state" id="name-' . htmlspecialchars($entry['id']) . '" />
                             <span class="name-directory-readmore-wrap">' . $short_desc . ' <span class="name-directory-readmore-target">' . $print_description .'</span></span>
                             <label for="name-' . htmlspecialchars($entry['id']) . '" class="name-directory-readmore-trigger"></label>
                           </div>';
                       }
                       else
                       {
                           echo '<br /><div>' . $short_desc . '</div>';
                       }
   
                   }
                   else {
                       echo '<br /><div>' . $print_description . '</div>';
                   }
               }
       		if(! empty($directory['show_submitter_name']) && ! empty($entry['submitted_by']))
       		{
       			echo "<small>" . __('Submitted by:', 'name-directory') . " " . $entry['submitted_by'] . "</small>";
       		}
               echo '</div>';
       		echo '</div>';
   
       		return ob_get_clean();
       	}
   
       }
       add_shortcode('namedirectory_random', 'name_directory_show_random');
       ```
   
 * I noticed in another thread you just became a parent. Happy parenting! 🙂
 *  Plugin Author [Jeroen Peters](https://wordpress.org/support/users/jeroenpeters1986/)
 * (@jeroenpeters1986)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11151271)
 * Hi [@mastababa](https://wordpress.org/support/users/mastababa/) ,
 * thanks on the Congratulations and for the code. I will include it in v1.12, thank
   you!
 * Kind regards,
 * Jeroen Petes
 *  Thread Starter [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * (@mastababa)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11151279)
 * You’re welcome 🙂
 *  Thread Starter [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * (@mastababa)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11151344)
 * I see you pushed out a new release. I think I’m not the only one contributing
   to this release, right?
 * You might want to consider adding the contributors to the list of “Contributors&
   Developers” at the bottom of the main plugin page.
 *  Plugin Author [Jeroen Peters](https://wordpress.org/support/users/jeroenpeters1986/)
 * (@jeroenpeters1986)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11151902)
 * Hey [@mastababa](https://wordpress.org/support/users/mastababa/) ,
 * Thanks for the tip, I did put you Guys in the release notes, I have to check 
   where i can add you and Marin for the contributers part.
 * I Will take care of that 🙂
 * Kind regards,
 * Jeroen Peters
 *  Thread Starter [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * (@mastababa)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11152445)
 * Cool!
 *  Plugin Author [Jeroen Peters](https://wordpress.org/support/users/jeroenpeters1986/)
 * (@jeroenpeters1986)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11152561)
 * I have added you both, but your name does not seem to show up on my screen, your
   avatar does, so I am sure the name is spelled correctly.
    Did you do something
   in your profile?
 *  Thread Starter [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * (@mastababa)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11152808)
 * Thanks!
 * It’s a WordPress bug. My name also doesn’t show in the top right of this site
   when logged in. Even though my profile has the correct info.
 *  Thread Starter [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * (@mastababa)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11152815)
 * How about that! This suddenly resolved itself, after, what, years!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Shortcode for showing a random name?’ is closed to new replies.

 * ![](https://ps.w.org/name-directory/assets/icon-128x128.png?rev=1008185)
 * [Name Directory](https://wordpress.org/plugins/name-directory/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/name-directory/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/name-directory/)
 * [Active Topics](https://wordpress.org/support/plugin/name-directory/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/name-directory/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/name-directory/reviews/)

## Tags

 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 10 replies
 * 2 participants
 * Last reply from: [Babak Fakhamzadeh](https://wordpress.org/support/users/mastababa/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/shortcode-for-showing-a-random-name/#post-11152815)
 * Status: resolved