dqoqnmb163241
2016-09-17 15:53 阅读 37
已采纳

“注意:未定义的索引” - Wordpress插件开发

I'm developing a Wordpress plugin, and have avoided using a setting page, simply because it always causes some kind of error. This time: "Notice: Undefined index". Can anyone spot any obvious mistake that I have done, but can't notice? Thanks!

Here is the faulty code:

<?php

// Block direct access
if(!defined('ABSPATH'))exit;


add_action( 'admin_menu', 'jvc_add_admin_menu' );
add_action( 'admin_init', 'jvc_settings_init' );


function jvc_add_admin_menu(  ) { 

    add_options_page( 'jQuery Version Control', 'jQuery Version Control', 'manage_options', 'jquery_version_control', 'jvc_options_page' );

}


function jvc_settings_init(  ) { 

    register_setting( 'pluginPage', 'jvc_settings' );

    add_settings_section(
        'jvc_pluginPage_section', 
        __( 'Your section description', 'jquery-version-control' ), 
        'jvc_settings_section_callback', 
        'pluginPage'
    );

    add_settings_field( 
        'jvc_core_version', 
        __( 'Select your desired jQuery version.', 'jquery-version-control' ), 
        'jvc_select_jquery_core_version', 
        'pluginPage', 
        'jvc_pluginPage_section' 
    );

        add_settings_field( 
        'jvc_migrate_version', 
        __( 'Select your desired jQuery Migrate version.', 'jquery-version-control' ), 
        'jvc_select_jquery_migrate_version', 
        'pluginPage', 
        'jvc_pluginPage_section' 
    );


}


