Trying to write my first Wordpress Plugin .... HELP!

5 replies
I'm trying to create my first plugin and have gotten as far as creating the options admin page, adding it to the menu, showing a drop down box and showing a submit button.

Howsomever, I cannot get the form to save the value, or update an existing option.

As a non-coder, I have gotten myself lost trying to understand the codex and the api, including being unable to copy and paste the options page examples that are shown and make them work. If you can help me, please, please jump right in and tell me what to do.

Here's what I have for a form so far ...

// Plugin title and some other stuff

?>
<!-- Opening The Form -->
<div>
<form method="post" action="options.php">

<?php
settings_fields( 'products' );
?>

<!-- Inserting The Dropdown Box On The Page And Inside The Page Function -->
<!-- Using selected() instead -->
<select name="options[products]">
<option value="unicykes" <?php selected( $options['products'], unicykes ); ?>>unicykes</option>
<option value="bikes" <?php selected( $options['products'], bikes ); ?>>bikes</option>
<option value="trikes" <?php selected( $options['products'], trikes ); ?>>trikes</option>
</select>

<?php
do_settings_fields( 'products' );
?>
<p>
<input type="submit" value="<?php _e( 'Save Options', 'products' ); ?>" />
</p></form>
</div>

<?php
}

// End of page
?>
#admin menu #options page #plugin #wordpress #write
  • Profile picture of the author kevinclanton
    Kirk,

    First, for a "non-coder", you sure have done a lot. wow.
    I am not really sure what you are doing with the selected() function.. I was kinda confused by that when i did a quick once over of your code...

    Here are some things that will hopefully help ya out..

    in your settings panel function
    put this at thetop:

    settings_fields( 'KirksPlugin-options-group' );
    $products = get_option('KirksPlugin_products');

    then all of your option input names will look like:
    <select name="KirksPlugin_products" >
    <option name="x" <?php if ($products == "x") { echo "selected"; } ?> >Product X </option>
    </select>

    Close your settings panel function and put something like this below it:

    function kirkssettingsstuff() {


    register_setting( 'WPConversion-options-group', 'KirksPlugin_options[products]' );



    }




    function KirksPlugin() {

    if (is_admin()) {

    #add_action('admin_init', array(&$this, 'admin_init') );
    add_action( 'admin_init', array(&$this, 'kirkssettingsstuff') );
    add_action('admin_menu', array(&$this, 'kirkspluginSettingsPanel_settings') );

    }
    }


    I know these suggestions are all over the place, but hopefully they will help you out a bit.
    I think the main thing you are missing is registering your settings, and then using get_option to get what is currently set..
    You can always check what has already been set by going to /wp-admin/options.php and scrolling though what is there.

    PS. don't use what i wrote verbatim as i might have some syntax errors in there
    good luck on your project
    {{ DiscussionBoard.errors[7299393].message }}
  • Profile picture of the author Kirk Ward
    Hi Kevin,

    I apologize for being so late in responding. I was watching for the email from the WF telling me someone had responded, and didn't stop to think about the fact that I was having problems with some of my emails not arriving.

    I got the plugin to work, and actually got it to save three values. Hot Diggity.

    It takes awhile when you don't know the rules and are limited to copy and paste and ask. Lots of times I get stuck at the "Oops, that didn't work stage" and spend another two or three days searching around. That's where I was, and what I did for all this time.

    Thanks for your time.
    Kirk
    Signature
    "We are not here to sell a parcel of boilers and vats, but the potentiality of growing rich beyond the dreams of avarice."

    Dr. Samuel Johnson (Presiding at the sale of Thrales brewery, London, 1781)
    {{ DiscussionBoard.errors[7320397].message }}
  • Profile picture of the author Kirk Ward
    PS - What I was doing with the "echo selected" stuff was trying to get the product stored in the database to be the "selected" item in the dropdown. I wound up changing the code to an if statement instead of a wordpress function.

    My copy and paste wasn't working there, so I did an if from memory and my code actually worked, will wonders never cease?
    Signature
    "We are not here to sell a parcel of boilers and vats, but the potentiality of growing rich beyond the dreams of avarice."

    Dr. Samuel Johnson (Presiding at the sale of Thrales brewery, London, 1781)
    {{ DiscussionBoard.errors[7320409].message }}
  • Profile picture of the author eminc
    Hey Kirk,

    A suggestion to tidy up the code a bit.

    In case you have a big list of options in that select box, you would have to put the if condition in every select option, and if one particular value has something wrong, it'll take time to identify where the problem was.

    A possible solution:

    1) Take all select elements in a array Like

    $AllOptions = array("value1"=>"Option 1", "value2"=>"Option 2");

    The array is called a associative array, in which values are in key/value pairs.


    2) Then, you can traverse that array, and check it with the original value. If the value matches your selection. This prints out the whole thing, saves a bit of space too

    <select name="something">
    <?php
    foreach( $AllOptions as $Value => $Label){
    if( $Value == $SavedValue)
    {
    $selected = "selected='selected'";
    }
    else
    {
    $selected = "";
    }
    echo "<option value='$Value' $selected >$Label</option>";
    }
    ?>
    </select>

    This can work for checkboxes and radio buttons also. Saves a little bit of time and space

    My copy and paste wasn't working there, so I did an if from memory and my code actually worked, will wonders never cease?
    Must be a teeny bit of problem somewhere in code. IMHO, While typing the code we are careful not to make a mistake. Its always better to write it out carefully than copying and then checking the whole thing for problems

    My 2 cents

    -Mohit
    Signature

    The best way to predict future is to create it ― Abraham Lincoln

    {{ DiscussionBoard.errors[7320697].message }}
  • Profile picture of the author kevinclanton
    Interesting Mohit. I like that.

    Kirk, I am glad its working for you. I am very impressed that you are taking on such an ambitious project, without a long development history. But honestly, that is one of the best ways to learn. Good luck on it!
    {{ DiscussionBoard.errors[7329452].message }}

Trending Topics