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.

Essentially I need to verify my data using a nonce, but I'm not sure that I'm doing it right. What I'm trying to do is remove a menu option for a user that isn't an admin. If a checkbox is checked and the form is submitted, that menu item will be removed for anyone that cannot manage options.

EDIT: Got my code posted here instead of Gist:

<?php
/*
Plugin Name: Developer Mode Tools 02
Author: Tyler Gerig
Version: 1.1
Description: Add developer mode options to your wordpress installation.
License: GNU General Public License v2 or later
*/

function pretty_dump($dump){
        echo '<pre>';
        var_dump($dump);
        echo '</pre>';
}

/**
*Add an options page for the plugin.
*
*@since 1.0.
*
*@return void
*/
function check_admin_page(){
        $screen = get_current_screen();
        //pretty_dump($screen->base);
        if($screen->base == 'settings_page_tgdmt_options_page'){
                if($_POST){
                                $menus_to_remove = array();
                                $tgdmt_menus = $_POST;
                                foreach($tgdmt_menus as $tgdmt_menu){
                                        if($tgdmt_menu != 'Save Changes'){
                                                $menus_to_remove[] = $tgdmt_menu;
                                        }
                                }
                                update_option( 'tgdmt_menu_settings', array_map('sanitize_text_field', $menus_to_remove) );
                                //pretty_dump($menus_to_remove);
                }

        }
        if(isset( $_POST[ 'tdgmt_plugin_noncename']) && wp_verify_nonce( $_POST[' tdgmt_plugin_noncename'], plugins_url( __FILE__))){
                echo 'Nonce verified';
        }else{
                echo 'Nonce not verified';
        }

}
add_action('admin_head', 'check_admin_page');

function tgdmt_remove_menus(){
        if(!current_user_can('manage_options')){
                if(get_option('tgdmt_menu_settings')){
                        $tgdmt_remove = get_option('tgdmt_menu_settings');
                        foreach($tgdmt_remove as $remove){
                                remove_menu_page($remove);
                        }
                }
        }
}
add_action('admin_menu', 'tgdmt_remove_menus', 11);

function tgdmt_add_options_page(){
        //Add new page under the "Settings tab"
        add_options_page(
                __( 'Developer Mode Tools Options' ),
                __( 'Developer Mode Tools Options' ),
                'manage_options',
                'tgdmt_options_page',
                'tgdmt_render_options_page'
        );
}

add_action( 'admin_menu', 'tgdmt_add_options_page' );


function tgdmt_menu_settings() {
    // Register a binary value called ""
    register_setting(
        'tgdmt_menu_settings',
        'tgdmt_menu_settings',
        ''
    );
}
add_action('admin_init','tgdmt_menu_settings');


function tgdmt_render_options_page(){
        ?>
        <div class="wrap">
                <h2><?php _e( 'Developer Mode Tools Options'); ?></h2>
                <form action="<?php //plugins_url( 'tgdmt_update_menu.php' , dirname(__FILE__) )?>" method="post">
                        <p>
                        <?php wp_nonce_field(plugins_url(__FILE__), 'tgdmt_plugin_noncename'); ?>
                        <?php
                        global $menu;
                                //pretty_dump($menu);
                                $i = 0; 
                                //if( current_user_can('manage_options')){
                                        foreach($menu as $item){

                                                if($item[0] != ''){
                                                        $menu_name = trim(str_replace(range(0,9),'',$item[0]));
                                                        echo '<input name="menu'.$i.'" type="checkbox" value="'.$item[2].'" ' . checked( 1, '', false ) . ' />'. $menu_name . '<br>';
                                                        $i++;
                                                        //echo '<input id="tgdmt_disable'.$i.'" name="tgdmt_disable'.$i.'" type="checkbox" value="'.$item[2].'" ' . checked( 1, $status, false ) . ' />'.__($menu_name, 'tgdmt').'<br>';
                                                }
                                        }
                                ?>

                                <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'tgdmt' ); ?>">
                        </p>
                </form>
        </div>
        <?php



}
share|improve this question
    
What does "save my data into a nonce" mean? Are you sure you know what an nonce is? –  s_ha_dum Sep 8 '13 at 19:25
    
Also, posting "here in a block of code" is pretty much the rule, so post your code here. Look for the {} button to format it. –  s_ha_dum Sep 8 '13 at 19:28
    
I don't, that's my point. I don't really understand nonces, but I need to use one. I probably worded it wrong, but I do need to use a nonce and verify that nonce for my form. –  Tyler Gerig Sep 8 '13 at 19:42
    
Post your answer in the "Answer" box, please. –  s_ha_dum Sep 8 '13 at 23:12
    
I would, but I can't for another 4 hours. –  Tyler Gerig Sep 8 '13 at 23:17
add comment

1 Answer

I figured out what I needed to do. I needed

if(isset( $_POST[ 'tdgmt_plugin_noncename']) && wp_verify_nonce( $_POST['tdgmt_plugin_noncename'], plugins_url( __FILE__))){

to be above

 if($screen->base == 'settings_page_tgdmt_options_page'){

as well as fixing my typos of

tdgmt_plugin_noncename

instead of

tgdmt_plugin_noncename
share|improve this answer
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.