2017-08-02 7 views
0

私はワードプレスのテーマを構築しています。私のindex.phpでは、get_theme_mod()を使ってオプションを取得し、そのオプションに応じて起動するか起動しないかのコードをいくつか持っています。なぜget_theme_mod()が致命的なエラーをスローする:ワードプレスの未定義関数を呼び出す

<!-- index.php --> 
<?php 
/** 
* The main template file 
* 
*/ 
if (!empty(get_theme_mod('czen_hero'))) { 
    $header_logo = get_theme_mod('czen_hero'); 
} 

if (!empty(get_theme_mod('czen_hero_text'))) { 
    $header_text = get_theme_mod('czen_hero_text'); 
} 

これらのオプションはfunctions.phpで定義され、wordpress 'Customizerによって設定されます。

// HOMEPAGE HEADER 
$wp_customize->add_section('czen_hero_section' , array(
     'title'  => __('Custom Homepage Header', 'coffee-zen'), 
     'priority' => 40, 
     'description' => 'Upload an image and enter some text to create a custom header', 
)); 

$wp_customize->add_setting('czen_hero' , array ('sanitize_callback' => 'czen_sanitize_setting',)); 
$wp_customize->add_setting('czen_hero_text', array ('sanitize_callback' => 'czen_sanitize_setting',)); 

$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'czen_hero', array(
     'label' => __('Header Image', 'coffee-zen'), 
     'section' => 'czen_hero_section', 
     'settings' => 'czen_hero', 
))); 

$wp_customize->add_control('header_text', array(
     'label' => __('Header Text', 'coffee-zen'), 
     'section' => 'czen_hero_section', 
     'settings' => 'czen_hero_text', 
     'type' => 'text', 
)); 

コードが機能します。これらの設定が設定されると、htmlが想定される場所に表示されます。私が調べたときにしかし、私はフロントエンドにページをロードするたびに、私はこれを参照してください。

<!-- index.php --> 
<br /> 
<b>Fatal error</b>: Uncaught Error: Call to undefined function get_theme_mod() in /var/www/vhosts/zentest/wp-content/themes/coffee-zen/index.php:7 
Stack trace: 
#0 {main} 
    thrown in <b>/var/www/vhosts/zentest/wp-content/themes/coffee-zen/index.php</b> on line <b>7</b><br /> 

私は関数が879行目に/wp-includes/theme.php、ライン855で定義されていることを確認することができます。 :

function get_theme_mod($name, $default = false) { 
    $mods = get_theme_mods(); 

    if (isset($mods[$name])) { 
     /** 
     * Filters the theme modification, or 'theme_mod', value. 
     * 
     * The dynamic portion of the hook name, `$name`, refers to 
     * the key name of the modification array. For example, 
     * 'header_textcolor', 'header_image', and so on depending 
     * on the theme options. 
     * 
     * @since 2.2.0 
     * 
     * @param string $current_mod The value of the current theme modification. 
     */ 
     return apply_filters("theme_mod_{$name}", $mods[$name]); 
    } 

    if (is_string($default)) 
     $default = sprintf($default, get_template_directory_uri(), get_stylesheet_directory_uri()); 

    /** This filter is documented in wp-includes/theme.php */ 
    return apply_filters("theme_mod_{$name}", $default); 
} 

この「致命的なエラー」を防ぐにはどうすればよいですか?それは何の原因ですか?私は他のWordpressのコア機能をうまく使用することができます。

すべてのプラグインを無効にしましたが、問題は解決しません。問題はすべての主要なブラウザで発生します。私はWordPressのバージョン4.8を使用しています。

ありがとうございます。

+0

'/ wp-includes/theme.php'が含まれていないと仮定します。 – ficuscr

+0

あなたのテーマの 'index.php'ファイルがどこかの行に直接ロードされているように聞こえます。 –

+0

これは、おそらくインデックスファイルを直接ロードしようとしているAjax-yのようなものです。簡単な修正は、それらのget_theme_mod()ビットを 'function_exists( 'get_theme_mod')'呼び出しでラップすることです。 – JakeParis

答えて

1

あなたは、あなたのテーマのインデックスファイルにこれを追加することによって、おそらく

if (!defined('ABSPATH')) die(); 

でWordPressのテーマファイルの直接読み込みを防ぐことができ、エラーを防ぐことができます。

+0

これを修正すると....だから私はWordPressが直接、または何かをロードするような何かをしようとしていたと思います... –

関連する問題