最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - wordpress form not saving or updating

programmeradmin1浏览0评论

i have been looking for a bug in my plugin am working on for days now and i haven't gotten any explanation that can solve my problem. the objective of the plugin is to save an array of options to the database using the update_option. unfortunately when the submit button is clicked, it refreshes to the same page and nothing happens or saves to options table in WordPress. no error either, although, i noticed the message notifiers is also not called either when i click reset or submit. i am hoping someone can point out what i am missing or not doing right,am fairly new to WordPress development.

code:

global $pluginmename, $shortname, $options;
$pluginmename = "Fego-edu CPanel";
$shortname = "fcp";
/**
 * options array. subdivided into sections
 *
 * @since   1.0.0
 * 
 * 
 */ 
$options = array (

    array( "name" => $themename." Options",
        "type" => "title"),


    array( "name" => "General",
        "type" => "section"),
    array( "type" => "open"),

    array( "name" => "Colour Scheme",
        "desc" => "Select the colour scheme for the theme",
        "id" => $shortname."_color_scheme",
        "type" => "select",
        "options" => array("blue", "red", "green"),
        "std" => "blue"),

    array( "name" => "Logo URL",
        "desc" => "Enter the link to your logo image",
        "id" => $shortname."_logo",
        "type" => "text",
        "std" => ""),


    array( "name" => "Custom CSS",
        "desc" => "Want to add any custom CSS code? Put in here, and the    rest is taken care of. This overrides any other stylesheets. eg: 
a.button{color:green}",
        "id" => $shortname."_custom_css",
        "type" => "textarea",
        "std" => ""),        

    array( "type" => "close"),
    array( "name" => "Homepage",
        "type" => "section"),
    array( "type" => "open"),

    array( "name" => "Homepage header image",
        "desc" => "Enter the link to an image used for the homepage 
header.",
        "id" => $shortname."_header_img",
        "type" => "text",
        "std" => ""),

    array( "name" => "Homepage featured category",
        "desc" => "Choose a category from which featured posts are drawn",
        "id" => $shortname."_feat_cat",
        "type" => "select",
        "options" => $wp_cats,
        "std" => "Choose a category"),


    array( "type" => "close"),
    array( "name" => "Footer",
        "type" => "section"),
    array( "type" => "open"),

    array( "name" => "Footer copyright text",
        "desc" => "Enter text used in the right side of the footer. It can 
be HTML",
        "id" => $shortname."_footer_text",
        "type" => "text",
        "std" => ""),

    array( "name" => "Google Analytics Code",
        "desc" => "You can paste your Google Analytics or other tracking 
code in this box. This will be automatically added to the footer.",
        "id" => $shortname."_ga_code",
        "type" => "textarea",
        "std" => ""),    

    array( "name" => "Custom Favicon",
        "desc" => "A favicon is a 16x16 pixel icon that represents your 
site; paste the URL to a .ico image that you want to use as the image",
        "id" => $shortname."_favicon",
        "type" => "text",
        "std" => get_bloginfo('url') ."/favicon.ico"),   

    array( "name" => "Feedburner URL",
        "desc" => "Feedburner is a Google service that takes care of your RSS feed. Paste your Feedburner URL here to let readers see it in your 
website",
        "id" => $shortname."_feedburner",
        "type" => "text",
        "std" => get_bloginfo('rss2_url')),


    array( "type" => "close")

    );
/**
 * Save and reset data settings to database.
 * options for testing. to be changed to usermeta
 *
 * @since   1.0.0
 * 
 * 
 */ 
if ( $_GET['page'] == basename(__FILE__)) {

    if ( 'save' == $_REQUEST['action1'] ) {

        foreach ($options as $value) {
        update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }

        foreach ($options as $value) {
            if( isset( $_REQUEST[ $value['id'] ] ) ) { 
                update_option( $value['id'], 
                $_REQUEST[ $value['id'] ]  ); 
            } 
            else { 
                delete_option( $value['id'] ); 
            } 
        }

header("Location: http://localhost/fego/wp-admin/admin.php?page=fego-edu&action=edit_data&user_id='.$user->ID.'&saved=true");
            //header("Location: admin.php&saved=true");

        die;
    } 
else if( 'reset' == $_REQUEST['action1'] ) {

        foreach ($options as $value) {
            delete_option( $value['id'] ); }

header("Location: http://localhost/fego/wp-admin/admin.php?page=fego-edu&action=edit_data&user_id='.$user->ID.'&reset=true");
        die;    
    }   

}

function wpdocs_theme_name_scripts() {
wp_enqueue_style( 'fego-edu-admin', get_stylesheet_uri() );
wp_enqueue_script( 'fego-edu-admin', get_template_directory_uri() . 
'/js/fego-edu-admin.js', array(), '1.0.0', true );
}

