Why aren't these WordPress options being saved to the database correctly?

I have a plugin for editing certain options already stored in the database. It creates an options page under Settings with a simple form that populates each field with whatever values are stored in the database for the given field. When the page loads, each of the options loaded into the fields are correct, but if I edit any of the fields and click "Save changes", all of the fields on the page are cleared out, in addition to deleting …
Category: Web

Add Plugin options as subpage to Theme options page

I have my own Theme with an options page. Now I am developing a new Plugin which should have an options page that should be a subpage of the Theme options page. Theme options.php: <?php function xx_theme_admin_menu() { add_menu_page( 'Theme Settings', 'Theme Settings', 'manage_options', 'xx-settings', 'xx_theme_admin_page' , '/wp-content/themes/xx/assets/img/logo-Grayscale-30x25.png' ); } add_action( 'admin_menu', 'xx_theme_admin_menu' ); function xx_theme_admin_page() { ... } Plugin options.php: <?php function xx_cookie_banner_admin_menu() { add_submenu_page( 'xx-settings', 'Cookie Banner', 'Cookie Banner', 'manage_options', 'xx-cookie-banner', 'xx_cookie_banner_admin_page' ); } add_action( 'admin_menu', 'xx_cookie_banner_admin_menu' ); …
Category: Web

How to add new options to my WordPress plugin

I'm the author of a small and simple WordPress plugin based on WodPress Plugin Boilerplate using WordPress Plugin Boilerplate Generator. My plugins is Language Switcher for Transposh: it just allows to get a nicer language switcher if you're using Transposh Language Filter plugin (currently cut off by WordPress repo). In the last version I have added a couple of options but I have realized that during the update the activation hook is not called so the new options aren't written …
Category: Web

Performance of several get_option() calls

I am currently working on my first WordPress plugin. To save and output certain settings I use the native Settings API. Now the question arises, how performant are several calls of the get_option() function. Since I work object-oriented I use a function which returns the options: private function get_settings() { $styles = get_option("style_settings"); return $styles; } Every time I need one or more values from the options array, I now call this function. Is it better to assign the options …
Category: Web

how to show new field if option selected?

