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.

What is the right way to issue commands in wp-admin? My plugin imports data from a set of RSS feeds via a cron job. Thats all working good. I've set up a custom settings page to change parameters, and wanted to add an "Update Now" button so the user could force a manual refresh. I need a few other commands as well.

What is the "wordpress way" to add and handle these commands?

I tried adding a url http://example.com/wp-admin/options-general.php?page=myplugin&action=update to the options page, then

if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'update') { doUpdate(); }

but i get an error showing $wp has not been initialised. I tried including the wp-load.php and the wp-settings.php but it didn't help.

Error message is Fatal error: Call to a member function add_rewrite_tag() on a non-object in /wordpress/wp-includes/rewrite.php on line 51

The lines causing the problem in doUpdate() are

    if (taxonomy_exists($name)) {
        return;
    }

    $slug  = str_replace(' ', '_', $title);
    register_taxonomy(
        $name,
        '',
        array(
             'labels'            => array( 'name' => __(ucwords($title)) ),
             'public'            => true,
             'show_ui'           => self::SHOW_UI,
             'show_in_nav_menus' => true,
             'show_admin_column' => true,
             'hierarchical'      => false,
             'query_var'         => $slug,
             'rewrite'           => array( 'slug' => __($slug) ),
        )
    );

What am I doing wrong? How can I allow the user to perform an action from an admin settings pane?

share|improve this question
    
This sounds roughly correct but please post more code. The problem must be in the implementation. And post the complete unedited error message. –  s_ha_dum Mar 15 '13 at 4:19
add comment

1 Answer 1

up vote 1 down vote accepted

Always wait for init or wp_loaded, so you know all needed functions and classes are available.

add_action( 'wp_loaded', 'plugin_prefix_start' );

function plugin_prefix_start()
{
    if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'update') 
    { 
        doUpdate(); 
    }
}
share|improve this answer
    
aagh! of course! thanks for the patient answer to my silly question. Its working beautifully now (after wasting hours trying to figure out why it wasn't). –  Steve Mar 16 '13 at 0:09
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.