Josh 2017-02-21T10:30:11
Functions play a big part in how your WordPress theme works as its performs as a plugin would.
You can add code snippets to improve the functionality and features of your site, you can add your own functions, call PHP functions or hook into a WordPress function.
It really is a useful file and here is some of my most used WordPress snippets to help me solve problems I face from time to time.
I know that many people would say that there are plugins to do these functions, Yes there are, but less is more, if its only 8 lines of code that you can easily paste in the functions file then why use a plugin.
So lets begin.
Adds 3 custom sidebar for you to use in the widget section of WordPress. simply remove / add as many as you need. (register_sidebar codex)
<?php function custom_sidebar_function() { register_sidebar( array ( 'name' => __( 'Custom Sidebar 1', 'text-domain' ), 'id' => 'custom-sidebar-widget1', 'description' => __( 'Custom Sidebar 1', 'text-domain' ), 'before_widget' => '', 'after_widget' => "", 'before_title' => '', 'after_title' => '' ) ); register_sidebar( array ( 'name' => __( 'Custom Sidebar 1', 'text-domain' ), 'id' => 'custom-sidebar-widget1', 'description' => __( 'Custom Sidebar 1', 'text-domain' ), 'before_widget' => '', 'after_widget' => "", 'before_title' => '', 'after_title' => '' ) ); register_sidebar( array ( 'name' => __( 'Custom Sidebar 1', 'text-domain' ), 'id' => 'custom-sidebar-widget1', 'description' => __( 'Custom Sidebar 1', 'text-domain' ), 'before_widget' => '', 'after_widget' => "", 'before_title' => '', 'after_title' => '' ) ); } add_action( 'widgets_init', 'custom_sidebar_function' ); ?>
Adds a custom menu to wordpress which you can then use in your widgets. (register_nav_menu codex)
<?php add_action( 'init', 'register_custom_menu' ); function register_custom_menu() { register_nav_menu('custom-menu',__( 'Custom Menu' )); } ?>
Adds a settings page to the settings panel in WordPress with a textbox setting to get you started. (add_submenu_page codex) (Options Menu Video Tutorial) (Settings Video Tutorial)
<?php //myTheme ADMIN MENU add_action('admin_menu', 'myTheme_menu'); function myTheme_menu() { add_submenu_page( 'options-general.php', 'myTheme Settings', 'myTheme Settings', 'edit_posts', basename(__FILE__), 'coupon_settingspage' ); add_action( 'admin_init', 'myTheme_settings'); } //myTheme REGISTER SETTINGS function myTheme_settings() { register_setting( 'myTheme-sgroup', 'textBox' ); } //myTheme SETTINGS PAGE function myTheme_settingspage() { if (!current_user_can('manage_options')) { wp_die( __('You do not have sufficient privileges to access this page.') ); } $textBox = esc_attr( get_option('textBox') ); ?> <div class="wrap"> <?php settings_errors(); ?> <form method="post" action="options.php"> <?php settings_fields( 'myTheme-sgroup' ); ?> <?php do_settings_sections( 'myTheme-sgroup' ); ?> <input type="text" id="textBox" name="textBox" value="<?php echo $textBox; ?>"> <?php submit_button(); ?> </form> </div> <?php }
Remember to create a main.css file and scripts.js file in your theme directory and update the directory paths. (wp_enqueue_style codex) (Video Tutorial)
<?php function add_theme_styles() { wp_enqueue_style( 'custom-styles', get_stylesheet_directory_uri() . '/assets/css/main.css',array(),null,'all'); } function add_theme_scripts() { wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/assets/js/min/scripts-min.js',array('jquery'), null, true); } add_action('wp_enqueue_scripts', 'add_theme_styles'); add_action('wp_enqueue_scripts', 'add_theme_scripts'); ?>
Add that precious tracking code to the header of your site with ease. (wp_head codex)
<?php add_action('wp_head', 'add_google_analytics'); function add_google_analytics() { ??> // Paste your Google Analytics code here <?php } ??>
Add a custom shortcode to do what you want with the one below shows a h1 title and a h3 subtitle. (add_shortcode codex) (Video Tutorial)
<?php // [myshortcode title="My Title" subtitle="My Subtitle"] function shortcode_func( $atts ) { $a = shortcode_atts( array( 'title' => 'something', 'subtitle' => 'something else', ), $atts ); $message = '<h1>' . $a['title'] . '</h1>'; $message .= '<h3>' . $a['subtitle'] . '</h3>'; return $message; } add_shortcode( 'myshortcode', 'shortcode_func' ); ?>
Puts a space between the second to last word so no paragraph ends with one word on a new line, not really a function more JavaScript but still useful
<?php add_action('wp_footer', 'add_orphan_script'); function add_orphan_script() { ?> <script type="text/javascript"> jQuery(function($){ $(document).ready(function(){ $('p').each(function(){var string = $(this).html();string = string.replace(/ ([^ ]*)$/,' $1');$(this).html(string);}); }); }); </script> <?php } ?>
Useful when your enqueueing scripts or when your referencing an image in your theme directory.
Theme / Child Directory – /wp-content/themes/myTheme
<?php echo get_stylesheet_directory_uri(); ?>
Plugin Directory – /wp-content/plugins/
<?php echo plugins_url( '/assets/css/main.css', __FILE__ ); ?>
Show something on one page but not the others? run scripts on the home page? make a certain page a different colour?
All can be achieved with conditional tags, they are used to do exactly that, run a specific piece of code on a specific post or page.
<?php add_action('wp_head', 'conditional_tags'); function conditional_tags() { if ( is_front_page() && is_home() ) { ?> //FRONT PAGE CSS <?php } elseif ( is_front_page() ) { // static homepage } elseif ( is_home() ) { // blog page } elseif ( is_page(PAGE ID) ) { // a page } elseif ( is_post(POST ID) ) { // a post } } ?>
For SEO useful if you want to make sure Robots stay on your site and dont drift off somewhere you dont want them to go.
<?php add_action('wp_footer', 'add_orphan_script'); function add_orphan_script() { ?> <script type="text/javascript"> jQuery(function($){$( 'a' ).each(function() { if( location.hostname === this.hostname || !this.hostname.length ) { } else { $(this).attr('rel','nofollow'); } }); </script> <?php } ?>
For SEO echo the contents of the Yoast meta boxes on the post or page, using the below snippets.
Will Require a bit of PHP knowledge
Echo Focus Keyword
<?php echo get_post_meta(get_the_ID(), '_yoast_wpseo_focuskw', true); ?>
Echo Meta Description
<?php echo get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true); ?>
If you have any problems or questions please let me know in the comments and I will happily assist.
Professional Web Development Agency in Doncaster, Worksop & Barnsley | Fisher INC ltd