Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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,

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.

share|improve this question
    
Rarst you copied some of the page markup when you adjusted the question, just letting you know.. ;) –  t31os Nov 30 '10 at 11:03
    
Wasn't me. That is how original question was. I just tried to clarify the title (which it seem will require take two). –  Rarst Nov 30 '10 at 11:10
    
By the function prefix this seems like something built on top of WP e-Commerce plugin?.. –  Rarst Nov 30 '10 at 11:29
    
@Steve: Is your problem that you don't know when/how to call wpsc_product_seo_details()? Did you add it to your menu somehow? –  Jan Fabry Nov 30 '10 at 11:30
2  
Ok, looking at your solution I am lost completely now. :) Could you please rename your question to accurately describe issue and context? So others can benefit from this in the future. –  Rarst Nov 30 '10 at 11:49

4 Answers 4

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?

share|improve this answer
    
When I click on the link, I get: You do not have sufficient permissions to access this page. –  Steve Clark Nov 30 '10 at 10:57
    
Sorry I am probably not explaining this very well. I have a submenu item which when you click on it, you get a list of products. I have an edit button next to each product and when the administrator clicks on that button it goes to the url above and calls the relevant function (So I can do a product edit form etc) –  Steve Clark Nov 30 '10 at 11:01
    
Products are what technically, custom post types? What is edit link exactly? I am still not sure, but take a look at edit_post_link() which generates edit links for individual posts. –  Rarst Nov 30 '10 at 11:06

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>';
}
share|improve this answer
    
I have updated the code block in my original post! - Hope this helps, Im sure your going to say "WTF" –  Steve Clark Nov 30 '10 at 11:13

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.. :)

share|improve this answer
    
I want to note that return of add page function won't work. It's not name of page, it's more complex screen identifier that goes into $hook_suffix global. Also you can pass associative array of stuff as first argument to add_query_arg() instead of calling several times. –  Rarst Jul 5 '11 at 16:48
    
I'm not sure i follow Rarst, what exactly is it that won't work? If the code won't do what it was intended to do then i'll remove the answer(code that doesn't work serves little purpose). –  t31os Jul 6 '11 at 10:05
    
I meant this part $my_hook = add_xxxx_page in code comments, $my_hook won't work as replacement for $plugin_page because they are different things. The code itself is sound and working. –  Rarst Jul 6 '11 at 10:39
    
I never suggested it as a replacement, only that you could refer to $my_hook in place of that var, because they usually hold the same value. Not sure how they're different, take a look at how $plugin_page is filled(wp-admin/admin.php), then take a look at how that menu value is created. –  t31os Jul 6 '11 at 11:24
1  
Yes your right sorry, early start, brain hadn't quite booted up yet... lol.. forget my previous comments.. ;) I'll remove that part of the answer. –  t31os Jul 6 '11 at 13:26

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();
    }

}
share|improve this answer
    
why you didn't post all the code ? –  Francisco Corrales Morales Feb 6 at 15:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.