I have this code for create setting page for my plugin. with this code , i show two field in plugin setting for change style and set shadow on/off. In style section i have 4 option and i want show new section with options below style section if the second option is selected. commented in ode to better understand : <?php class my_Settings_Page { public function __construct() { add_action( 'admin_menu', array( $this, 'my_create_settings' ) ); add_action( 'admin_init', array( $this, 'my_setup_sections' …
Category: Web

Gathering and logging data from a plugin: how to do it without race conditions?

I have a need to capture data in a WordPress plugin and append it into some sort of log. The data I want happens to be slow SQL queries from $wpdb->queries. I'm hoping to present useful "dirty dozen" SQL queries to my users to help them with optimizations. It's a companion plugin, or maybe a feature set, for this plugin. (I'm aware of the performance issues around this kind of system instrumentation. Capture sessions will be limited in duration. Etc.) …
Category: Web

Change custom post type slug from plugin options

I'd like to give users the option to change a custom post type slug in my plugin. Using this answer, I'm able to hardcode it into a function: Redeclare/Change Slug of a Plugin's Custom Post Type So how can I rewrite this to update when the user updates the plugin option? I tried something like this: function add_custom_rewrite_rule() { $slug = get_option('change_rewrite_slug'); // my plugin option // First, try to load up the rewrite rules. We do this just in …
Category: Web

Make inline uploader (plupload) on options page upload to a specific folder

I’ve set up an options page for my CPT that contains the drag'n'drop upload area (just like Media → Add New). Basicly, I’ve copied all the relevant stuff from wp-admin/media-new.php and it works like charming. This is the code of my options page: /** * Callback for the submenu page */ function syk_bulk_import_callback() { // Taken from media-new.php if ( ! current_user_can( 'upload_files' ) ) { wp_die( __( 'Sorry, you are not allowed to upload files.' ) ); } wp_enqueue_script( …
Category: Web

Error: options page not found when I try to save the changes made in my settings page

I'm developing a plugin... everything was fine but suddenly when I try to save the plugin settings... I found with the error that says the page options not found.. I was looking for some answer but not one fix my error... Following some code that makes the settings page and save the options in the database: admin.php <?php /** * * This file contains the necesary functions to display the admin area of the plugin * * @author Ezequiel Cattaneo …
Category: Web

Save Plugin Options as Array

I am trying to create a simple option page. The options consist of a text field per user role. But I don't want to edit the code every time I add a new user role. So I am trying to "for each"- user roles and build text fields from it. But the value does not get saved in the options. I think I need to save the values in an array before save. Like this: add_settings_field( 'weborder_leveranstext', __( 'Header', 'weborder' …
Category: Web

How to save custom form data in wp_options table

I have some data that I want stored in the wp_option table. I know the code is not right but I want to store data in this way: global $wpdb; if (isset($_POST['add_estimated_satisfaction'])) { // run validation if you're not doing it in js $option_name = $confused_about_treatment_editor; $option_name = $estimated_price_editor; $option_name = $satisfaction_guarantee_editor; $option_value = isset($_POST['confused_about_treatment']) ? $_POST['confused_about_treatment'] : ''; $option_value = isset($_POST['estimated_price']) ? $_POST['estimated_price'] : ''; $option_value = isset($_POST['satisfaction_guarantee']) ? $_POST['satisfaction_guarantee'] : ''; $status = $wpdb->insert('www_options', array( 'option_name' => $option_name, …
Category: Web

update_option() passing empty array() but still updating

I am reviewing some code in a custom plugin but I am not understanding how it could be working. All form fields are indexed by name="opm_settings[name]" At the top of the page inside the function that draws the page is the following code: global $opm_settings; if ( array_key_exists('submit', $_POST) ) { update_option('opm_settings', array());?> <div id="setting-error-settings-updated" class="settings_error notice notice-success is-dismissible"> <p><?php _e('Settings updated.', 'opm_domain'); ?></p> </div> <?php } In the call to update_option() the 2nd parameter being passed is array() which …
Category: Web

Multiple checkbox doesn't work in wordpress settings api

I was creating a checkbox of roles n WordPress. I successfully generated them but the checked function doesn't seem to work. Also, it is throwing this warning. Warning: Illegal string offset 'ue_roles_confirm_Administrator' Here is the code I am using. function username_editor_roles_callback() { global $wp_roles; $option = get_option( 'username_editor_settings' ); $roles = $wp_roles->roles; foreach ($roles as $role) { $roleName = $role['name']; $output = sprintf('<input type="checkbox" name="username_editor_settings[ue_roles_confirm_%1$s]" value="%1$s" %2$s><label>%1$s</label><br>', $roleName, checked(1, $option["ue_roles_confirm_{$roleName}"]) ); echo $output; } } I am unable to find …
Category: Web

Custom plugin: how do I call a PHP file if settings option is set to true?

I am making a custom plugin with various functions that do different things. I have created a settings page using the Settings API to save data to the options db. That works fine. Now I'm trying to "turn on/off" functions from the settings page, with the below example currently being set to "true." If the form value on the settings page equals true, then the function should be called, otherwise it should not be called. What would be the best …
Category: Web

Is it ok to call get_option without hooks?

Many plugins call get_option() to fetch their settings right in the main plugin file, without using any action hook. Is this the way it is meant to be used? I am asking because it seems to me that calling get_option() straight away has two downsides: Extensibility: It makes it hard if not impossibile for devs to reliably filter the plugin options with the option_{$option} filter. A workaround is to use a must-use plugin, but that is not always an option. …
Category: Web

How to automatically redirect to custom admin menu after plugin activation?

I need a function to redirect user to the plugin settings page, right after plugin activation. I use this function to create a custom menu settings page: // add option page menu link function axl_ads_add_admin_menu() { $icon = 'dashicons-align-left"'; add_menu_page( 'AXL Ads', 'AXL Ads', 'manage_options', 'axl_ads', array($this, 'axl_ads_options_page'), $icon, '3' ); } And my settings page url is: wp-admin/admin.php?page=axl_ads Any help with this, please?
Category: Web

Plugin Options not being output

I've been making a plugin for wordpress that adds two simple text field options. The options are saved successfully in plugin options page but don't work, it doesn't output saved options. Here's the code: add_action( 'admin_menu', 'viptips_add_admin_menu' ); add_action( 'admin_init', 'viptips_settings_init' ); function viptips_add_admin_menu( ) { $icon = 'dashicons-editor-table'; add_menu_page( 'VIP Tips', 'VIP Tips', 'manage_options', 'vip_tips', 'viptips_options_page', $icon ); } function viptips_settings_init( ) { register_setting( 'pluginPage', 'viptips_settings' ); add_settings_section( 'viptips_pluginPage_section', __( 'Main plugin options', 'viptips' ), 'viptips_settings_section_callback', 'pluginPage' ); add_settings_field( …
Category: Web

WordPress setting with select - where is my mistake?

I have two option callbacks in my plugin, one with a ckeckbox and one with a select field. The first one is working perfectly, the second not. The select field doesn't save its value. I double checked the code which registers the two settings, I assume my mistake is somewhere in the callback. Any ideas? Checkbox (works): public function myplugin_post_menu_cb() { echo '<input type="checkbox" name="' . $this->option_name . '_post_menu' . '" value="1" "' . checked(1, get_option('myplugin_post_menu'), false) . '" />'; …
Category: Web

Using Checkboxes on Plugin Options Page for Custom Plugin

I am creating my first WordPress plugin. It is a simple plugin that extends Visual Composer allowing you to remove the element options in VC (shown in the screenshot). On the plugin "options" page I am creating a form to show/hide the elements. Here is my code: // Going to add a conditional statement here to run this vc_remove_element function if the checkbox for Row is selected vc_remove_element( "vc_row" ); // Row add_action('admin_menu', 'plugin_admin_settings_tab'); function plugin_admin_settings_tab() { add_options_page('Remove Visual Composer …
Category: Web

Addition of custom option panel crashes Media Library & Admin Area

So, I was carefully following the Wordpress Codex to create a Custom options panel for a custom post type.(https://codex.wordpress.org/Creating_Options_Pages) Specifically, Example 2. Everything was actually working well with the post types and the options panel. However, when I later went to the Media Library it wouldn't load. I tried signing out of the admin area but when I tried to sign back in and it wouldn't load. I had to manually remove the plugin from the directory to get back …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.