0

I am using a plugin to show a form in admin that inputs some data from user.

This form submits to this url:

http://localhost/wordpress/wp-admin/my_settings_save.php

But when this url is called, although its an admin url, it does not set is_admin() to true and thus my admin handling code does not get called.

This is the handling code:

if ( is_admin() )
    require_once plugin_dir_path( __FILE__ ) . 'admin/core.php';
else
    require_once plugin_dir_path( __FILE__ ) . 'public/core.php';

When the form is called first time, is_admin() is true... but when the posted url (http://localhost/wordpress/wp-admin/my_settings_save.php) is called, is_admin() returns false and thus my admin/core.php is not called.

Any suggestions...?

1 Answer 1

0

Ok, here is what I found out to solve the issue:

First of all, the form should be posted to admin-post.php

2nd, there should be a hidden variable "action"

so the form should be like:

<form method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
    <input type="hidden" name="action" value="my_settings_save">
    ....
</form>

Now the is_admin() is true and my admin core file is called successfully.

In that admin core file, I only set this action:

add_action('admin_post_my_settings_save', 'my_settings_save_function');
function my_settings_save_function()
{
    ...
}

and it works like a charm!

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.