0

After I have added a plugin menu and have some input to be saved in the database, everything works fine and gets saved, even if I refresh the current page. However, once I go to other menus in Wordpress admin and come back to my plugin menu, my inputs get deleted. What causes this issue?

Functions.php
Adding the item to menu

function plugin_admin_exist() {
    add_menu_page('plugin', 'plugin', 'manage_options', 'plugin', 'plugin_Dashboard', plugins_url( 'plugin/images/icon.png' ));
}
add_action('admin_menu', 'plugin_admin_exist');

Menu Function:
External .php file for my html tags and php scripts.

function admin_Dashboard() {ob_start();
    require ( plugin_dir_path( __FILE__ ) . 'dashboard.php'); } 

Database Set-up
Create database related to my input fields.

if (get_option('Plugin_Master_Run')) {
    $AdminOption = get_option('Plugin_Master_Run');
} else {
    add_option('Plugin_Master_Run', array(
        'plugin_profile'       => '', 
        'plugin_googleFont'    => '', 
        'plugin_browserIcon'   => '',
        'plugin_regularMobile' => '',
        'plugin_retinaMobile'  => '',
        'plugin_logo'          => '',
        'plugin_retinaLogo'    => ''
    ));
}

Input in external page for menu option:
I have already declared $AdminOption as global variable and included a new array with htmlentities($_POST['plugin_profile'], ENT_QUOTES), for update_option('Plugin_Master_Run', $newAdminValue); and get the new input by $AdminOption = $newAdminValue;

<input name="Admin_profile" value="<?php echo $AdminOption['plugin_profile']; ?>" type="text"/>
  • I tink that you should take a look to Settings API and build the settings page of your plugin using it. – cybmeta Sep 2 '14 at 17:56
  • @cybmeta but How come when i enter text in input and click saved, it get stored? i have even checked the phpmyadmin and it look like the input values are there. but once i go to another page and come back to the plugin it get deleted and overwrite in database. – Farhad Nežad Sep 2 '14 at 18:00
  • Show us the full code you refer as htmlentities($_POST['plugin_profile'], ENT_QUOTES) and update_option('Plugin_Master_Run', $newAdminValue);. I think that you are executing that code in every page, so $_POST['plugin_profile'] is empty if you are not in your options page. Anyway, I recommend again to use the Settings API for your purpose (build a page in the backend to manage options). – cybmeta Sep 2 '14 at 18:25
  • @cybmeta i thing imma look more into Setting API (sound promising ), now, will that allow me to pull from database to theme? after input saved. – Farhad Nežad Sep 2 '14 at 19:49
  • Oh, yes. The settings API is used to build the settings page in the backend. Once a option is saved, you can use get_option() to get the value in a theme, plugin or anywhere within Wordpress. – cybmeta Sep 2 '14 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.