4

カスタム WordPress テーマを作成していますが、WordPress と Functions.php について簡単な質問があります。

jQueryカスタム テーマの管理パネルの一部で500 行を実行しています。コードはファイルjQuery内にありfunctions.php、カスタム管理メニュー ページが表示されたときに呼び出されます。

理想的には、JavaScript をfunctions.phpファイルから移動して wp_register_script + wp_enqueue_script を使用したいのですが、スクリプトにいくつかの php コードが混在しています。下記参照:

//Function that Provides Options within the Menu Page
function quz_custom_theme_options(){
  global $quz_default_colors, $quz_default_fonts;

    ?>

   <style type="text/css">';

    <?php   include('admin-style.php'); ?>

   </style>';



<script type="text/javascript">
function asf_toggle_box(element)
{
  if (jQuery(element).next('.expandable').is(':visible') )
  {
    jQuery(element).next('.expandable').fadeOut('slow', function(){
      jQuery(element).val('+ Settings');
      jQuery(element).next('.expandable').find('.indicator').val('');  
    });
  } else {
    jQuery(element).next('.expandable').fadeIn('slow', function(){
      jQuery(element).val('- Settings');
      jQuery(element).next('.expandable').find('.indicator').val('visible');  
    });
  }
}

js内のPHPの例

function quz_reset_colors(t)
  {
    var theme_name = jQuery(t).attr('id').substr(6);
    var color_fields = jQuery(t).parent().find('div.color_admin_area').find('.color-input-wrap > input[type="text"]');
    // jQuery(color_fields).css('border', '1px solid red');
    // console.log(color_fields);
    var colors = new Array();
    <?php
    foreach ($quz_default_colors as $key => $value)
    {
      echo 'var ary = new Array(\'' . implode('\',\'', $value) . '\');' . "\r\n";
      echo 'colors[\'' . $key . '\'] = ary;' . "\r\n";
    }
    ?>
    jQuery(color_fields).each(function(index, elem){
      jQuery(elem).val(colors[theme_name][index]);    
    });

これを処理する最善の方法は何ですか? すべてのJSを別の php ファイルに入れて、上記のステートメントでCSSinclude('my_script.php');で行ったように使用する必要がありますか?

これを機能させるには複数の方法がありますが、最善の方法で行いたいと考えています。考え?

4

2 に答える 2

2

Easy Retweet WordPress Pluginでこれを行うためのちょっとしたトリックを行います。

// Enqueue the script
add_action('template_redirect', 'add_script');

function add_script() {
    wp_enqueue_script('retweet', get_option('home') . '/?retweetjs');
}

add_action('init', 'deliver_js');

function deliver_js() {
    if ( array_key_exists('retweetjs', $_GET) ) {

        header('Content-Type: text/javascript');
        print_retweet_js($options);

        // die after printing js
        die();
    }
}

function print_retweet_js($options) {
     include_once('path/to/your/js/script.php');
}

これがあなたに役立つことを願っています

于 2012-10-05T07:07:33.530 に答える
0
function theme_script() {
?>
    < script type="text / javascript" >
      here is your script
    < /script >
<?php
 wp_enqueue_script( 'theme_script_id', get_bloginfo('template_directory').'/js/theme_script.js', array( 'jquery' ) );
}
 add_action( 'wp_print_scripts', 'theme_script' );
于 2012-10-05T05:20:47.920 に答える