function jvc_select_jquery_core_version(  ) { 

    $options = get_option( 'jvc_settings' );
  $defaults = array( 'value' => '3.1.0' );
  $options = wp_parse_args( $options, $defaults );
    ?>
    <select name='select-jquery-core-version'>
        <option value='3.1.0' <?php selected( $options['jvc_core_version'], '3.1.0' ); ?>>jQuery Core 3.1.0</option>
        <option value='3.0.0' <?php selected( $options['jvc_core_version'], '3.0.0' ); ?>>jQuery Core 3.0.0</option>
        <option value='2.2.4' <?php selected( $options['jvc_core_version'], '2.2.4' ); ?>>jQuery Core 2.2.4</option>
        <option value='2.2.3' <?php selected( $options['jvc_core_version'], '2.2.3' ); ?>>jQuery Core 2.2.3</option>
    <option value='2.2.2' <?php selected( $options['jvc_core_version'], '2.2.2' ); ?>>jQuery Core 2.2.2</option>
    <option value='2.2.1' <?php selected( $options['jvc_core_version'], '2.2.1' ); ?>>jQuery Core 2.2.1</option>
    <option value='2.2.0' <?php selected( $options['jvc_core_version'], '2.2.0' ); ?>>jQuery Core 2.2.0</option>
    <option value='2.1.4' <?php selected( $options['jvc_core_version'], '2.1.4' ); ?>>jQuery Core 2.1.4</option>
    <option value='2.1.3' <?php selected( $options['jvc_core_version'], '2.1.3' ); ?>>jQuery Core 2.1.3</option>
    <option value='2.1.2' <?php selected( $options['jvc_core_version'], '2.1.2' ); ?>>jQuery Core 2.1.2</option>
    <option value='2.1.1' <?php selected( $options['jvc_core_version'], '2.1.2' ); ?>>jQuery Core 2.1.1</option>
    <option value='2.1.0' <?php selected( $options['jvc_core_version'], '2.1.2' ); ?>>jQuery Core 2.1.0</option>
    <option value='2.0.3' <?php selected( $options['jvc_core_version'], '2.0.3' ); ?>>jQuery Core 2.0.3</option>
    <option value='2.0.2' <?php selected( $options['jvc_core_version'], '2.0.2' ); ?>>jQuery Core 2.0.2</option>
    <option value='2.0.1' <?php selected( $options['jvc_core_version'], '2.0.1' ); ?>>jQuery Core 2.0.1</option>
    <option value='2.0.0' <?php selected( $options['jvc_core_version'], '2.0.0' ); ?>>jQuery Core 2.0.0</option>
    <option value='1.12.4' <?php selected( $options['jvc_core_version'], '1.12.4' ); ?>>jQuery Core 1.12.4</option>
    <option value='1.12.3' <?php selected( $options['jvc_core_version'], '1.12.3' ); ?>>jQuery Core 1.12.3</option>
    <option value='1.12.2' <?php selected( $options['jvc_core_version'], '1.12.2' ); ?>>jQuery Core 1.12.2</option>
    <option value='1.12.1' <?php selected( $options['jvc_core_version'], '1.12.1' ); ?>>jQuery Core 1.12.1</option>
    <option value='1.12.0' <?php selected( $options['jvc_core_version'], '1.12.0' ); ?>>jQuery Core 1.12.0</option>
    <option value='1.11.3' <?php selected( $options['jvc_core_version'], '1.11.3' ); ?>>jQuery Core 1.11.3</option>
    <option value='1.11.2' <?php selected( $options['jvc_core_version'], '1.11.2' ); ?>>jQuery Core 1.11.2</option>
    <option value='1.11.1' <?php selected( $options['jvc_core_version'], '1.11.1' ); ?>>jQuery Core 1.11.1</option>
    <option value='1.11.0' <?php selected( $options['jvc_core_version'], '1.11.0' ); ?>>jQuery Core 1.11.0</option>
    <option value='1.10.2' <?php selected( $options['jvc_core_version'], '1.10.2' ); ?>>jQuery Core 1.10.2</option>
    <option value='1.10.1' <?php selected( $options['jvc_core_version'], '1.10.1' ); ?>>jQuery Core 1.10.1</option>
    <option value='1.10.0' <?php selected( $options['jvc_core_version'], '1.10.0' ); ?>>jQuery Core 1.10.0</option>
    <option value='1.9.1' <?php selected( $options['jvc_core_version'], '1.9.1' ); ?>>jQuery Core 1.9.1</option>
    <option value='1.9.0' <?php selected( $options['jvc_core_version'], '1.9.0' ); ?>>jQuery Core 1.9.0</option>
    <option value='1.8.3' <?php selected( $options['jvc_core_version'], '1.8.3' ); ?>>jQuery Core 1.8.3</option>
    <option value='1.8.2' <?php selected( $options['jvc_core_version'], '1.8.2' ); ?>>jQuery Core 1.8.2</option>
    <option value='1.8.1' <?php selected( $options['jvc_core_version'], '1.8.1' ); ?>>jQuery Core 1.8.1</option>
    <option value='1.8.0' <?php selected( $options['jvc_core_version'], '1.8.0' ); ?>>jQuery Core 1.8.0</option>
    <option value='1.7.2' <?php selected( $options['jvc_core_version'], '1.7.2' ); ?>>jQuery Core 1.7.2</option>
    <option value='1.7.1' <?php selected( $options['jvc_core_version'], '1.7.1' ); ?>>jQuery Core 1.7.1</option>
    <option value='1.7' <?php selected( $options['jvc_core_version'], '1.7' ); ?>>jQuery Core 1.7.0</option>
    <option value='1.6.4' <?php selected( $options['jvc_core_version'], '1.6.4' ); ?>>jQuery Core 1.6.4</option>
    <option value='1.6.3' <?php selected( $options['jvc_core_version'], '1.6.3' ); ?>>jQuery Core 1.6.3</option>
    <option value='1.6.2' <?php selected( $options['jvc_core_version'], '1.6.2' ); ?>>jQuery Core 1.6.2</option>
    <option value='1.6.1' <?php selected( $options['jvc_core_version'], '1.6.1' ); ?>>jQuery Core 1.6.1</option>
    <option value='1.6.0' <?php selected( $options['jvc_core_version'], '1.6.0' ); ?>>jQuery Core 1.6.0</option>
    <option value='1.5.2' <?php selected( $options['jvc_core_version'], '1.5.2' ); ?>>jQuery Core 1.5.2</option>
    <option value='1.5.1' <?php selected( $options['jvc_core_version'], '1.5.1' ); ?>>jQuery Core 1.5.1</option>
    <option value='1.5' <?php selected( $options['jvc_core_version'], '1.5' ); ?>>jQuery Core 1.5.0</option>
    <option value='1.4.4' <?php selected( $options['jvc_core_version'], '1.4.4' ); ?>>jQuery Core 1.4.4</option>
    <option value='1.4.3' <?php selected( $options['jvc_core_version'], '1.4.3' ); ?>>jQuery Core 1.4.3</option>
    <option value='1.4.2' <?php selected( $options['jvc_core_version'], '1.4.2' ); ?>>jQuery Core 1.4.2</option>
    <option value='1.4.1' <?php selected( $options['jvc_core_version'], '1.4.1' ); ?>>jQuery Core 1.4.1</option>
    <option value='1.4.0' <?php selected( $options['jvc_core_version'], '1.4.0' ); ?>>jQuery Core 1.4.0</option>
    <option value='1.3.2' <?php selected( $options['jvc_core_version'], '1.3.2' ); ?>>jQuery Core 1.3.2</option>
    <option value='1.3.1' <?php selected( $options['jvc_core_version'], '1.3.1' ); ?>>jQuery Core 1.3.1</option>
    <option value='1.3' <?php selected( $options['jvc_core_version'], '1.3' ); ?>>jQuery Core 1.3.0</option>
    <option value='1.2.6' <?php selected( $options['jvc_core_version'], '1.2.6' ); ?>>jQuery Core 1.2.6</option>
    <option value='1.2.5' <?php selected( $options['jvc_core_version'], '1.2.5' ); ?>>jQuery Core 1.2.5</option>
    <option value='1.2.4' <?php selected( $options['jvc_core_version'], '1.2.4' ); ?>>jQuery Core 1.2.4</option>
    <option value='1.2.3' <?php selected( $options['jvc_core_version'], '1.2.3' ); ?>>jQuery Core 1.2.3</option>
    <option value='1.2.2' <?php selected( $options['jvc_core_version'], '1.2.2' ); ?>>jQuery Core 1.2.2</option>
    <option value='1.2.1' <?php selected( $options['jvc_core_version'], '1.2.1' ); ?>>jQuery Core 1.2.1</option>
    <option value='1.2' <?php selected( $options['jvc_core_version'], '1.2' ); ?>>jQuery Core 1.2.0</option>
    </select>

<?php

}

