Name Directory Shortcodes

Below, you’ll find a detailed guide on how to add the Name Directory Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Name Directory Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Name Directory Plugin and the shortcodes it provides:

Plugin Icon
Name Directory

"Name Directory is a dynamic WordPress plugin that provides a unique platform for building an interactive and searchable directory. Enhance your site with this user-friendly tool."

★★★★☆ (76) Active Installs: 3000+ Tested with: 6.4 PHP Version: 5.3
Included Shortcodes:
  • [namedirectory]
  • [namedirectory_random]
  • [namedirectory_single]

Name Directory [namedirectory] Shortcode

The Name Directory shortcode is used to display a name directory on a page. It extracts attributes, filters names based on provided criteria, and generates a directory with search and submit form functionalities. It also handles character indexing and column splitting for easy reading.

Shortcode: [namedirectory]

Parameters

Here is a list of all possible namedirectory shortcode parameters and attributes:

  • dir – Specifies the directory to be displayed
  • start_with – Sets the initial character filter for the directory

Examples and Usage

Basic example – Display a name directory by referencing its ID.

[namedirectory dir=1]

Advanced examples

Display a name directory by referencing its ID and set the directory to start with a specific character.

[namedirectory dir=1 start_with="A"]

Use the shortcode to display a name directory by referencing its ID and show the latest entries.

[namedirectory dir=1 name_directory_startswith="latest"]

PHP Function Code

In case you have difficulties debugging what causing issues with [namedirectory] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('namedirectory', 'name_directory_show_directory');

