function manage_seo_title_tags()
{
    global $wpdb, $tabletags, $tablepost2tag, $install_directory, $wp_version;
    $search_value = '';
    $search_query_string = '';
    // Save Pages Form
    if (isset($_POST['action']) && ($_POST['action'] == 'pages' || $_POST['action'] == 'posts')) {
        if (function_exists('check_admin_referer')) {
            check_admin_referer('seo-title-tag-action_posts-form');
        }
        foreach ($_POST as $name => $value) {
            // Update Title Tag
            if (preg_match('/^tagtitle_(\\d+)$/', $name, $matches)) {
                $value = stripslashes(strip_tags($value));
                delete_post_meta($matches[1], get_option("custom_title_key"));
                add_post_meta($matches[1], get_option("custom_title_key"), $value);
            }
            // Update Slug
            if (preg_match('/^post_name_(\\d+)$/', $name, $matches)) {
                $postarr = get_post($matches[1], ARRAY_A);
                $old_post_name = $postarr['post_name'];
                $postarr['post_name'] = sanitize_title($value, $old_post_name);
                $postarr['post_category'] = array();
                $cats = get_the_category($postarr['ID']);
                if (is_array($cats)) {
                    foreach ($cats as $cat) {
                        $postarr['post_category'][] = $cat->term_id;
                    }
                }
                $tags_input = array();
                $tags = get_the_tags($postarr['ID']);
                if (is_array($tags)) {
                    foreach ($tags as $tag) {
                        $tags_input[] = $tag->name;
                    }
                }
                $postarr['tags_input'] = implode(', ', $tags_input);
                wp_insert_post($postarr);
            }
        }
        echo '<div class="updated"><p>The custom ' . ('pages' == $_POST['action'] ? 'page' : 'post') . ' titles have been updated.</p></div>';
        // Save Category and Tag Forms
    } elseif (isset($_POST['action']) && ($_POST['action'] == 'categories' || $_POST['action'] == 'tags')) {
        if (function_exists('check_admin_referer')) {
            check_admin_referer('seo-title-tag-action_taxonomy-form');
        }
        $singular = 'tags' == $_POST['action'] ? 'tag' : 'category';
        foreach ($_POST as $name => $value) {
            // Update Title Tag
            if (preg_match('/^title_(\\d+)$/', $name, $matches)) {
                $title = stripslashes(strip_tags($_POST['title_' . $matches[1]]));
                $title = $wpdb->escape($title);
                if (get_option("use_category_description_as_title")) {
                    $temp = $wpdb->get_row('SELECT term_id FROM ' . $wpdb->term_taxonomy . ' where term_id = ' . $matches[1]);
                    if ($temp->term_id == $matches[1]) {
                        $wpdb->query('UPDATE ' . $wpdb->term_taxonomy . ' SET description = \'' . $title . '\' where term_id = ' . $matches[1]);
                    }
                } else {
                    $table_name = $wpdb->prefix . 'seo_title_tag_' . $singular;
                    $temp = $wpdb->get_results('SELECT ' . $singular . '_id as term_id from ' . $table_name . ' WHERE ' . $singular . '_id = ' . $matches[1]);
                    if (isset($temp[1])) {
                        $wpdb->query('DELETE FROM ' . $table_name . ' WHERE ' . $singular . '_id = ' . $matches[1]);
                        unset($temp);
                    } elseif (isset($temp[0])) {
                        $temp = $temp[0];
                    }
                    if (isset($temp) && $temp->term_id == $matches[1] && !empty($title)) {
                        $wpdb->query('UPDATE ' . $table_name . ' SET title = \'' . $title . '\' WHERE ' . $singular . '_id = ' . $matches[1]);
                    } elseif (!empty($title)) {
                        $wpdb->query('INSERT INTO ' . $table_name . ' (' . $singular . '_id,title) values(\'' . $matches[1] . '\',\'' . $title . '\')');
                    } else {
                        $wpdb->query('DELETE FROM ' . $table_name . ' where ' . $singular . '_id = ' . $matches[1]);
                    }
                }
            }
        }
        echo '<div class="updated"><p>The custom ' . $singular . ' titles have been saved.</p></div>';
        // Save URLs Form
    } elseif (isset($_POST['action']) and $_POST['action'] == 'urls') {
        if (function_exists('check_admin_referer')) {
            check_admin_referer('seo-title-tag-action_urls-form');
        }
        $table_name = $wpdb->prefix . "seo_title_tag_url";
        foreach ($_POST as $name => $value) {
            // Update Title Tag
            if (preg_match('/^url_(\\d+)$/', $name, $matches)) {
                $url = stripslashes($value);
                $url = $wpdb->escape($url);
                $title = stripslashes(strip_tags($_POST['title_' . $matches[1]]));
                $title = $wpdb->escape($title);
                if (!empty($url) and !empty($title)) {
                    $wpdb->query('UPDATE ' . $table_name . ' SET url = \'' . $url . '\', title = \'' . $title . '\' WHERE id = ' . $matches[1]);
                } elseif (empty($url) and empty($title)) {
                    $wpdb->query('DELETE FROM ' . $table_name . ' WHERE id = ' . $matches[1]);
                }
            } elseif (preg_match('/^url_new_(\\d+)$/', $name, $matches)) {
                $url = stripslashes($value);
                $url = $wpdb->escape($url);
                $title = stripslashes(strip_tags($_POST['title_new_' . $matches[1]]));
                $title = $wpdb->escape($title);
                if (!empty($url) and !empty($title)) {
                    $wpdb->query('INSERT INTO ' . $table_name . ' (url,title) VALUES (\'' . $url . '\',\'' . $title . '\')');
                }
            }
        }
        echo '<div class="updated"><p>The custom URLs and URL titles have been saved.</p></div>';
        // Filter by Search Value
    } elseif (isset($_POST['search_value'])) {
        $search_value = stripslashes(strip_tags($_POST['search_value']));
    }
    // If no search value from POST check for value in GET
    if (!isset($_POST['search_value']) && isset($_GET['search_value'])) {
        $search_value = stripslashes(strip_tags($_GET['search_value']));
    }
    $title_tags_type = stripslashes(strip_tags($_GET['title_tags_type']));
    $page_no = intval($_GET['page_no']);
    $manage_elements_per_page = get_option("manage_elements_per_page");
    $element_count = 0;
    if (empty($title_tags_type)) {
        $title_tags_type = 'pages';
    }
    if (empty($manage_elements_per_page)) {
        $manage_elements_per_page = 15;
    }
    $_SERVER['QUERY_STRING'] = preg_replace('/&title_tags_type=[^&]+/', '', $_SERVER['QUERY_STRING']);
    $_SERVER['QUERY_STRING'] = preg_replace('/&page_no=[^&]+/', '', $_SERVER['QUERY_STRING']);
    $_SERVER['QUERY_STRING'] = preg_replace('/&search_value=[^&]*/', '', $_SERVER['QUERY_STRING']);
    $search_query_string = '&search_value=' . $search_value;
    if (!$page_no) {
        $page_no = 0;
    }
    ?>

    <div class="wrap">

    <form  id="posts-filter" action="" method="post">
    <h2>SEO Title Tags</h2>

    <p id="post-search">
        <label class="hidden" for="search_value">Search Title Tags:</label>
        <input type="text" id="search_value" name="search_value" value="<?php 
    if (isset($search_value)) {
        echo wp_specialchars($search_value, true);
    }
    ?>
" />
        <input type="submit" value="Search Title Tags" class="button" />

    </p>

    <div><a href="http://www.netconcepts.com"><img width="233" height="36" alt="visit netconcepts" align="right" src="<?php 
    echo get_option('siteurl');
    ?>
/wp-content/plugins/seo-title-tag/nclogo.jpg" /></a></div>
    <p><a href="options-general.php?page=seo-title-tag">Edit main SEO Title Tag plugin options &raquo;</a></p>

    <br class="clear" />

    </form>
    <?php 
    //do the nav menu items for the subsubmenu
    if (empty($_REQUEST['title_tags_type'])) {
        $_REQUEST['title_tags_type'] = 'pages';
    }
    echo '<ul id="subsubmenu">' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'pages') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=pages">Pages</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'posts') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=posts">Posts</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'categories') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=categories">Categories</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'tags') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=tags">Tags</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'urls') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=urls">URLs</a></li>' . "\n";
    echo '</ul>' . "\n";
    // Render Page and Post Tabs
    if ($title_tags_type == 'pages' || $title_tags_type == 'posts') {
        $post_type = substr($title_tags_type, 0, -1);
        // Database table uses singular version
        ?>
        <p>Use the form below to enter or update a custom <?php 
        echo $post_type;
        ?>
 title.<br /></p>
        <?php 
        if (empty($search_value)) {
            if ($page_no > 0) {
                $limit = ' LIMIT ' . $page_no * $manage_elements_per_page . ', ' . $manage_elements_per_page;
            } else {
                $limit = ' LIMIT ' . $manage_elements_per_page;
            }
            $posts = $wpdb->get_results('SELECT * FROM ' . $wpdb->posts . ' WHERE post_type = \'' . $post_type . '\' ORDER BY menu_order ASC' . ('posts' == $title_tags_type ? ', post_date DESC' : ', ID ASC') . $limit);
        } else {
            $posts = $wpdb->get_results('SELECT * FROM ' . $wpdb->posts . ' WHERE post_type = \'' . $post_type . '\' ORDER BY menu_order ASC' . ('posts' == $title_tags_type ? ', post_date DESC' : ', ID ASC'));
            $new_posts;
            foreach ($posts as $post) {
                if (isset($post->post_type) and $post->post_type != $post_type) {
                    continue;
                }
                if (empty($search_value)) {
                    // No search value, add all
                    $new_posts[] = $post;
                } else {
                    // Filter based on search value
                    if (preg_match('/' . $search_value . '/i', $post->post_title)) {
                        $new_posts[] = $post;
                    } else {
                        $post_custom = get_post_custom($post->ID);
                        if (preg_match('/' . $search_value . '/i', $post_custom[get_option("custom_title_key")][0]) || preg_match('/' . $search_value . '/i', $post->post_content) || preg_match('/' . $search_value . '/i', $post->post_excerpt)) {
                            $new_posts[] = $post;
                        }
                    }
                }
            }
            $posts = $new_posts;
            $element_count = count($posts);
            if ($element_count > $manage_elements_per_page and ($page_no != 'all' or empty($page_no))) {
                if ($page_no > 0) {
                    $posts = array_splice($posts, $page_no * $manage_elements_per_page);
                }
                $posts = array_slice($posts, 0, $manage_elements_per_page);
            }
        }
        if ($posts) {
            ?>
            <form name="posts-form" action="<?php 
            echo $_SERVER['REQUEST_URI'];
            ?>
" method="post">
            <?php 
            if (function_exists('wp_nonce_field')) {
                wp_nonce_field('seo-title-tag-action_posts-form');
            }
            ?>
            <input type="hidden" name="action" value="<?php 
            echo $title_tags_type;
            ?>
" />
            <table class="widefat">
            <thead>
            <tr>
            <th scope="col">ID</th>
            <th scope="col">Title</th>
            <th scope="col">Custom Title</th>
            <th scope="col">Slug</th>
            </tr>
            </thead>
            <tbody>
            <?php 
            manage_seo_title_tags_recursive($title_tags_type, $posts);
            echo '</table><br /><input type="submit" class="button" value="Submit" /></form>';
        } else {
            echo '<p><b>No ' . $title_tags_type . ' found!</b></p>';
        }
        // Render Categories Tab
    } elseif ($title_tags_type == 'categories' || $title_tags_type == 'tags') {
        $singular = 'tags' == $title_tags_type ? 'tag' : 'category';
        $taxonomy = 'tags' == $title_tags_type ? 'post_tag' : 'category';
        ?>
        <p>Use the form below to enter or update a custom <?php 
        echo $singular;
        ?>
 title.<br /></p>
        <?php 
        $terms = seo_title_tag_get_taxonomy($taxonomy);
        $table_name = $wpdb->prefix . "seo_title_tag_" . $singular;
        $term_titles = array();
        if (get_option("use_category_description_as_title") && 'categories' == $title_tags_type) {
            foreach ($terms as $category) {
                $term_titles[$category->term_id] = $category->category_description;
            }
        } else {
            // defult filling of the category titles field.
            $sql = 'SELECT ' . $singular . '_id as term_id, title FROM ' . $table_name;
            $results = $wpdb->get_results($sql);
            $term_titles = array();
            foreach ($results as $term) {
                $term_titles[$term->term_id] = $term->title;
            }
            $terms_new = array();
            if ($terms) {
                foreach ($terms as $term) {
                    $term->title = isset($term_titles[$term->term_id]) ? $term_titles[$term->term_id] : '';
                    if (empty($search_value)) {
                        $terms_new[] = $term;
                    } else {
                        if (preg_match('/' . $search_value . '/i', $term->title) || preg_match('/' . $search_value . '/i', $term->name)) {
                            $terms_new[] = $term;
                        }
                    }
                }
                $terms = $terms_new;
            }
        }
        $element_count = count($terms);
        if ($element_count > $manage_elements_per_page and ($page_no != 'all' or empty($page_no))) {
            if ($page_no > 0) {
                $terms = array_splice($terms, $page_no * $manage_elements_per_page);
            }
            $terms = array_slice($terms, 0, $manage_elements_per_page);
        }
        if ($terms) {
            ?>
            <form name="categories-form" action="<?php 
            echo $_SERVER['REQUEST_URI'];
            ?>
" method="post">
            <?php 
            if (function_exists('wp_nonce_field')) {
                wp_nonce_field('seo-title-tag-action_taxonomy-form');
            }
            ?>
            <input type="hidden" name="action" value="<?php 
            echo $title_tags_type;
            ?>
" />
            <table class="widefat">
            <thead>
            <tr>
            <th scope="col">ID</th>
            <th scope="col"><?php 
            echo ucfirst($singular);
            ?>
</th>
            <th scope="col">Custom Title</th>
            </tr>
            </thead>
            <tbody>
            <?php 
            foreach ($terms as $term) {
                $term_href = 'tags' == $title_tags_type ? get_tag_link($term->term_id) : get_category_link($term->term_id);
                ?>
                <tr>
                <td><a href="<?php 
                echo $term_href;
                ?>
"><?php 
                echo $term->term_id;
                ?>
</a></td>
                <td><?php 
                echo $term->name;
                ?>
</td>
                <td><input type="text" name="title_<?php 
                echo $term->term_id;
                ?>
" value="<?php 
                echo wp_specialchars($term->title, true);
                ?>
" size="70" /></td>
                <?php 
            }
            echo '</table><br /><input type="submit" class="button" value="Submit" /></form>';
        } else {
            //End of check for terms
            print "<b>No " . ucfirst($title_tags_type) . " found!</b>";
        }
    } elseif ($title_tags_type == 'urls') {
        ?>
            <p>Use the form below to enter or update a title tag for any URL, including archives pages, tag conjunction pages, etc.</p><p>In the URL field, leave off the http:// and your domain and your blog's directory (if you have one). e.g. <i>tag/seo+articles</i> is okay; <i>http://www.netconcepts.com/tag/seo+articles</i> is NOT.<br /></p>
            <?php 
        $table_name = $wpdb->prefix . "seo_title_tag_url";
        $urls;
        $sql = 'SELECT id, url, title from ' . $table_name;
        if (!empty($search_value)) {
            $sql .= ' WHERE url LIKE "%' . $wpdb->escape($search_value) . '%" OR title LIKE "%' . $wpdb->escape($search_value) . '%"';
        }
        $sql .= ' ORDER BY title';
        $urls = $wpdb->get_results($sql);
        $element_count = count($urls);
        if ($element_count > $manage_elements_per_page and ($page_no != 'all' or empty($page_no))) {
            if ($page_no > 0) {
                $urls = array_splice($urls, $page_no * $manage_elements_per_page);
            }
            $urls = array_slice($urls, 0, $manage_elements_per_page);
        }
        ?>
            <form name="urls-form" action="<?php 
        echo $_SERVER['REQUEST_URI'];
        ?>
" method="post">
            <?php 
        if (function_exists('wp_nonce_field')) {
            wp_nonce_field('seo-title-tag-action_urls-form');
        }
        ?>
            <input type="hidden" name="action" value="urls" />
            <table class="widefat">
            <thead>
            <tr>
            <th scope="col">ID</th>
            <th scope="col">URL</th>
            <th scope="col">Custom Title</th>
            </tr>
            </thead>
            <tbody>
            <?php 
        if (is_array($urls)) {
            foreach ($urls as $url) {
                $url_value = $url->title;
                if (get_magic_quotes_runtime()) {
                    $url_value = stripslashes($url_value);
                }
                ?>
                    <tr>
                    <td><a href="/<?php 
                echo preg_replace('/^\\//', '', $url->url);
                ?>
"><?php 
                echo $url->id;
                ?>
</a></td>
                    <td><input type="text" title="<?php 
                echo wp_specialchars($url->url, true);
                ?>
" name="url_<?php 
                echo $url->id;
                ?>
" value="<?php 
                echo wp_specialchars($url->url, true);
                ?>
" size="40" /></td>
                    <td><input type="text" title="<?php 
                echo wp_specialchars($url->title, true);
                ?>
" name="title_<?php 
                echo $url->id;
                ?>
" value="<?php 
                echo wp_specialchars($url_value, true);
                ?>
" size="70" /></td>
                    </tr>
                    <?php 
            }
        }
        for ($n = 0; $n < 5; $n++) {
            ?>
                <tr>
                <td>New (<?php 
            echo $n + 1;
            ?>
)</td>
                <td><input type="text" name="url_new_<?php 
            echo $n;
            ?>
" value="" size="40" /></td>
                <td><input type="text" name="title_new_<?php 
            echo $n;
            ?>
" value="" size="70" /></td>
                </tr>
                <?php 
        }
        echo '</table><br /><input type="submit" class="button" value="Submit" /></form>';
    } else {
        echo '<p>unknown title tags type!</p>';
    }
    ?>

    <?php 
    if ($element_count > $manage_elements_per_page) {
        if ($page_no == 'all' and !empty($page_no)) {
            echo 'View All&nbsp;&nbsp;';
        } else {
            echo '<a href="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&page_no=all&title_tags_type=' . $title_tags_type . $search_query_string . '">View All</a>&nbsp;&nbsp;';
        }
    }
    if ($element_count > $manage_elements_per_page) {
        for ($p = 0; $p < (int) ceil($element_count / $manage_elements_per_page); $p++) {
            if ($page_no == $p) {
                echo $p + 1 . '&nbsp;';
            } else {
                echo '<a href="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&page_no=' . $p . '&title_tags_type=' . $title_tags_type . $search_query_string . '">' . ($p + 1) . '</a> ';
            }
        }
    }
    ?>
    </div>
    <?php 
}
function manage_seo_title_tags()
{
    global $wpdb, $tabletags, $tablepost2tag, $install_directory, $wp_version;
    $search_value = '';
    $search_query_string = '';
    // Save Pages Form
    if (isset($_POST['action']) && ($_POST['action'] == 'pages' || $_POST['action'] == 'posts')) {
        if (function_exists('check_admin_referer')) {
            check_admin_referer('seo-title-tag-action_posts-form');
        }
        foreach ($_POST as $name => $value) {
            // Update Title Tag
            if (preg_match('/^tagtitle_(\\d+)$/', $name, $matches)) {
                $value = stripslashes(strip_tags($value));
                //print_r( $value); die();
                delete_post_meta($matches[1], get_option("custom_title_key"));
                add_post_meta($matches[1], get_option("custom_title_key"), $value);
            }
            // Update Descrption
            if (preg_match('/^tagdescription_(\\d+)$/', $name, $matches)) {
                $value_meta = stripslashes(strip_tags($value));
                delete_post_meta($matches[1], get_option("custom_meta_description_key"));
                add_post_meta($matches[1], get_option("custom_meta_description_key"), $value_meta);
            }
            // Update Slug
            if (preg_match('/^post_name_(\\d+)$/', $name, $matches)) {
                $postarr = get_post($matches[1], ARRAY_A);
                $old_post_name = $postarr['post_name'];
                $postarr['post_name'] = sanitize_title($value, $old_post_name);
                $postarr['post_category'] = array();
                $cats = get_the_category($postarr['ID']);
                if (is_array($cats)) {
                    foreach ($cats as $cat) {
                        $postarr['post_category'][] = $cat->term_id;
                    }
                }
                $tags_input = array();
                $tags = get_the_tags($postarr['ID']);
                if (is_array($tags)) {
                    foreach ($tags as $tag) {
                        $tags_input[] = $tag->name;
                    }
                }
                $postarr['tags_input'] = implode(', ', $tags_input);
                wp_insert_post($postarr);
            }
        }
        echo '<div class="updated"><p>The custom ' . ('pages' == $_POST['action'] ? 'page' : 'post') . ' titles have been updated.</p></div>';
        // Save Category and Tag Forms
    } elseif (isset($_POST['action']) && ($_POST['action'] == 'categories' || $_POST['action'] == 'tags')) {
        if (function_exists('check_admin_referer')) {
            check_admin_referer('seo-title-tag-action_taxonomy-form');
        }
        $singular = 'tags' == $_POST['action'] ? 'tag' : 'category';
        foreach ($_POST as $name => $value) {
            // Update Title Tag
            if (preg_match('/^title_(\\d+)$/', $name, $matches)) {
                $title = stripslashes(strip_tags($_POST['title_' . $matches[1]]));
                $title = $wpdb->escape($title);
                //for description
                $tag_meta_description = stripslashes(strip_tags($_POST['description_' . $matches[1]]));
                $tag_meta_description = $wpdb->escape($tag_meta_description);
                /*
                                          if (get_option("use_category_description_as_title")) {
                                          $temp = $wpdb->get_row('SELECT term_id FROM ' . $wpdb->term_taxonomy . ' where term_id = ' . $matches[1]);
                                          if ($temp->term_id == $matches[1]) {
                
                                          $wpdb->query('UPDATE ' . $wpdb->term_taxonomy . ' SET description = \'' . $title . '\' where term_id = ' . $matches[1]);
                
                                          }
                                          } else { */
                $table_name = $wpdb->prefix . 'seo_title_tag_' . $singular;
                $temp = $wpdb->get_results('SELECT ' . $singular . '_id as term_id from ' . $table_name . ' WHERE ' . $singular . '_id = ' . $matches[1]);
                if (isset($temp[1])) {
                    $wpdb->query('DELETE FROM ' . $table_name . ' WHERE ' . $singular . '_id = ' . $matches[1]);
                    unset($temp);
                } elseif (isset($temp[0])) {
                    $temp = $temp[0];
                }
                if (isset($temp) && $temp->term_id == $matches[1] && !empty($title)) {
                    $wpdb->query('UPDATE ' . $table_name . ' SET title = \'' . $title . '\', description= \'' . $tag_meta_description . '\' WHERE ' . $singular . '_id = ' . $matches[1]);
                } elseif (!empty($title)) {
                    $wpdb->query('INSERT INTO ' . $table_name . ' (' . $singular . '_id,title,description) values(\'' . $matches[1] . '\',\'' . $title . '\',\'' . $tag_meta_description . '\')');
                    //echo $wpdb->query('INSERT INTO ' . $table_name . ' (' . $singular . '_id,title) values(\'' . $matches[1] . '\',\'' . $title . '\')');
                } else {
                    $wpdb->query('DELETE FROM ' . $table_name . ' where ' . $singular . '_id = ' . $matches[1]);
                }
                //  }
            }
        }
        echo '<div class="updated"><p>The custom ' . $singular . ' titles have been saved.</p></div>';
        // Save URLs Form
    } elseif (isset($_POST['action']) and $_POST['action'] == 'urls') {
        if (function_exists('check_admin_referer')) {
            check_admin_referer('seo-title-tag-action_urls-form');
        }
        $table_name = $wpdb->prefix . "seo_title_tag_url";
        foreach ($_POST as $name => $value) {
            // Update Title Tag
            if (preg_match('/^url_(\\d+)$/', $name, $matches)) {
                $url = stripslashes($value);
                $url = $wpdb->escape($url);
                $title = stripslashes(strip_tags($_POST['title_' . $matches[1]]));
                $title = $wpdb->escape($title);
                $meta_description_url = stripslashes(strip_tags($_POST['meta_description_' . $matches[1]]));
                $meta_description_url = $wpdb->escape($meta_description_url);
                //for url description insert
                if (!empty($url) and !empty($title)) {
                    $wpdb->query('UPDATE ' . $table_name . ' SET url = \'' . $url . '\', title = \'' . $title . '\', description = \'' . $meta_description_url . '\' WHERE id = ' . $matches[1]);
                } elseif (empty($url) and empty($title)) {
                    $wpdb->query('DELETE FROM ' . $table_name . ' WHERE id = ' . $matches[1]);
                }
            } elseif (preg_match('/^url_new_(\\d+)$/', $name, $matches)) {
                $url = stripslashes($value);
                $url = $wpdb->escape($url);
                $title = stripslashes(strip_tags($_POST['title_new_' . $matches[1]]));
                $title = $wpdb->escape($title);
                //for url description insert
                $meta_description_url = stripslashes(strip_tags($_POST['meta_description_new_' . $matches[1]]));
                $meta_description_url = $wpdb->escape($meta_description_url);
                if (!empty($url) and !empty($title)) {
                    $wpdb->query('INSERT INTO ' . $table_name . ' (url,title,description) VALUES (\'' . $url . '\',\'' . $title . '\',\'' . $meta_description_url . '\')');
                }
            }
        }
        echo '<div class="updated"><p>The custom URLs and URL titles and description have been saved.</p></div>';
        // Filter by Search Value
    } elseif (isset($_POST['search_value'])) {
        $search_value = stripslashes(strip_tags($_POST['search_value']));
    }
    // If no search value from POST check for value in GET
    if (!isset($_POST['search_value']) && isset($_GET['search_value'])) {
        $search_value = stripslashes(strip_tags($_GET['search_value']));
    }
    $title_tags_type = stripslashes(strip_tags($_GET['title_tags_type']));
    $page_no = intval($_GET['page_no']);
    $manage_elements_per_page = get_option("manage_elements_per_page");
    $element_count = 0;
    if (empty($title_tags_type)) {
        $title_tags_type = 'pages';
    }
    if (empty($manage_elements_per_page)) {
        $manage_elements_per_page = 15;
    }
    $_SERVER['QUERY_STRING'] = preg_replace('/&title_tags_type=[^&]+/', '', $_SERVER['QUERY_STRING']);
    $_SERVER['QUERY_STRING'] = preg_replace('/&page_no=[^&]+/', '', $_SERVER['QUERY_STRING']);
    $_SERVER['QUERY_STRING'] = preg_replace('/&search_value=[^&]*/', '', $_SERVER['QUERY_STRING']);
    $search_query_string = '&search_value=' . $search_value;
    if (!$page_no) {
        $page_no = 0;
    }
    ?>

        <div class="wrap">

            <form  id="posts-filter" action="" method="post">
                <h2>SEO Title Tags</h2>

                <p id="post-search">
                    <label class="hidden" for="search_value">Search Title Tags:</label>
                    <input type="text" id="search_value" name="search_value" value="<?php 
    if (isset($search_value)) {
        echo esc_html($search_value);
    }
    ?>
" />
                    <input type="submit" value="Search Title Tags" class="button" />

                </p>

                <p><a href="options-general.php?page=seo-title-tag">Edit main SEO Title Tag plugin options &raquo;</a></p>

                <br class="clear" />

            </form>
            <!-- csv upload in tool page -->
    <?php 
    //Upload File
    if (isset($_POST['upload'])) {
        global $wpdb;
        $allowedExts = array("csv");
        $extension = end(explode(".", $_FILES['filename']['name']));
        //echo "type=". $_FILES['filename']['type'];
        if ($_FILES['filename']['type'] == "text/csv" || in_array($extension, $allowedExts)) {
            if ($_FILES['filename']['error'] > 0) {
                echo "Error: " . $_FILES['filename']['error'] . "<br />";
            } else {
                if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
                    echo "<h3 style='color:green;'>" . "File " . $_FILES['filename']['name'] . " uploaded successfully." . "</h3>";
                    //  readfile($_FILES['filename']['tmp_name']);
                }
                //Import uploaded file to Database
                $handle = fopen($_FILES['filename']['tmp_name'], "r");
                $i = 1;
                while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                    $num = count($data);
                    if ($i > 1) {
                        //// check value
                        $Postid = $data[0];
                        $meta_values = get_post_meta($Postid, $key, $single);
                        if (array_key_exists('meta_description', $meta_values) && array_key_exists('title_tag', $meta_values)) {
                            echo "&nbsp;";
                        } else {
                            add_post_meta($Postid, get_option("custom_title_key"), $value);
                            add_post_meta($Postid, get_option("custom_meta_description_key"), $Value_meta);
                        }
                        //$data = str_replace('"',"",$data[0]);
                        //$data = explode(',',$data);
                        $meta_description_key = get_option("custom_meta_description_key");
                        $meta_title_key = get_option("custom_title_key");
                        //$import="INSERT into wp_postmeta(post_id,meta_key,meta_value) values('".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."')";
                        //$import="INSERT into wp_postmeta(post_id,meta_key,meta_value) values('".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."')";
                        $import_title = "UPDATE " . $wpdb->prefix . "postmeta SET meta_value = '" . addslashes($data[1]) . "' WHERE post_id = '" . addslashes($data[0]) . "' and meta_key = '" . $meta_title_key . "'";
                        $import_meta_description = "UPDATE " . $wpdb->prefix . "postmeta SET meta_value = '" . addslashes($data[2]) . "' WHERE post_id = '" . addslashes($data[0]) . "' and meta_key = '" . $meta_description_key . "'";
                        $import_post_slug = "UPDATE " . $wpdb->prefix . "posts SET post_name = '" . addslashes($data[3]) . "' WHERE id = '" . addslashes($data[0]) . "'";
                        mysql_query($import_title) or die(mysql_error());
                        mysql_query($import_meta_description) or die(mysql_error());
                        mysql_query($import_post_slug) or die(mysql_error());
                    } else {
                        echo $sucess_msg = "<h3 style='color:green;'>CSV File Data Successfully Inserted.</h3>";
                    }
                    $i++;
                }
                fclose($handle);
            }
        } else {
            echo "<h2 style='color:red';>Invalid File! Please Select Valid CSV File to upload</h2>";
        }
    }
    ?>
            <!-- End Funcations for csv file upload -->
            <?php 
    $url = plugins_url();
    ?>
            <h3>Upload Titles and Meta Descriptions directly through a CSV File:</h3>
            <!-- for csv file upload -->
            <form enctype='multipart/form-data' action='<?php 
    echo $_SERVER["REQUEST_URI"];
    ?>
