Data Not Saving in Plugin
-
Theme: 2012
Trying to make my own plugin using a tutorial in a book I have and the data is not saving. Not sure why or where to begin. Any pointers would be helpful.
Note: I’ve kept getting an illegal offset warning but I have since turned debugging off.
Code is over 100 lines and is located at a Pastebin:
https://pastebin.com/Q34iMYPk
-
You have several different data inputs, which one(s) are not being saved? I hope you’re not going to say all! Even if so, each different form type (options, widget, metabox, etc.) likely has it’s own quirks about saving data. I suggest you tackle each type individually. Throwing 100 (or 300, including blank and comments!) lines of code at us does not encourage anyone to help you!
Posting just the code relating to, say, a widget, is much more dealable. Or just post the same link with the relevant line numbers noted. Also, selecting PHP syntax highlighting when posting to pastebin is helpful too.
With formalities out of the way, I’m just going to address your options page right now. Anytime your form posts to options.php you must use the Settings API for your data to be handled correctly. This includes using add_settings_field() and register_setting() for every input field you use.
Using the Settings API can get rather tedious for extensive forms. This is the price to pay for not having to directly manage the data yourself. Not only saving, but getting the correct values when the form loads. Not to mention formatting and CSS is also handled. The alternative is to manage everything yourself, have the form submit to itself and include code to sanitize and save data right on the same code page. Option pages are not setup for this arrangement, but there are workarounds.
Let’s get your options properly saved first, then we can address any other problems you might have.
I’m going to be offline for a day or so, so if anyone else wants to jump in in the interim, feel free!
Thanks for the pointers about where to look. I’ll spend sometime wrestling with the API and those functions.
I apologize for the code not being in the proper format, though I did read over the posting rules/guidelines for this forum and it didn’t mention anything about php syntax being turned on.
Could we got the mods to add that phrase so other users don’t run into that issue like I did?
I am not sure where to begin but here is the code that creates the settings page. I can enter in data and I get the green phrase “settings saved” at the top BUT when I do the data that I had entered is gone.
Before: https://pasteboard.co/qb8hf6U.png
After: https://pasteboard.co/qb9EERt.png
[Large code excerpt removed by moderator per forum rules. Please use the pastebin for all large code excerpts. It works better anyway.]
Any chance you can share the entire code base here? I’d be willing to help out if it’s relatively soon. I won’t be around after 4:30PM EST.
Evan
Sure can, though it is a bit long (I included a link to the code on pastebin at the top of this thread. The code definitely needs to be cleaned up a bit but I figure once I get it saved properly I can format it better. I’ve been unable to make heads or tails of getting the options page to save. I know there is a pattern and once I can figure out how to make one part save I can apply it to the meta box data.
[Large code excerpt removed by moderator per forum rules. Please use the pastebin for all large code excerpts. It works better anyway.]
Sure can, though I think I am limited to how much code I can post here.
I’ve included a link to pastebin…this time with PHP syntax turned on.
Once I can figure out how to make the options data save properly I can take that pattern and apply it to the meta box data.
Awesome, thanks pcosta88. I’m taking a look into the issue now!
I will report back here after I play with it a bit.
Evan
Hey Pat,
I was able to locate the issue in the code after playing around on a localhost install on my end.
In short, inside of your
register_setting()
function your sanitize callback didn’t properly match the function name below it. You had:register_setting ('school-settings-group', 'school_options', 'school_sanitize_options');
Note: school_sanitize_options in the callback
Your function name was
school_sanitize_settings
.In your original code these two lines were lines #119 and #122 respectively.
On top of that I went ahead and updated some of your widget code. There was also a few issues there which I noticed when some errors were being thrown in the header of the admin dashboard. Things should be all cleared up now tho.
Take a look at the following bit of code – I’ve updated your original and pasted a copy on my pastebin account.
Evan
Evan,
Super Thumbs up! Thank you so much!!
What clued you in that it was a register_setting () function?
I’d be interested in knowing your process of how you narrowed it down to that function.When you say you updated the widget code what do you mean?
Where the errors being shown in the DB related to outdated widget code?
Hey Pat,
Honestly, I just had the code open and the plugin installed on a localhost install that I have on my computer. I do quite a bit of plugin development at work and in my spare time, so I was fairly familiar with how things were to be set up. I just read through the code line by line, and realized the callback function wasn’t the same as the actual function name. I also cleaned up the formatting on a few lines to help with readability. It may have gotten a little messed up when you pasted it into pastebin.
I was doing also some testing along the way. I noticed upon initial activation there were settings that existed, but after saving the page – the options array was non existent – which again lead me to believe that something was up with the callback function for saving the data into the database.
In regards to the widget, there was a fatal error being thrown. I re-worked the widget initialization code so that it uses more up to date code. You were using a deprecated function (which still worked, but was throwing a warning). With the new code you shouldn’t be seeing the warning any longer. I believe the warning was on
idgets.php
.Evan
Thanks for the response.
I running the plugin on my site The settings page saves but the data in a Product meta box does not.
Image https://pasteboard.co/zOyTbNv.png
I then removed a set of curey braces at line 222 and 238: https://pastebin.com/140pfb6L
and that got the Product meta data to save.However, the short-codes are not working. I’ve posted what appears to be the relevant shortcode code
//create shortcode function school_store_shortcode ($atts, $content=null) { global $post; extract (shortcode_atts ( array( "show"=> '' ), $atts ) ); //Load Options Arrary $ss_options_arr = get_option ('school_options'); if ($show=='sku') { $ss_show = get_post_meta ($post->ID, '_school_product_sku', true); }elseif ($show =='price') { $ss_show = $ss_options_arr ['currency_sign']. get_post_meta ( $post->ID, '_school_product_price', true); }elseif ($show =='weight') { $ss_show = get_post_meta ( $post->ID, '_school_product_weight', true); }elseif ($show =='color') { $ss_show = get_post_meta ( $post->ID, '_school_product_color', true); }elseif ($show =='inventory') { $ss_show = get_post_meta ( $post->ID, '_school_product_inventory', true); } // return the shortcode value return $ss_show; }
Thoughts on how to approach this?
Let me take a look. I will report back here shortly.
Evan
Do you have
WP_DEBUG
turned on in wp_config.php? If you turn it on, you’ll see that when you use the shortcode[ss]
, an error is thrown.If you add a
show
attribute to the shortcode, and data has been saved to that product, you should get what you’re after.[ss show="sku"]
If you want to prevent errors, you can also add a check just below where you extract the shortcode attributes (around line #250):
// prevent warning from being thrown if you forgot to include the show attribute if( empty( $atts['show'] ) ) { return; }
Evan
Evan,
I have it turned on yet no errors related to the shortcode appear. The only one that alerts me the constructor method for WP_Widget is depreciated.
When you say add a show attribute to the shortcode did you mean to add it here
extract (shortcode_atts ( array( "show"=> '' ), $atts ) );
to be this
extract (shortcode_atts ( array( "show"=> 'sku' ), $atts ) );
or did you mean somewhere else?
Pat,
You should have copied the code I provided on my bit bucket account in the previous comment. The widget issue was resolved.
Also as mentioned the short code works as expected on my end as long as you use it the same way I have in my previous comment.
Evan
- The topic ‘Data Not Saving in Plugin’ is closed to new replies.