Shortcode PHP function:

                    function name_directory_show_directory($attributes)
{
    $dir = null;
    $show_all_link = '';
    $show_latest_link = '';
    $jump_location = '';
    extract(shortcode_atts(
        array('dir' => '1'),
        $attributes
    ));

    $name_filter = array();
    if(! empty($_GET['name_directory_startswith']) && $_GET['name_directory_startswith'] == "latest")
    {
        $name_filter['character'] = "latest";
    }
    else if(isset($_GET['name_directory_startswith']) && isset($_GET['dir']) && ($_GET['dir'] == $dir))
    {
        $name_filter['character'] = name_directory_get_first_char($_GET['name_directory_startswith']);
    }
    else if(! empty($attributes['start_with']) && empty($_GET['name-directory-search-value']))
    {
        $name_filter['character'] = $attributes['start_with'];
    }

    $str_all = __('All', 'name-directory');
    $str_latest = __('Latest', 'name-directory');
    $highlight_search_term = false;
    $search_value = '';

    $letter_url = name_directory_make_plugin_url('name_directory_startswith', 'name-directory-search-value', $dir);
    $directory = name_directory_get_directory_properties($dir);
    if($directory === null)
    {
        echo sprintf(__('Error: Name Directory #%d does not exist (anymore). If you are the webmaster, please change the shortcode.', 'name-directory'), $dir);
        return false;
    }

    if(! empty($_GET['name-directory-search-value']) && ! empty($_GET['dir']) && $_GET['dir'] == $dir)
    {
        $search_value = htmlspecialchars($_GET['name-directory-search-value']);
        $name_filter['containing'] = $search_value;
        if(! empty($directory['search_highlight']))
        {
            $highlight_search_term = true;
        }
    }

    $names = name_directory_get_directory_names($directory, $name_filter);
    $num_names = count($names);

    if(isset($_GET['show_submitform']))
    {
        return name_directory_show_submit_form($dir, name_directory_make_plugin_url('','show_submitform', $dir));
    }

    ob_start();

    if(! empty($directory['jump_to_search_results']))
    {
        $jump_location = "#name_directory_position" . $dir;
    }

    echo "<a name='name_directory_position" . $dir . "'></a>";

    if(! empty($directory['show_title']))
    {
        echo "<h3 class='name_directory_title'>" . $directory['name'] . "</h3>";
    }

    if(! empty($directory['show_all_names_on_index']))
    {
        $show_all_link = '<a class="name_directory_startswith" href="' . $letter_url . $jump_location . '">' . $str_all . '</a> |';
    }

    if(! empty($directory['nr_most_recent']))
    {
        $show_latest_link = ' <a class="name_directory_startswith" href="' . $letter_url . 'latest' . $jump_location . '">' . $str_latest . '</a> |';
    }

    /* Prepare and print the index-letters */
    echo '<div class="name_directory_index">';
    echo $show_all_link;
    echo $show_latest_link;

    $index_letters = range('A', 'Z');
    array_unshift($index_letters, '#');
    $starting_letters = name_directory_get_directory_start_characters($dir);

    /* User does not want to show all the index characters */
    if(empty($directory['show_all_index_letters']))
    {
        $index_letters = $starting_letters;
    }

    foreach($index_letters as $index_letter)
    {
        $extra_classes = '';
        if(! empty($name_filter['character']) && $name_filter['character'] == $index_letter)
        {
            $extra_classes .= ' name_directory_active';
        }

        if(! in_array($index_letter, $starting_letters))
        {
            $extra_classes .= ' name_directory_empty';
        }

        echo ' <a class="name_directory_startswith ' . $extra_classes . '" href="' . $letter_url . urlencode($index_letter) . $jump_location . '">' . strtoupper($index_letter) . '</a> ';
    }

    if(! empty($directory['show_submit_form']))
    {
        if(empty($directory['name_term_singular']))
        {
            $submit_string = __('Submit a name', 'name-directory');
        }
        else
        {
            $submit_string =  sprintf(__('Submit a %s', 'name-directory'), $directory['name_term_singular']);
        }

        echo " | <a href='" . name_directory_make_plugin_url('','name_directory_startswith', $dir) . "&show_submitform=true'>" . $submit_string . "</a>";
    }

    if(! empty($directory['show_search_form']))
    {
        $parsed_url = parse_url($_SERVER['REQUEST_URI']);
        $search_get_url = array();
        if(! empty($parsed_url['query']))
        {
            parse_str($parsed_url['query'], $search_get_url);
        }
        unset($search_get_url['name-directory-search-value']);

        echo "<br />";
        echo "<form role='search' method='get' action='" . $jump_location . "'>";
        foreach($search_get_url as $key_name => $value)
        {
            if(in_array($key_name, ['name_directory_startswith', 'dir']) || is_array($key_name) || is_array($value))
            {
                continue;
            }
            echo "<input type='hidden' name='" . htmlspecialchars($key_name) . "' value='" . htmlspecialchars($value) . "' />";
        }
        echo "<input type='search' autocomplete='off' aria-description='" . __('Directory names below will match your searchcriteria after submit', 'name-directory') . "' name='name-directory-search-value' id='name-directory-search-input-box' placeholder='" . __('Search for...', 'name-directory') . "' />";
        echo "<input type='hidden' name='dir' value='" . (int)$directory['id'] . "' />";
        echo "<input type='submit' id='name-directory-search-input-button' value='" . __('Search', 'name-directory') . "' />";
        echo "</form>";
    }
    echo '</div>';

    echo '<div class="name_directory_total">';
    if(empty($name_filter['character']) && empty($search_value) && $directory['show_current_num_names'])
    {
        if(empty($directory['name_term'])) {
            echo sprintf(__('There are currently %d names in this directory', 'name-directory'), $num_names);
        } else {
            if( $num_names == 1 ) {
                echo sprintf(__('There is currently %d %s in this directory', 'name-directory'), $num_names, $directory['name_term_singular']);
            } else {
                echo sprintf(__('There are currently %d %s in this directory', 'name-directory'), $num_names, $directory['name_term']);
            }
        }
    }
    else if(empty($name_filter['character']) && ! empty($search_value))
    {
        if(empty($directory['name_term'])) {
            echo sprintf(__('There are %d names in this directory containing the search term %s.', 'name-directory'), $num_names, "<em>" . stripslashes($search_value) . "</em>");
        } else {
            if( $num_names == 1 ) {
                echo sprintf(__('There is currently %d %s in this directory containing the search term %s.', 'name-directory'), $num_names, $directory['name_term_singular'], "<em>" . stripslashes($search_value) . "</em>");
            } else {
                echo sprintf(__('There are currently %d %s in this directory containing the search term %s.', 'name-directory'), $num_names, $directory['name_term'], "<em>" . stripslashes($search_value) . "</em>");
            }
        }

        echo " <a href='" . get_permalink() . "'><small>" . __('Clear results', 'name-directory') . "</small></a>.<br />";
    }
    else if($directory['show_current_num_names'] && isset($name_filter['character']) && $name_filter['character'] == 'latest')
    {
        if(empty($directory['name_term'])) {
            echo sprintf(__('Showing %d most recent names in this directory', 'name-directory'), $num_names);
        } else {
            echo sprintf(__('Showing %d most recent %s in this directory', 'name-directory'), $num_names, $directory['name_term']);
        }
    }
    else if($directory['show_current_num_names'])
    {
        if(empty($directory['name_term'])) {
            if( $num_names == 1 ) {
                echo sprintf(__('There is currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, strtolower(__('Name', 'name-directory')), $name_filter['character']);
            } else {
                echo sprintf(__('There are currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, __('names', 'name-directory'), $name_filter['character']);
            }
        } else {
            if( $num_names == 1 ) {
                echo sprintf(__('There is currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, $directory['name_term_singular'], $name_filter['character']);
            } else {
                echo sprintf(__('There are currently %d %s in this directory beginning with the letter %s.', 'name-directory'), $num_names, $directory['name_term'], $name_filter['character']);
            }
        }
    }
    echo  '</div>';

    echo '<div class="name_directory_names">';
    if($num_names === 0 && empty($search_value))
    {
        echo '<p class="name_directory_entry_message">' . __('There are no entries in this directory at the moment', 'name-directory') . '</p>';
    }
    else if(isset($directory['show_all_names_on_index']) && $directory['show_all_names_on_index'] != 1 && empty($name_filter))
    {
        if($directory['show_index_instructions'])
        {
            echo '<p class="name_directory_entry_message">' . __('Please select a letter from the index (above) to see entries', 'name-directory') . '</p>';
        }
    }
    else
    {
        $split_at = null;
        if(! empty($directory['nr_columns']) && $directory['nr_columns'] > 1)
        {
            $split_at = round($num_names/$directory['nr_columns'])+1;
        }

        echo '<div class="name_directory_column name_directory_nr' . (int)$directory['nr_columns'] . '">';

        $i = 1;
        $split_i = 1;
        $this_letter = '---';
        foreach($names as $entry)
        {
            /* Show the header of the next starting letter */
            if(! empty($directory['show_character_header']))
            {
                if ($entry['letter'] !== $this_letter) {
                    $this_letter = $entry['letter'];

                    echo '<div class="name_directory_character_header">' . $this_letter . '</div>';
                    if(! empty($directory['show_line_between_names']))
                    {
                        echo '<hr />';
                    }
                }
            }

            name_directory_render_namebox($entry, $directory);

            if(! empty($directory['show_line_between_names']) && $num_names != $i)
            {
                echo '<hr />';
            }

            $split_i++;
            $i++;

            if($split_at == $split_i)
            {
                echo '</div><div class="name_directory_column name_directory_nr' . (int)$directory['nr_columns'] . '">';
                $split_i = 1;
            }
        }
        echo '</div>';
    }
    echo '</div>';

    if(! empty($directory['nr_columns']) && $directory['nr_columns'] > 1)
    {
        echo '<div class="name_directory_column_clear"></div>';
    }

    if(! empty($directory['show_submit_form']))
    {
        echo "<br /><br />
              <a href='" . name_directory_make_plugin_url('','name_directory_startswith', $dir) . "&show_submitform=true' 
                    class='name_directory_submit_bottom_link'>" . $submit_string . "</a>";
    }

    /** Sad to print it like this, but this is needed for translating the show more/less buttons */
    echo "<style>
        .name_directory_readmore_state ~ .name_directory_readmore_trigger:before {
            content: '... " . __('Show more', 'name-directory') . "';
        }
       
        .name_directory_readmore_state:checked ~ .name_directory_readmore_trigger:before {
            content: '" . __('Show less', 'name-directory') . "';
        }
        </style>";

    if($highlight_search_term == true)
    {

        wp_enqueue_script( 'name-directory-highlight', 'https://cdn.jsdelivr.net/mark.js/8.6.0/mark.min.js', array() );
        wp_add_inline_script( 'name-directory-highlight', 'var markInstance = new Mark(document.querySelector(".name_directory_names"));
            markInstance.mark("' . $search_value . '");');
    }

	return ob_get_clean();
}
                    

Code file location:

name-directory/name-directory/shortcode.php

Name Directory [namedirectory_random] Shortcode

The Name Directory plugin shortcode, ‘namedirectory_random’, displays a random entry from a specified directory. Upon execution, it retrieves directory properties and checks for existence. If no directory is found, an error message is displayed. If the directory exists but is empty, a notification is shown. If the directory has entries, a random entry is selected and displayed within a ‘name_directory_random_name’ div.

Shortcode: [namedirectory_random]

Parameters

Here is a list of all possible namedirectory_random shortcode parameters and attributes:

  • dir – Specifies the ID of the directory to display a random name from.

Examples and Usage

Basic example – Displays a random name from the default directory (directory id 1).

[namedirectory_random /]

Advanced examples

Displays a random name from a specified directory. Replace ‘2’ with the id of your desired directory.

[namedirectory_random dir=2 /]

Multiple shortcodes can also be used on the same page to display random names from different directories. For instance, to display random names from directories with id ‘2’ and ‘3’, you can use the following shortcodes:

[namedirectory_random dir=2 /]
[namedirectory_random dir=3 /]

Please note that the ‘dir’ attribute corresponds to the id of the directory from which the names are to be displayed. If no ‘dir’ attribute is provided, the shortcode will default to directory id ‘1’. If the specified directory does not exist, an error message will be displayed.

PHP Function Code

In case you have difficulties debugging what causing issues with [namedirectory_random] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('namedirectory_random', 'name_directory_show_random');

Shortcode PHP function:

                    function name_directory_show_random($attributes)
{
    $dir = null;
    extract(shortcode_atts(
        array('dir' => '1'),
        $attributes
    ));

    $directory = name_directory_get_directory_properties($dir);
    if($directory === null)
    {
        echo sprintf(__('Error: Name Directory #%d does not exist (anymore). If you are the webmaster, please change the shortcode.', 'name-directory'), $dir);
        return false;
    }

    $names = name_directory_get_directory_names($directory);

    if (! count($names))
    {
        echo __('There are no entries in this directory at the moment', 'name-directory');
    }

    $entry = $names[array_rand($names)];

    ob_start();

    echo '<div class="name_directory_random_name">';
    name_directory_render_namebox($entry, $directory);
    echo '</div>';

    return ob_get_clean();

}
                    

Code file location:

name-directory/name-directory/shortcode.php

Name Directory [namedirectory_single] Shortcode

The Name Directory shortcode is designed to display a single name entry from the directory. It extracts the ‘id’ attribute from the shortcode, fetches the name entry linked to this ‘id’, and displays it within a styled div. The shortcode is flexible, allowing customization of the displayed name based on the directory properties.

Shortcode: [namedirectory_single]

Parameters

Here is a list of all possible namedirectory_single shortcode parameters and attributes:

  • id – Unique identifier to retrieve a specific name entry

Examples and Usage

Basic example – A simple usage of the shortcode to display a specific directory name based on its ID. The ID is set to 1 by default.

[namedirectory_single id=1 /]

Advanced examples

Displaying a specific directory name by using the shortcode with different ID parameters. This is useful when you want to display different directory names on different parts of your website.

[namedirectory_single id=2 /]

Displaying multiple directory names by using the shortcode multiple times with different ID parameters. This is useful when you want to display a list of specific directory names on your website.

[namedirectory_single id=3 /]
[namedirectory_single id=4 /]
[namedirectory_single id=5 /]

PHP Function Code

In case you have difficulties debugging what causing issues with [namedirectory_single] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('namedirectory_single', 'name_directory_show_single_name');

Shortcode PHP function:

                    function name_directory_show_single_name($attributes)
{
    $id = null;
    extract(shortcode_atts(
        array('id' => '1'),
        $attributes
    ));

    $name_entry = name_directory_get_single_name($id);
    $directory = name_directory_get_directory_properties($name_entry['directory']);

    ob_start();

    echo '<div class="name_directory_random_name">';
    name_directory_render_namebox($name_entry, $directory);
    echo '</div>';

    return ob_get_clean();
}
                    

Code file location:

name-directory/name-directory/shortcode.php

Conclusion

Now that you’ve learned how to embed the Name Directory Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *