0

I have this little php code for wordpress. It doesn't work. I want to make one add_action under another add_action. But the add_action that is under, is not working!

function no_reminder() {
    echo "no_reminder function is active";
    add_action('admin_menu','no_reminder');

    remove_action( 'admin_notices', 'update_nag', 3 );
}

add_action('admin_menu', 'custom');
function custom() {
    add_menu_page('settings', 'settings', '8', 'custom', 'customfunc');
}

function customfunc(){
    $wpp_rg = get_option('wpp_rg');
    if ($_POST['wpp_submit']):
        update_option('wpp_rg',$_POST['wpp_rg']);
    endif;
    $farzad = get_option('wpp_rg');
    /******************** IF1 ***********************/
    if ($farzad==1) {
        do_action('no_reminder');
        no_reminder();
        echo "If active";
    }
    if ($farzad==2) {
        echo "IF Not Active";
    }
/******************** IF1 ***********************/
?>
<div class="wrap">
<form action="" method="post">
<table width="50%" border="0" cellspacing="5" cellpadding="5">
<tr>
        <th scope="row">test mode :</th>
        <td><p>
         <label>
                <input name="wpp_rg" type="radio" value="1" id="RadioGroup1_0" <?php echo ($farzad==1 ? 'checked="checked"' : '') ?> />
                active</label>
         <br />
         <label>
                <input name="wpp_rg" type="radio" value="2" id="RadioGroup1_1" <?php echo ($farzad==2 ? 'checked="checked"' : '') ?>/>
                not active</label>
         <br />
        </p></td>
</tr>

<tr>
        <th></th>
        <td><input type="submit" name="wpp_submit" value="save" class="button" /></td>
</tr>
</table>
</form>
</div>
<?php
}

?>

What should i do ?

1 Answer 1

3

The preferred method of creating a settings page is to use the WordPress Settings API.

From your code example, it looks like it's too late to remove the admin_notices hook inside your callback in the admin_menu hook.

You should try to move the logic from the customfunc() callback into another callback activated from some earlier hook, for example admin_init.

Additionally:

You are also calling the new admin menu page settings, but the user might confuse it with the existing Settings page.

The User Levels you have in one of the add_menu_page() input parameters, are deprecated, use instead some of the Capabilites mentioned here.

So instead use for example:

add_menu_page( 'My settings', 'My settings', 'manage_options', 'custom', 'customfunc' );
14
  • 3
    I agree that a lot of the OP's issues will probably resolve themselves once the OP starts using the Settings API correctly. Commented Mar 8, 2014 at 10:56
  • 1
    @UserPHP You can help "more" by specifying what "it's not working again" is. This is vague at its best. Please come up with specific debug/error output and a really detailed description of assumptions vs. real world output.
    – kaiser
    Commented Mar 8, 2014 at 16:00
  • 1
    First, please read in Codex about debugging and WP_DEBUG. Second - as I already said - not working is no proper problem or error description.
    – kaiser
    Commented Mar 8, 2014 at 16:53
  • 1
    While you are going through the settings API and the above suggestions, you might try to add something like this (untested) : pastebin.com/fcH4MV8r to your code snippet. I don't want to add this to the answer, since I think you should use the settings API ;-)
    – birgire
    Commented Mar 8, 2014 at 20:12
  • 2
    I see, appreciate your thoughts and the vote on this answer, but please stop it on other questions, I would like to earn it through hard work. Could you down vote them back, thanks
    – birgire
    Commented Mar 9, 2014 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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