PHP Code Snippets

How to add a custom menu

To create custom menus, declare in PHP them use in HTML templates

  1. // PHP
  2. // example register a custom menu.
  3. // note: example of menu display in tf_examples.php
  4.  
  5. // turn this on to activate
  6. //add_action( 'init', 'register_menus' );
  7. function register_menus() {
  8.         add_theme_support( 'nav-menus' );
  9.         register_nav_menu ('join_menu', __('Join Us Menu'));
  10. }
// PHP
// example register a custom menu.
// note: example of menu display in tf_examples.php

// turn this on to activate
//add_action( 'init', 'register_menus' );
function register_menus() {
        add_theme_support( 'nav-menus' );
        register_nav_menu ('join_menu', __('Join Us Menu'));
}

to use custom menu in HTML template files:

  1. <?php
  2.  wp_nav_menu( array(
  3. 'menu' =>
  4. 'Join Us Menu',
  5. 'theme_location' =>
  6. 'Join Menu Location',
  7. 'top_menu',
  8. 'depth' => 3,
  9.  'container' => false,
  10. 'menu_class' =>
  11.  'nav navbar-nav',
  12.  'walker' => new ashi_services_side_navwalker(),
  13.  'landing_page' => $landing_page, ) );
  14.  ?>
<?php
 wp_nav_menu( array( 
'menu' => 
'Join Us Menu', 
'theme_location' => 
'Join Menu Location', 
'top_menu', 
'depth' => 3,
 'container' => false, 
'menu_class' =>
 'nav navbar-nav',
 'walker' => new ashi_services_side_navwalker(),
 'landing_page' => $landing_page, ) );
 ?>

it is also possible to specify a custom walker function:

  1.                 'walker' => new ashi_services_side_navwalker(),  
                'walker' => new ashi_services_side_navwalker(),  

it is also possible to specify arbitrary variables that can be picked up in the walker function:

  1.                 'limit_to' => $limit_to,
                'limit_to' => $limit_to,

How to add a second sidebar (WP widgetized area)

There is sample code in the child functions.php file that can be uncommented and customized, to create the sidebar.

To render the sidebar in a template use code like:

  1.   <!--?php if ( is_active_sidebar( 'tf-extrasidebar-1' ) ) { ?-->
  <!--?php if ( is_active_sidebar( 'tf-extrasidebar-1' ) ) { ?-->

Here is a demo page.

Enable Featured Images

  1. // example how to add featured images:
  2. // to all post types
  3. add_theme_support( 'post-thumbnails');
  4.  
  5. // just to certain post types (CPT):
  6. add_theme_support( 'post-thumbnails', array( 'tribe_events' ) );
// example how to add featured images:
// to all post types
add_theme_support( 'post-thumbnails');

// just to certain post types (CPT):
add_theme_support( 'post-thumbnails', array( 'tribe_events' ) );

User Emails From

  1. // how to change email "from" info for all user emails (except the "here's your account and password" email)
  2. add_filter('wp_mail_from', 'new_mail_from');
  3. function new_mail_from($old) {
  4.          return "admin@" . $_SERVER['SERVER_NAME'];
  5. }
  6. add_filter('wp_mail_from_name', 'new_mail_from_name');
  7. function new_mail_from_name($old) {
  8.      return 'From Me';
  9. }
// how to change email "from" info for all user emails (except the "here's your account and password" email)
add_filter('wp_mail_from', 'new_mail_from');
function new_mail_from($old) {
         return "admin@" . $_SERVER['SERVER_NAME'];
}
add_filter('wp_mail_from_name', 'new_mail_from_name');
function new_mail_from_name($old) {
     return 'From Me';
}