• Tom De Smedt

    (@steven_gardner)


    Hi,

    I’m learning to build a plugin with Classes and I’m stuck trying to take a function as place it inside my class and call it from there.

    The ‘sanitize_callback‘ for my ‘erynpro_global_css‘ setting calls an external function. I want to place that function inside my class and call it from there.

    Can someone help me achieve this.

    Thanks
    Steven

    My Class so far

    <?php
    /**
     * Plugin Name: *************
     * Plugin URI: **********
     * Description: *************
     * Version: ***********
     * Author: *****************
     * Author URI: *************
     * Text Domain: proeryn
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	die();
    }
    
    class Eryn_plugin {
    
        protected $plugin_slug;
        private static $instance;
    
        public static function get_instance() {
            if( null == self::$instance ) {
                    self::$instance = new Eryn_plugin();
            }
            return self::$instance;
        } 
    
        private function __construct() {
    
                add_action( 'customize_register', array( $this, 'eryn_plugin_register_theme_customizer' ));
                add_action( 'wp_head', array( $this, 'eryn_plugin_customizer_css' ));
        } 
    
        public function eryn_plugin_register_theme_customizer( $wp_customize ){
    
            //Section
            $wp_customize->add_section( 'erynpro_custom_css' , array(
                'title'         => __('Custom CSS', 'erynpro'),
                'description'   => 'Add your custom CSS which will overwrite the theme CSS',
                'panel'         => 'eryn_header_panel',
                'priority'      => 999,
            ) );
    
            // Setting
            $wp_customize->add_setting('erynpro_global_css', array(
                'sanitize_callback'	=> 'erynpro_sanitize_textarea',
            ));
    
            // Control
            $wp_customize->add_control(
                new Customize_CustomCss_Control(
                    $wp_customize,
                    'custom_css',
                    array(
                        'label'      => __('Custom CSS', 'erynpro'),
                        'section'    => 'erynpro_custom_css',
                        'settings'   => 'erynpro_global_css',
                        'type'		 => 'custom_css',
                        'priority'	 => 9999
                    )
                )
            );
        }
    
        public function eryn_plugin_customizer_css() {
            ?>
                <style id="pro_eryn_css">
                    <?php if(get_theme_mod( 'erynpro_global_css' )) : ?>
                        <?php echo get_theme_mod( 'erynpro_global_css' ); ?>
                    <?php endif; ?>
                </style>
            <?php
        }
    
    }
    add_action( 'after_setup_theme', array( 'Eryn_plugin', 'get_instance' ) );
    
    function Eryn_plugin_css_customize_register($wp_customize) {
    
        class Customize_CustomCss_Control extends WP_Customize_Control {
            public $type = 'custom_css';
    
            public function render_content() {
                ?>
                <label>
                <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
                <textarea style="width:100%; height:150px;" <?php $this->link(); ?>><?php echo $this->value(); ?></textarea>
                </label>
                <?php
            }
        }
    }
    add_action( 'customize_register', 'Eryn_plugin_css_customize_register' );
    
    function erynpro_sanitize_textarea($input) {
        global $allowedposttags;
        $output = wp_kses( $input, $allowedposttags);
        return $output;
    }
  • The topic ‘adding function to Class’ is closed to new replies.