Issue plugin commands in admin settings page

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?

Read More

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?

Related posts

Leave a Reply

1 comment

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