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

时间:2016-09-17 15:53:14

标签: php wordpress

我正在开发一个Wordpress插件,并且避免使用设置页面,因为它总是会导致某种错误。这一次:"注意:未定义索引"。任何人都可以发现我做过的任何明显的错误,但是不能注意到吗?谢谢!

这是错误的代码:

<?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 个答案:

答案 0 :(得分:0)

在打印选项之前添加到jvc_select_jquery_core_version()如何:

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

然后将选项更改为:

<option value='3.1.0' <?php selected( $jvc_core_version, '3.1.0' ); ?>>jQuery Core 3.1.0</option>
顺便说一句,不要担心通知:)