'  method='post'>
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td scope="row">Download current CSV file for editing:</td>
                        <td><input type='submit' name='download' value='Download'></td>
                    </tr>
                    <tr>
                        <td scope="row">Select a CSV file to upload:</td>
                        <td><input size='30' type='file' name='filename'>
                            <input type='submit' name='upload' value='Upload'> <br/>

                        </td>
                    </tr>
                </table>
            </form>
            <!-- end csv upload in tool page -->

    <?php 
    //do the nav menu items for the subsubmenu
    if (empty($_REQUEST['title_tags_type'])) {
        $_REQUEST['title_tags_type'] = 'pages';
    }
    echo '<ul id="subsubmenu">' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'pages') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=pages">Pages</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'posts') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=posts">Posts</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'categories') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=categories">Categories</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'tags') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=tags">Tags</a></li>' . "\n";
    echo '<li ' . is_current($_REQUEST['title_tags_type'], 'urls') . '><a href="?' . $_SERVER['QUERY_STRING'] . '&title_tags_type=urls">URLs</a></li>' . "\n";
    echo '</ul>' . "\n";
    // Render Page and Post Tabs
    if ($title_tags_type == 'pages' || $title_tags_type == 'posts') {
        $post_type = substr($title_tags_type, 0, -1);
        // Database table uses singular version
        ?>
                <p>Use the form below to enter or update a custom <?php 
        echo $post_type;
        ?>
 title.<br /></p>
        <?php 
        if (!empty($search_value)) {
            if ($page_no > 0) {
                // $limit = ' LIMIT ' . ($page_no * $manage_elements_per_page) . ', ' . $manage_elements_per_page;
                $limit = 10;
            } else {
                //  $limit = ' LIMIT ' . $manage_elements_per_page;
                $limit = 10;
            }
            $posts = $wpdb->get_results('SELECT * FROM ' . $wpdb->posts . ' WHERE post_type = \'' . $post_type . '\' ORDER BY menu_order ASC' . ('posts' == $title_tags_type ? ', post_date DESC' : ', ID ASC') . $limit);
        } else {
            $posts = $wpdb->get_results('SELECT * FROM ' . $wpdb->posts . ' WHERE post_type = \'' . $post_type . '\' ORDER BY menu_order ASC' . ('posts' == $title_tags_type ? ', post_date DESC' : ', ID ASC'));
            $new_posts;
            foreach ($posts as $post) {
                if (isset($post->post_type) and $post->post_type != $post_type) {
                    continue;
                }
                if (empty($search_value)) {
                    // No search value, add all
                    $new_posts[] = $post;
                } else {
                    // Filter based on search value
                    if (preg_match('/' . $search_value . '/i', $post->post_title)) {
                        $new_posts[] = $post;
                    } else {
                        $post_custom = get_post_custom($post->ID);
                        if (preg_match('/' . $search_value . '/i', $post_custom[get_option("custom_title_key")][0]) || preg_match('/' . $search_value . '/i', $post->post_content) || preg_match('/' . $search_value . '/i', $post->post_excerpt)) {
                            $new_posts[] = $post;
                        }
                    }
                }
            }
            $posts = $new_posts;
            $element_count = count($posts);
            if ($element_count > $manage_elements_per_page and ($page_no != 'all' or empty($page_no))) {
                if ($page_no > 0) {
                    $posts = array_splice($posts, $page_no * $manage_elements_per_page);
                }
                $posts = array_slice($posts, 0, $manage_elements_per_page);
            }
        }
        if ($posts) {
            ?>
                    <form name="posts-form" action="<?php 
            echo $_SERVER['REQUEST_URI'];
            ?>
" method="post">
                    <?php 
            if (function_exists('wp_nonce_field')) {
                wp_nonce_field('seo-title-tag-action_posts-form');
            }
            ?>
                        <input type="hidden" name="action" value="<?php 
            echo $title_tags_type;
            ?>
" />
                        <table class="widefat">
                            <thead>
                                <tr>
                                    <th scope="col">ID</th>
                                    <th scope="col">Title</th>
                                    <th scope="col">Custom Title and Meta Description</th>
                                    <th scope="col">Slug</th>
                                </tr>
                            </thead>
                            <tbody>
                    <?php 
            manage_seo_title_tags_recursive($title_tags_type, $posts);
            echo '</table><br /><input type="submit" class="button" value="Submit" /></form>';
        } else {
            echo '<p><b>No ' . $title_tags_type . ' found!</b></p>';
        }
        // Render Categories Tab
    } elseif ($title_tags_type == 'categories' || $title_tags_type == 'tags') {
        $singular = 'tags' == $title_tags_type ? 'tag' : 'category';
        $taxonomy = 'tags' == $title_tags_type ? 'post_tag' : 'category';
        ?>
                        <p>Use the form below to enter or update a custom <?php 
        echo $singular;
        ?>
 title.<br /></p>
        <?php 
        $terms = seo_title_tag_get_taxonomy($taxonomy);
        $table_name = $wpdb->prefix . "seo_title_tag_" . $singular;
        $term_titles = array();
        /*
                  if (get_option("use_category_description_as_title") && 'categories' == $title_tags_type) {
        
        
                  foreach ($terms as $category) {
                  print_r($term_titles[$category->term_id] = $category->category_description );
                  }
                  } else { */
        // defult filling of the category titles field.
        $sql = 'SELECT ' . $singular . '_id as term_id, title,description FROM ' . $table_name;
        $results = $wpdb->get_results($sql);
        $term_titles = array();
        $term_description = array();
        foreach ($results as $term) {
            $term_titles[$term->term_id] = $term->title;
            $term_description[$term->term_id] = $term->description;
        }
        $terms_new = array();
        if ($terms) {
            foreach ($terms as $term) {
                $term->title = isset($term_titles[$term->term_id]) ? $term_titles[$term->term_id] : '';
                $term->description = isset($term_description[$term->term_id]) ? $term_description[$term->term_id] : '';
                if (empty($search_value)) {
                    $terms_new[] = $term;
                } else {
                    if (preg_match('/' . $search_value . '/i', $term->title) || preg_match('/' . $search_value . '/i', $term->name)) {
                        $terms_new[] = $term;
                    }
                }
            }
            $terms = $terms_new;
        }
        // }
        $element_count = count($terms);
        if ($element_count > $manage_elements_per_page and ($page_no != 'all' or empty($page_no))) {
            if ($page_no > 0) {
                $terms = array_splice($terms, $page_no * $manage_elements_per_page);
            }
            $terms = array_slice($terms, 0, $manage_elements_per_page);
        }
        if ($terms) {
            ?>
                            <form name="categories-form" action="<?php 
            echo $_SERVER['REQUEST_URI'];
            ?>
" method="post">
                            <?php 
            if (function_exists('wp_nonce_field')) {
                wp_nonce_field('seo-title-tag-action_taxonomy-form');
            }
            ?>
                                <input type="hidden" name="action" value="<?php 
            echo $title_tags_type;
            ?>
" />
                                <table class="widefat">
                                    <thead>
                                        <tr>
                                            <th scope="col">ID</th>
                                            <th scope="col"><?php 
            echo ucfirst($singular);
            ?>
</th>
                                            <th scope="col">Custom Title and Meta Description</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                            <?php 
            foreach ($terms as $term) {
                $term_href = 'tags' == $title_tags_type ? get_tag_link($term->term_id) : get_category_link($term->term_id);
                ?>
                                            <tr>

                                                <td><a href="<?php 
                echo $term_href;
                ?>
"><?php 
                echo $term->term_id;
                ?>
</a></td>
                                                <td><?php 
                echo $term->name;
                ?>
</td>
                                                <td><span style="width:110px;float:left; padding-right: 5px;">Title </span><input type="text" name="title_<?php 
                echo $term->term_id;
                ?>
" value="<?php 
                echo esc_html($term->title);
                ?>
" size="80" /><br />
                                                    <span style="width:110px;float:left; padding-right: 5px;">Meta Description</span><input type="text" name="description_<?php 
                echo $term->term_id;
                ?>
" value="<?php 
                echo esc_html($term->description);
                ?>
" size="80" />

                                                </td>
                                    <?php 
            }
            echo '</table><br /><input type="submit" class="button" value="Submit" /></form>';
        } else {
            //End of check for terms
            print "<b>No " . ucfirst($title_tags_type) . " found!</b>";
        }
    } elseif ($title_tags_type == 'urls') {
        ?>
                                <p>Use the form below to enter or update a title tag for any URL, including archives pages, tag conjunction pages, etc.</p><p>In the URL field, leave off the http:// and your domain and your blog's directory (if you have one). e.g. <i>tag/seo+articles</i> is okay; <i>http://www.netconcepts.com/tag/seo+articles</i> is NOT.<br /></p>
        <?php 
        $table_name = $wpdb->prefix . "seo_title_tag_url";
        $urls;
        $sql = 'SELECT id, url, title,description from ' . $table_name;
        if (!empty($search_value)) {
            $sql .= ' WHERE url LIKE "%' . $wpdb->escape($search_value) . '%" OR title LIKE "%' . $wpdb->escape($search_value) . '%"';
        }
        $sql .= ' ORDER BY title';
        $urls = $wpdb->get_results($sql);
        $element_count = count($urls);
        if ($element_count > $manage_elements_per_page and ($page_no != 'all' or empty($page_no))) {
            if ($page_no > 0) {
                $urls = array_splice($urls, $page_no * $manage_elements_per_page);
            }
            $urls = array_slice($urls, 0, $manage_elements_per_page);
        }
        ?>
                                <form name="urls-form" action="<?php 
        echo $_SERVER['REQUEST_URI'];
        ?>
" method="post">
                                <?php 
        if (function_exists('wp_nonce_field')) {
            wp_nonce_field('seo-title-tag-action_urls-form');
        }
        ?>
                                    <input type="hidden" name="action" value="urls" />
                                    <table class="widefat">
                                        <thead>
                                            <tr>
                                                <th scope="col">ID</th>
                                                <th scope="col">URL</th>
                                                <th scope="col">Custom Title and Meta Description</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                <?php 
        if (is_array($urls)) {
            foreach ($urls as $url) {
                $url_value = $url->title;
                $url_value_meta_description = $url->description;
                if (get_magic_quotes_runtime()) {
                    $url_value = stripslashes($url_value);
                }
                ?>
                                                    <tr>
                                                        <td><a href="/<?php 
                echo preg_replace('/^\\//', '', $url->url);
                ?>
"><?php 
                echo $url->id;
                ?>
</a></td>
                                                        <td><input type="text" title="<?php 
                echo esc_html($url->url);
                ?>
" name="url_<?php 
                echo $url->id;
                ?>
" value="<?php 
                echo esc_html($url->url);
                ?>
" size="40" /></td>
                                                        <td>
                                                            <span style="width:110px;float:left; padding-right: 5px;">Title</span><input type="text" title="<?php 
                echo wp_specialchars($url->title, true);
                ?>
" name="title_<?php 
                echo $url->id;
                ?>
" value="<?php 
                echo esc_html($url_value);
                ?>
" size="70" /><br />
                                                            <span style="width:110px;float:left; padding-right: 5px;">Meta Description</span><input type="text" title="<?php 
                echo esc_html($url->description);
                ?>
" name="meta_description_<?php 
                echo $url->id;
                ?>
" value="<?php 
                echo esc_html($url_value_meta_description);
                ?>
" size="70" />

                                                        </td>
                                                    </tr>
                <?php 
            }
        }
        for ($n = 0; $n < 5; $n++) {
            ?>
                                                <tr>
                                                <td>New <!-- (<?php 
            // echo ($n + 1)
            ?>
) --> </td>
                                                    <td><input type="text" name="url_new_<?php 
            echo $n;
            ?>
" value="" size="40" /></td>
                                                    <td><span style="width:110px;float:left; padding-right: 5px;">Title</span><input type="text" name="title_new_<?php 
            echo $n;
            ?>
" value="" size="70" /><br />
                                                        <span style="width:110px;float:left; padding-right: 5px;"> Meta Description</span><input type="text" name="meta_description_new_<?php 
            echo $n;
            ?>
" value="" size="70" />
                                                    </td>
                                                </tr>
                                                <?php 
        }
        echo '</table><br /><input type="submit" class="button" value="Submit" /></form>';
    } else {
        echo '<p>unknown title tags type!</p>';
    }
    ?>

    <?php 
    if ($element_count > $manage_elements_per_page) {
        if ($page_no == 'all' and !empty($page_no)) {
            //  echo 'View All&nbsp;&nbsp;';
        } else {
            // echo '<a href="'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&page_no=all&title_tags_type='.$title_tags_type.$search_query_string.'">View All</a>&nbsp;&nbsp;';
        }
    }
    ?>
                                    <style>
                                        .pagination{float:right;}
                                        .pagination a{border:1px solid #ccc;padding:4px 8px;}
                                    </style>
    <?php 
    echo "<div class='pagination'>";
    if ($element_count > $manage_elements_per_page) {
        $max = (int) ceil($element_count / $manage_elements_per_page);
        $inital = 0;
        if ($max > 5) {
            $max = 5;
        }
        if ($element_count > $page_no * $manage_elements_per_page && $page_no >= 4) {
            $inital = $page_no - ($page_no + 1) % 5;
            $max = (int) ceil($element_count / $manage_elements_per_page);
            if ($max > $inital + 5) {
                $max = $inital + 5;
            }
            echo '<a href="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&page_no=' . ($page_no - 1) . '&title_tags_type=' . $title_tags_type . $search_query_string . '"><< Prev</a> ';
        }
        for ($p = $inital; $p < $max; $p++) {
            if ($page_no == $p) {
                echo $p + 1 . '&nbsp;';
            } else {
                echo '<a href="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&page_no=' . $p . '&title_tags_type=' . $title_tags_type . $search_query_string . '">' . ($p + 1) . '</a> ';
            }
        }
        if ($page_no != $max - 1 && $max >= 5) {
            echo '<a href="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&page_no=' . ($page_no + 1) . '&title_tags_type=' . $title_tags_type . $search_query_string . '">Next >> </a> ';
        }
    }
    echo "</div>";
    ?>
                                    </div>
                                    <?php 
}