How to create and link to administration page for a plugin?

OK Full Code:

<?php

//check to see whether the user is an admin or not.
if (is_admin()) {


function wpsc_display_products_seo(){
global $wpdb;
$productList = $wpdb->get_results("SELECT * FROM " . WPSC_TABLE_PRODUCT_LIST . " ORDER BY name ASC");
echo  get_admin_page_parent(); 

$path = 'admin.php?page=wpsc_product_seo_details';
$url = admin_url($path);


?>
<h2><?php _e('Products List','wpsc'); ?></h2>
<p>Below is a list of products. Select a product to edit it's SEO properties.</p>
    <table>
        <tr>
            <td></td>
            <td>Product Name</td>
            <td>Meta Title</td>
            <td>Meta Description</td>
            <td>Edit</td>
        </tr>
    </table>

    <table>
        <?php if($productList) : ?>
            <?php foreach($productList as $product) : ?>
                <?php $meta = getProductMeta($product->id); ?>
                <tr style="background:#fff;">
                    <td width="200"><?php echo $product->name; ?></td>
                    <td width="300"><?php getVal($meta, 'meta_title'); ?></td>
                    <td width="200"><?php getVal($meta, 'meta_description'); ?></td>
                    <td width="200"><?php getVal($meta, 'meta_keywords'); ?></td>
                    <td width="200"><a href="<?php echo $url; ?>">Edit</a></td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
    </table>

<?php

}

function wpsc_product_seo_details()
{
    echo "<h2>Hello</h2>";
}

function getProductMeta($id)
{
    global $wpdb;
    $meta = $wpdb->get_results("SELECT * FROM " . WPSC_TABLE_PRODUCT_META . " WHERE product_id = $id");
    if($meta)
    {
        return $meta;
    } else {
        return null;
    }
}

function getVal($RS, $key)
{
    if($RS)
    {
        if (property_exists($RS, $key))
        {
            return $RS->$key;
        } 
    }
    return null;

}




    function wpsc_add_seo_admin_pages($page_hooks, $base_page) {
        $page_hooks[] =  add_submenu_page($base_page, __('SEO For Products','wpsc'),  __('SEO For Products','wpsc'), 7, 'wpsc-module-seo', 'wpsc_display_products_seo');
        return $page_hooks;
    }
    add_filter('wpsc_additional_pages', 'wpsc_add_seo_admin_pages',10, 2);
}

?>

OLD POST

Hi Guys,

Read More

I am currently writing a plugin for my client in wordpress. The issue I am having is when a user clicks on edit to change a record I am not sure how to create the admin link to do this.

i.e.

<a href="<?php echo get_bloginfo('url'); ?>/wp-admin/admin.php?page=wpsc_product_seo_details">Edit</a>


function wpsc_product_seo_details()
{
    echo "<h2>Hello</h2>";
}

I know my markup for the tag is probably wrong but I was just testing. Do I need to register a hook to do this.

Related posts

Leave a Reply

4 comments

  1. I am not sure what you are trying to do.

    Admin links can be conveniently created with admin_url() function, like this:

    $path = 'admin.php?page=wpsc_product_seo_details';
    $url = admin_url($path);
    $link = "<a href='{$url}'>Edit</a>";
    echo $link;
    

    What exactly do you have problem with?

  2. Do you have problems creating the administration page?

    add_action( 'admin_menu', 'wpse4677_admin_menu' );
    function wpse4677_admin_menu()
    {
        add_options_page(
            'WPSE 4677 Page title',
            'WPSE 4677 Menu item title',
            'manage_options', // Minimum capability to view this page
            'wpse4677-page-identifier', // Unique identifier
            'wpse4677_page_content' // Callback function to get the contents
        );
    }
    
    function wpse4677_page_content()
    {
        echo '<div class="wrap">';
        echo '<h2>Welcome to my page!</h2>';
        echo '</div>';
    }
    
  3. Just some information to tag onto the discussion, and this may be useful to you later..

    When you’re on a plugin page, various admin variables are set, you can build a base URL from those variables and generate links from there, assuming you want them to point at the current plugin page..

    global $pagenow, $plugin_page;
    $this_page = add_query_arg( 'page', $plugin_page, admin_url( $pagenow ) );
    /* 
       Example URL
       themes.php?page=custom-background 
    
       $pagenow     - themes.php
       $plugin_page - custom-background
    
    */
    

    Want to add your own query vars onto that URL, simply add them to the variable created above using add_query_arg

    $edit_link = add_query_arg( 'edit_id', $id, $this_page );
    /*
       Example only, $id would need to come from your code
       and $edit_link generated appropriately for each result you display
    */
    

    It should work in theory at least, i didn’t test the code, but i see no reason it wouldn’t work.. 🙂

  4. Resolved!

    Try to overcomplicate things when a simple conditional would of done the trick in the main function called from the page.

    function wpsc_display_products_seo(){
    
        if($_GET['pid'] != "") 
        {
            renderProductForm();
        } else {
            renderProductList();
        }
    
    }