function jvc_select_jquery_migrate_version(  ) { 

    $options = get_option( 'jvc_settings' );
  $defaults = array( 'value' => '3.0.0' );
  $options = wp_parse_args( $options, $defaults );
    ?>
    <select name='select-jquery-migrate-version'>
        <option value='3.0.0' <?php selected( $options['jvc_migrate_version'], '3.0.0' ); ?>>jQuery Migrate 3.0.0</option>
        <option value='1.4.1' <?php selected( $options['jvc_migrate_version'], '1.4.1' ); ?>>jQuery Migrate 1.4.1</option>
        <option value='1.4.0' <?php selected( $options['jvc_migrate_version'], '1.4.0' ); ?>>jQuery Migrate 1.4.0</option>
    <option value='1.3.0' <?php selected( $options['jvc_migrate_version'], '1.3.0' ); ?>>jQuery Migrate 1.3.0</option>
    <option value='1.2.1' <?php selected( $options['jvc_migrate_version'], '1.2.1' ); ?>>jQuery Migrate 1.2.1</option>
    <option value='1.2.0' <?php selected( $options['jvc_migrate_version'], '1.2.0' ); ?>>jQuery Migrate 1.2.0</option>
    <option value='1.1.1' <?php selected( $options['jvc_migrate_version'], '1.1.1' ); ?>>jQuery Migrate 1.1.1</option>
    <option value='1.1.0' <?php selected( $options['jvc_migrate_version'], '1.1.0' ); ?>>jQuery Migrate 1.1.0</option>
    <option value='1.0.0' <?php selected( $options['jvc_migrate_version'], '1.0.0' ); ?>>jQuery Migrate 1.0.0</option>
    </select>

<?php

}


function jvc_settings_section_callback(  ) { 

    echo __( 'The appropriate jQuery Migrate version will load automatically.', 'jquery-version-control' );

}


function jvc_options_page(  ) { 

    ?>
    <form action='options.php' method='post'>

        <h2><?php _e( 'jQuery Version Control', 'jquery-version-control' );?></h2>

        <?php
        settings_fields( 'pluginPage' );
        do_settings_sections( 'pluginPage' );
        submit_button();
        ?>

    </form>
    <?php

}

function jvc_jquery_updater() {

    // Deregister core jQuery and Jquery Migrate
    wp_deregister_script( 'jquery' );
    wp_deregister_script( 'jquery-migrate' );

  // Get options
    $options = get_option( 'jvc_settings' );

    // Register new jQuery and jQuery Migrate
    wp_enqueue_script( 'jquery', 'https://code.jquery.com/jquery-' . $options['jvc_core_version'] . '.min.js', false, $options['jvc_core_version'] );
  wp_enqueue_script( 'jquery-migrate', 'https://code.jquery.com/jquery-migrate-' . $options['jvc_migrate_version'] . '.min.js', array( 'jquery' ), $options['jvc_migrate_version'] );

}
// Front-End
add_action('wp_enqueue_scripts', 'jvc_jquery_updater');
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dougu2006 dougu2006 2016-09-17 22:16

    How about add to jvc_select_jquery_core_version(), before select are printed this:

    if( isset($options['jvc_core_version']) && !empty($options['jvc_core_version']) ){
        $jvc_core_version = $options['jvc_core_version'];
    } else {
        $jvc_core_version = '';
    }
    

    Then change options to something like that:

    <option value='3.1.0' <?php selected( $jvc_core_version, '3.1.0' ); ?>>jQuery Core 3.1.0</option>
    

    BTW, don't worry about notices :)

    点赞 评论 复制链接分享

相关推荐