Wordpress functions.php Tutorial


Wordpress is world's most popular blogging platform but now also used as a cms. Its main strenght is customization whether it is theme or want a new feature. Today I am going to discuss role of functions.php in Wordpress theme. Functions.php or the theme functions file is a template used by WordPress themes. It acts like a plugin and gets automatically loaded in both admin and front-end pages of a WordPress site. In functions.php we enque CSS and Javascript Files, define some Custom functions and so one.

Below a Simple functions.php content,

//Load CSS and JavaScripts

add_action( 'wp_enqueue_scripts', 'sp_enqueue_css_and_js' );
function sp_enqueue_css_and_js() {

    // load styles
    $css_url = get_stylesheet_directory_uri().'/css/';
    wp_enqueue_style( 'owl', $css_url.'owl.carousel.css' );
    wp_enqueue_style( 'owltheme', $css_url.'owl.theme.min.css' );
    wp_enqueue_style( 'main-style', get_stylesheet_directory_uri() . '/style.css');

    // load scripts
    $js_url = get_stylesheet_directory_uri().'/js/';
    wp_enqueue_script( 'owl', $js_url.'owl.carousel.min.js', array(), '', true );
    wp_enqueue_script( 'main', $js_url.'custom.js', array('owl'), '', true );
}

add_action( 'wp_enqueue_scripts', 'sp_load_dashicons' );
function sp_load_dashicons() {
    wp_enqueue_style( 'dashicons' );
}

// For uploading Logo using Cutomizer

add_action( 'customize_register', 'sp_customize_register' );
function sp_customize_register( $wp_customize ) {

    // customize sites logo
    $wp_customize->add_section( 'sp_logo' , array(
        'title'      => __( 'Site Logo', 'sp' ),
        'description'      => 'Modify the site logo image'
    ));
    $wp_customize->add_setting( 'site_logo' , array(
        'default'      => ''
    ));
    $wp_customize->add_control( new WP_Customize_Image_Control(
        $wp_customize,
        'site_logo',
        array(
            'label' => __( 'Upload Site Logo', 'sp' ),
            'section' => 'sp_logo',
            'settings' => 'site_logo'
        )
    ));
}

 

//Enable Thumbail Option
add_theme_support( 'post-thumbnails' );
// custom length excerpt
function custom_excerpt_length( $length ) {
    return 40;
}
function new_excerpt_more($more) {
    global $post;
    return '<br/><br/><a class="btn btn-info pull-right" href="'. get_permalink($post->ID) . '"> Read More</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); 


 Category : PHP   Posted By : Sanjay   on 2016-03-31 05:43:20


Related Post

blog comments powered by Disqus

Login



Categories

May I Know You

  • System » Unknown
  • Browser » Unknown
  • IP Address » 54.80.132.10
  • 3 Online