/**
 * Html buildup based of off options array
 *
 *
 * @since   1.0.0
 * 
 * 
 */
if( current_user_can('edit_users' ) ) { ?>
<h2> <?php echo __('Dashboard Information for ' . '( Fullname: '.$user- 
>display_name . '), ( Username: ' . $user->user_login . ')', $this-    >plugin_text_domain ); ?> </h2>
    <br>
     <!--   Main section   -->
    <?php 
    $i=0;
    if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"> 
<p><strong>'
    .$pluginmename.' settings saved.</strong></p></div>';
    if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"> 
<p><strong>'
    .$pluginmename.' settings reset.</strong></p></div>';
    ?>
    <div class="wrap rm_wrap">
    <h2><?php echo $pluginmename; ?> Settings</h2>

    <div class="rm_opts">
        <form method="post">
        <?php foreach ($options as $value) {
        switch ( $value['type'] ) {
        case "open":
        ?>
        <?php break;
        case "close":
        ?>
        </div>
        </div>
        <br />
        <?php break;
        case "title":
        ?>
        <p>To easily use the <?php echo $pluginmename;?> theme, you can use 
the menu below.</p>
        <?php break;
        case 'text':
        ?>
        <div class="rm_input rm_text">
            <label for="<?php echo $value['id']; ?>"><?php echo 
 $value['name']; ?></label>
            <input name="<?php echo $value['id']; ?>" id="<?php echo 
$value['id']; ?>" 
            type="<?php echo $value['type']; ?>" 
            value="<?php if ( get_settings( $value['id'] ) != "") 
            { echo stripslashes(get_settings( $value['id'])  ); } 
            else { echo $value['std']; } ?>" />
        <small><?php echo $value['desc']; ?></small><div class="clearfix"> 
</div>

        </div>
        <?php
        break;

        case 'textarea':
        ?>

        <div class="rm_input rm_textarea">
            <label for="<?php echo $value['id']; ?>"><?php echo 
$value['name']; ?></label>
            <textarea name="<?php echo $value['id']; ?>" type="<?php echo 
$value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) 
!= "") { echo stripslashes(get_settings( $value['id']) ); } else { echo 
$value['std']; } ?></textarea>
        <small><?php echo $value['desc']; ?></small><div class="clearfix"> 
</div>

        </div>

        <?php
        break;

        case 'select':
        ?>
        <div class="rm_input rm_select">
            <label for="<?php echo $value['id']; ?>"><?php echo 
$value['name']; ?></label>

        <select name="<?php echo $value['id']; ?>" id="<?php echo 
$value['id']; ?>">
        <?php foreach ($value['options'] as $option) { ?>
                <option <?php if (get_settings( $value['id'] ) == $option) { 
echo 'selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>
        </select>

            <small><?php echo $value['desc']; ?></small><div 
class="clearfix"></div>
        </div>
        <?php
        break;

        case "checkbox":
        ?>

        <div class="rm_input rm_checkbox">
            <label for="<?php echo $value['id']; ?>"><?php echo 
$value['name']; ?></label>

        <?php if(get_option($value['id'])){ $checked = 
"checked=\"checked\""; }else{ $checked = "";} ?>
        <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php 
echo $value['id']; ?>" value="true" <?php echo $checked; ?> />


            <small><?php echo $value['desc']; ?></small><div 
class="clearfix"></div>
        </div>
        <?php break;
        case "section":

        $i++;

        ?>

        <div class="rm_section">
        <div class="rm_title">
            <h3>
            <img src="<?php 
            bloginfo('template_directory')?>/functions/images/trans.gif" 
            class="inactive" alt="""><?php echo $value['name']; ?>
            </h3><span class="submit"><input name="save<?php echo $i; ?>" 
type="submit" 
            value="Save progress" />
            </span><div class="clearfix"></div></div>
        <div class="rm_options">


        <?php break;

        }
        }
        ?>

    <input type="hidden" name="action1" value="save" />
    </form>
    <form method="post">
    <p class="submit">
    <input name="reset" type="submit" value="Reset all" />
    <input type="hidden" name="action1" value="reset" />
    </p>
    </form>

    <!--    Back Button   -->
    <a href="<?php echo esc_url( add_query_arg( array( 'page' => wp_unslash( $_REQUEST['page'] ) ) ,
     admin_url( 'admin.php' ) ) ); ?>"><?php _e( 'Back', $this->plugin_text_domain ) ?></a>
<?php
}
else {  
?>
    <p> <?php echo __( 'You are not authorized to perform this operation.', 
$this->plugin_text_domain ) ?> </p>
<?php   
}
add_action( 'admin_enqueue_scripts', 'wpdocs_theme_name_scripts' );
//add_action('admin_init', 'save_options');
发布评论

评论列表(0)

  1. 暂无评论