如何从WordPress选项页面单选按钮更新wp_enQueue_脚本()

时间:2018-01-14 作者:Mona Coder

请看一下这个脚本,让我知道如何更新wp_enqueue_scripts () 在主题中functions.php 基于自定义选项页中的单选选项?

我的功能是什么。php是

function theme_scripts() {
    wp_enqueue_script( \'script\', get_template_directory_uri() . \'/js/scripts.js\');
    wp_enqueue_script( \'Map\', get_template_directory_uri() . \'/js/Google.js\' );
}

add_action( \'wp_enqueue_scripts\', \'theme_scripts\');
现在我需要更改

wp_enqueue_script( \'Map\', get_template_directory_uri() . \'/js/Google.js\' );

wp_enqueue_script( \'Map\', get_template_directory_uri() . \'/js/ESRI.js\' );
或者反过来换收音机

<?php

function map-settings()
{
    add_settings_section("section", "Section", null, "map");
    add_settings_field("style-radio", "Map Radio Buttons", "map_radio_display", "map", "section");  
    register_setting("section", "style-radio");
}

function map_radio_display()
{
   ?>
        <input type="radio" name="style-radio" value="1" <?php checked(1, get_option(\'style-radio\'), true); ?>>ERSI Map
        <input type="radio" name="style-radio" value="2" <?php checked(2, get_option(\'style-radio\'), true); ?>>Google Map
   <?php
}

add_action("admin_init", "map-settings");

function map_op_page()
{
  ?>
      <div class="wrap">
         <h1>Map Script</h1>

         <form method="post" action="options.php">
            <?php
               settings_fields("section");

               do_settings_sections("map");

               submit_button(); 
            ?>
         </form>
      </div>
   <?php
}

function menu_item()
{
  add_submenu_page("options-general.php", "Map", "Map", "manage_options", "map", "map_op_page"); 
}

add_action("admin_menu", "menu_item");

1 个回复
SO网友:Peter HvD

Try this:

$mapScript = ( get_option(\'style-radio\') === 1 ) ? "ESRI.js" : "Google.js";
wp_enqueue_script( \'Map\', get_template_directory_uri() . \'/js/\' . $mapScript );
结束

相关推荐