Hi, I'm new to PHP and not really much of a programmer. But I'm a good explainer I have a function like so: add_option($name, $value, $description, $autoload); I want to pass an array to it as follows: 'rup_prelink_msg', '', 'First segment of notification email', 'yes' 'rup_link', '', 'Link to pdf retreival page', 'yes' 'rup_postlink_msg', '', 'Last segment of notification email', 'yes' 'rup_subject', '', 'Subject of notification email', 'yes' 'rup_support_email', '', 'From address for notification email', 'yes' How would that actually be done in with code?? Thanks!
You maybe can't just do it like that. You would probably have to modify your function for that. Maybe something like this if I understand you right. add_option($options) { list($name, $value, $description, $autoload) = $options; // ... rest of function } // Usage: $details = array('rup_prelink_msg', '', 'First segment of notification email', 'yes'); add_option($details); PHP:
OK. Here is what's driving this. The function is built into wordpress and is used to store the options into the database, one row for each option. Typically one just adds the code like so: add_option('rup_prelink_msg', '', 'First segment of notification email', 'yes'); add_option('rup_link', '', 'Link to pdf retreival page', 'yes'); add_option('rup_postlink_msg', '', 'Last segment of notification email', 'yes'); add_option('rup_subject', '', 'Subject of notification email', 'yes'); add_option('rup_support_email', '', 'From address for notification email',); Resulting in five rows added to the database. But the following suggestion is made in the WordPress Codex: "If you are storing more than one option for your plugin, it would be more efficient to pass an array of option settings to add_option(), which will be stored as a single row of database data." So I'm trying to learn the more efficient method. Any ideas? Thanks!
like so <? function add_option($options) { echo "<pre>"; print_r($options); } $data = array ( "one" => array("rup_prelink_msg", "", "First segment of notification email", "yes"), "two" => array("rup_link", "", "Link to pdf retreival page", "yes"), "three" => array("rup_postlink_msg", "", "Last segment of notification email", "yes"), "four" => array("rup_subject", "", "Subject of notification email", "yes"), "five" => array("rup_support_email", "", "From address for notification email", "yes") ); add_option($data); PHP:
Thanks Joe! Somehow I knew it would be a few simple lines of code. Now if I can just figure out what it's doing
Two questions: Single array...how so? and I actually got the array code to work but the database simply had a new record with the option_name field value of "array". I really don't see how a single record can have multiple values as described in the original motive for attempting to do this. Can anyone shed light? Thanks!