这是function.php中的php函数:
<?php
add_action('admin_menu', 'baw_create_menu');
function baw_create_menu() {
add_menu_page('Bubbles', 'Bubbles', 'administrator', __FILE__, 'baw_settings_page');
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
register_setting( 'baw-settings-group', 'category' );
}
function baw_settings_page() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<?php do_settings_sections( 'baw-settings-group' ); ?>
<textarea style="width:400px" name="category"><?php echo get_option('category'); ?></textarea>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
我想得到字符串:
var category = "<?php echo get_option('category'); ?>";
alert(category);
但警报给了我我的PHP代码:<php echo get_option ('category');?>
而不是像“香蕉”这样的字符串
答案 0 :(得分:2)
var category = <?php echo json_encode(get_option('bulle_index_1')); ?> ;
alert( category );
答案 1 :(得分:0)
无法在.js文件中解析PHP。您需要在.js文件中创建一个函数并传入从PHP返回的字符串:
function fromPHP(phpString) {
var category = phpString;
alert(category);
}
在.php文件中:
fromPHP(<?php echo json_encode(get_option('bulle_index_1')); ?>);