首页 > 解决方案 > 将代码添加到定制器部分时出错

问题描述

PHP的新手,正在努力学习。我收到此错误:

由于文件 wp-content/themes/softwarehill/customizer.php 第 54 行的错误,您的 PHP 代码更改已回滚。请修复并再次尝试保存。

语法错误,意外的 'if' (T_IF),期望函数 (T_FUNCTION) 或 const (T_CONST)

似乎在代码的卫生部分

class New_Customizer {

private static $instance;

public function register_footer_input() {
add_action( 'wp_footer', array( $this, 'foot_html' ) );
}
public function add_item_to_customizer( $wp_customize ) {

$wp_customize->add_section( 'section_foot_html', array(

'title' => 'Footer HTML', 

'description'   => 'It is recommended to type code in a text editor and then paste it into the field below',  ) );

$wp_customize->add_setting( 'custom_foot_html', array(

'transport' => 'refresh',
'sanitize_callback' => 'sanitize_html', ) );

$wp_customize->add_control( 'custom_foot_html', array(

'label' => 'Custom footer HTML',

'description' => 'Please copy and paste HTML Code Here',    'section' => 'foot_html',

'settings' => 'custom_foot_html',

'type'  => 'textarea',
) );
}

public function foot_html() {

$footer_html = get_theme_mod( 'custom_foot_html', '' );
if ( $footer_html !== '' ) { echo trim( $footer_html ); }
}

/**
 * Adds sanitization callback function: footer html code
 */

if( ! function_exists( 'sanitize_html' ) ) {

function sanitize_html( $input ) {
 return trim( $input );

}
}

new New_Customizer();

标签: phpwordpresswordpress-theming

解决方案


您可能忘记在课堂上关闭括号

class New_Customizer {

    private static $instance;

    public function register_footer_input() {
        add_action( 'wp_footer', array( $this, 'foot_html' ) );
    }

    public function add_item_to_customizer( $wp_customize ) {

        $wp_customize->add_section( 'section_foot_html', array(
            'title' => 'Footer HTML', 
            'description'   => 'It is recommended to type code in a text editor and then paste it into the field below',  ) );
        $wp_customize->add_setting( 'custom_foot_html', array(
            'transport' => 'refresh',
            'sanitize_callback' => 'sanitize_html', ) );

        $wp_customize->add_control( 'custom_foot_html', array(
            'label' => 'Custom footer HTML',
            'description' => 'Please copy and paste HTML Code Here',    'section' => 'foot_html',
            'settings' => 'custom_foot_html',
            'type'  => 'textarea',) );
    }
}

推荐阅读