Thursday, July 25, 2013

Today's wp work - day 7

Before I implement any more options in the theme I'm going back to the settings api tutorials and finishing summarizing them.

In the sixth tutorial, http://wp.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-6-menu-pages/, the author is teaching how to introduce a top-level menu, add two submenus that will link to different tabs.   This will be making use of the add_menu_page and add_submenu_page functions I explained in better detail here: http://persteezy.blogspot.com/2013/07/options-panel-day-5-wordpress-menu-types.html

Now this goal is all fine and dandy but I couldn't get the tabs from the 5th tutorial (http://wp.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-5-tabbed-navigation-for-your-settings-page/) to work and since this tutorial builds upon that working I can only summarize what he's saying now.  The 5th tutorial not only didn't work for me but it didn't work for another person as well.

Simply reading through the tut now, the author used add_theme_page and add_menu_page in a function that's hooked to the admin_menu hook.  Then a submenu (goes under top-level) item for one of the tabs is added using the add_submenu_page leaving you with two submenu items.  Two?  Yes, that's because the add_menu_page also leaves the user with a submenu page with the same name and callback function.  After this, a third submenu page is added for the second option.

Author then adds a tab parameter (default value of null) to the *_theme_display func grabs the value of tab and assigns it to a var, creates an if loop that checks what the var equals and reassigns the var depending on what it equals.  Then to utilize this change the author changes the callback of the submenu to something like this:
create_function( null, 'sandbox_theme_display( "social_options" );' ) 
create_function explained: http://php.net/manual/en/function.create-function.php

Now this would be great to implement if the tabs worked but they don't and I don't plan on looking for a solution until another time.  Honestly I think jQuery is probably the better solution here because (A) I'm not very good with ajax and (B) it cuts back on load time majorly.  Now I'm onto tut #7: http://wp.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-7-validation-sanitisation-and-input-i/

Validation - the process by which data entered into the options page by the user is examined and it is determined whether it's acceptable to save.

Sanitization - the process by which data coming out of the database is assured to be clean and properly formatted for rendering.

So validation should be done before data gets written to the db and sanitization should be done between loading the data from the db and displaying it in the browser.  The author then goes to add a new input_examples function, create a submenu for it, and add it to the tabs like the other functions.  He then adds an input field to the tab that will be used to test validation.

The third parameter for the register_setting function is what gets used for the validation function callback.  The function should accept the $input parameter which will obviously hold the data to be validated.  This function will normally follow these three steps:
  • Create an array for storing validated options
  • Validate the options
  • Return the created array
In the author's validation function he does this then uses a foreach to loop through the inputs.  If the loop is not null (isset: http://php.net/manual/en/function.isset.php) then the author makes use of two php functions to validate the input and add it too $ouput at the same time:
Not sure why the user doesn't try making use of the esc_html and esc_url wp functions but I guess I'll just have to continue reading and find out.  Here's the validation function in my code:
function b4b_theme_display_options_validation($input){
$output = array();
foreach($input as $key => $value){
if(isset($input[$key])){
$output[$key] = strip_tags(stripslashes($input[$key]));
}
}
return apply_filters('b4b_theme_display_options_validation', $output, $input);
}
apply_filters(http://codex.wordpress.org/Function_Reference/apply_filters) is used because it is just known as a best practice (idk).

The author then uses sanitize_text_field(http://codex.wordpress.org/Function_Reference/sanitize_text_field) for echoing the options.

This concludes the tutorial and my wp work for today.  The author included me with one link that I'm now using as a reference: http://codex.wordpress.org/Data_Validation
One more tut in the series to go and I plan on finishing it tomorrow.

No comments:

Post a Comment